| JDK 1.5+ Convert To | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| From | to: boolean t | to: /*signed*/ byte b | to: /*unsigned*/ byte u | to: short s | to: char c | to: int i | to: long n | to: float f | to: double d | to: String g | to: Boolean tt | to: /*signed*/ Byte bb | to: /*unsigned*/ Byte uu | to: Short ss | to: Character cc | to: Integer ii | to: Long nn | to: Float ff | to: Double dd | From |
| boolean t | -------- | // to /*signed*/ byte b from boolean t
b = (byte)(t?1:0); |
// to /*unsigned*/ byte u from boolean t
u = (byte)(t?1:0); |
// to short s from boolean t
s = (short)(t?1:0); |
// to char c from boolean t
// to get '0' and '1' c = (char)(t?'1':'0'); // or to get Unicode value 0 or 1 c = (char)(t?1:0); |
// to int i from boolean t
i = t?1:0; |
// to long n from boolean t
n = t?1:0; |
// to float f from boolean t
f = t?1:0; |
// to double d from boolean t
d = t?1:0; |
// to String g from boolean t
g = String.valueOf(t); |
// to Boolean tt from boolean t
// no conversion necessary in Java 1.5+ |
// to /*signed*/ Byte bb from boolean t
bb = new Byte((byte)(t?1:0)); |
// to /*unsigned*/ Byte uu from boolean t
uu = new Byte((byte)(t?1:0)); |
// to Short ss from boolean t
ss = new Short((short)(t?1:0)); |
// to Character cc from boolean t
// to get '0' or '1' cc = new Character((char)(t?'1':'0')); // or to get Unicode 0 or 1 cc = new Character((char)(t?0:1)); |
// to Integer ii from boolean t
// best JDK 1.5+ ii = Integer.valueOf(t?1:0); // or JDK 1.4- ii = new Integer(t?1:0); |
// to Long nn from boolean t
// best JDK 1.5+ nn = Long.valueOf(t?1:0); // or JDK 1.4- nn = new Long(t?1:0); |
// to Float ff from boolean t
ff = new Float(t?1:0); |
// to Double dd from boolean t
dd = new Double(t?1:0); |
boolean t |
| /*signed*/ byte b | // to boolean t from /*signed*/ byte b
t = b != 0; |
-------- | // to /*unsigned*/ byte u from /*signed*/ byte b
// no conversion necessary in Java 1.5+ |
// to short s from /*signed*/ byte b
// no conversion necessary in Java 1.5+ |
// to char c from /*signed*/ byte b
// no conversion necessary in Java 1.5+ |
// to int i from /*signed*/ byte b
// no conversion necessary in Java 1.5+ |
// to long n from /*signed*/ byte b
// no conversion necessary in Java 1.5+ |
// to float f from /*signed*/ byte b
// no conversion necessary in Java 1.5+ |
// to double d from /*signed*/ byte b
// no conversion necessary in Java 1.5+ |
// to String g from /*signed*/ byte b
// best for readability g = Integer.toString(b); // best for maintainability g = String.valueOf(b); // or g = Integer.toString(b, 7 /* radix */); // or g = Integer.toBinaryString(b); // or g = Integer.toOctalString(b); // or g = Integer.toHexString(b); // or kludgy and slow on unoptimised Javas g = "" + b; |
// to Boolean tt from /*signed*/ byte b
tt = (b != 0) ? Boolean.TRUE : Boolean.FALSE; |
// to /*signed*/ Byte bb from /*signed*/ byte b
// no conversion necessary in Java 1.5+ |
// to /*unsigned*/ Byte uu from /*signed*/ byte b
// no conversion necessary in Java 1.5+ |
// to Short ss from /*signed*/ byte b
// no conversion necessary in Java 1.5+ |
// to Character cc from /*signed*/ byte b
// no conversion necessary in Java 1.5+ |
// to Integer ii from /*signed*/ byte b
// no conversion necessary in Java 1.5+ |
// to Long nn from /*signed*/ byte b
// no conversion necessary in Java 1.5+ |
// to Float ff from /*signed*/ byte b
// no conversion necessary in Java 1.5+ |
// to Double dd from /*signed*/ byte b
// no conversion necessary in Java 1.5+ |
/*signed*/ byte b |
| /*unsigned*/ byte u | // to boolean t from /*unsigned*/ byte u
t = u != 0; |
// to /*signed*/ byte b from /*unsigned*/ byte u
// no conversion necessary in Java 1.5+ |
-------- | // to short s from /*unsigned*/ byte u
// no conversion necessary in Java 1.5+ |
// to char c from /*unsigned*/ byte u
// no conversion necessary in Java 1.5+ |
// to int i from /*unsigned*/ byte u
// no conversion necessary in Java 1.5+ |
// to long n from /*unsigned*/ byte u
// no conversion necessary in Java 1.5+ |
// to float f from /*unsigned*/ byte u
// no conversion necessary in Java 1.5+ |
// to double d from /*unsigned*/ byte u
// no conversion necessary in Java 1.5+ |
// to String g from /*unsigned*/ byte u
// best for readability g = Integer.toString(u & 0xff); // best for maintainability g = String.valueOf(u & 0xff); // or g = Integer.toString(u & 0xff, 7 /* radix */); // or g = Integer.toBinaryString(u & 0xff); // or g = Integer.toOctalString(u & 0xff); // or g = Integer.toHexString(u & 0xff); // or kludgy and possibly slow g = "" + (u & 0xff); |
// to Boolean tt from /*unsigned*/ byte u
tt = (u != 0) ? Boolean.TRUE : Boolean.FALSE; |
// to /*signed*/ Byte bb from /*unsigned*/ byte u
// no conversion necessary in Java 1.5+ |
// to /*unsigned*/ Byte uu from /*unsigned*/ byte u
// no conversion necessary in Java 1.5+ |
// to Short ss from /*unsigned*/ byte u
// no conversion necessary in Java 1.5+ |
// to Character cc from /*unsigned*/ byte u
// no conversion necessary in Java 1.5+ |
// to Integer ii from /*unsigned*/ byte u
// no conversion necessary in Java 1.5+ |
// to Long nn from /*unsigned*/ byte u
// no conversion necessary in Java 1.5+ |
// to Float ff from /*unsigned*/ byte u
// no conversion necessary in Java 1.5+ |
// to Double dd from /*unsigned*/ byte u
// no conversion necessary in Java 1.5+ |
/*unsigned*/ byte u |
| short s | // to boolean t from short s
t = s != 0; |
// to /*signed*/ byte b from short s
b = (byte)s; |
// to /*unsigned*/ byte u from short s
u = (byte)s; |
-------- | // to char c from short s
// no conversion necessary in Java 1.5+ |
// to int i from short s
// no conversion necessary in Java 1.5+ |
// to long n from short s
// no conversion necessary in Java 1.5+ |
// to float f from short s
// no conversion necessary in Java 1.5+ |
// to double d from short s
// no conversion necessary in Java 1.5+ |
// to String g from short s
// best for readability g = Integer.toString(s); // best for maintainability g = String.valueOf(s); // or g = Integer.toString(s, 7 /* radix */); // or g = Integer.toBinaryString(s); // or g = Integer.toOctalString(s); // or g = Integer.toHexString(s); // or kludgy and possibly slow g = "" + s; |
// to Boolean tt from short s
tt = (s != 0) ? Boolean.TRUE : Boolean.FALSE; |
// to /*signed*/ Byte bb from short s
bb = new Byte((byte)s); |
// to /*unsigned*/ Byte uu from short s
uu = new Byte((byte)s); |
// to Short ss from short s
// no conversion necessary in Java 1.5+ |
// to Character cc from short s
// no conversion necessary in Java 1.5+ |
// to Integer ii from short s
// no conversion necessary in Java 1.5+ |
// to Long nn from short s
// no conversion necessary in Java 1.5+ |
// to Float ff from short s
// no conversion necessary in Java 1.5+ |
// to Double dd from short s
// no conversion necessary in Java 1.5+ |
short s |
| char c | // to boolean t from char c
// to convert '0' or '1' t = c != '0'; // to convert Unicode 0 or 1 t = c != 0; |
// to /*signed*/ byte b from char c
// international b = (byte)Character.digit(c, 10 /* radix */); // fastest '9' -> 9 b = (byte)(c - '0'); // or to get Unicode value b = (byte)c; |
// to /*unsigned*/ byte u from char c
// international u = (byte)Character.digit(c, 10 /* radix */); // fastest '9' -> 9 u = (byte)(c - '0'); // or to get Unicode value u = (byte)c; |
// to short s from char c
// no conversion necessary in Java 1.5+ |
-------- | // to int i from char c
// no conversion necessary in Java 1.5+ |
// to long n from char c
// no conversion necessary in Java 1.5+ |
// to float f from char c
// no conversion necessary in Java 1.5+ |
// to double d from char c
// no conversion necessary in Java 1.5+ |
// to String g from char c
g = String.valueOf(c); |
// to Boolean tt from char c
// for '0' or '1' tt = (c != '0') ? Boolean.TRUE : Boolean.FALSE; // or for Unicode 0 or 1 tt = (c != 0) ? Boolean.TRUE : Boolean.FALSE; |
// to /*signed*/ Byte bb from char c
bb = new Byte((byte)c); |
// to /*unsigned*/ Byte uu from char c
uu = new Byte((byte)c); |
// to Short ss from char c
// no conversion necessary in Java 1.5+ |
// to Character cc from char c
// no conversion necessary in Java 1.5+ |
// to Integer ii from char c
// no conversion necessary in Java 1.5+ |
// to Long nn from char c
// no conversion necessary in Java 1.5+ |
// to Float ff from char c
// no conversion necessary in Java 1.5+ |
// to Double dd from char c
// no conversion necessary in Java 1.5+ |
char c |
| int i | // to boolean t from int i
t = i != 0; |
// to /*signed*/ byte b from int i
b = (byte)i; |
// to /*unsigned*/ byte u from int i
u = (byte)i; |
// to short s from int i
s = (short)i; |
// to char c from int i
// 9 -> '9' c = (char)(i + '0'); // or to get Unicode value c = (char)i; |
-------- | // to long n from int i
// no conversion necessary in Java 1.5+ |
// to float f from int i
// no conversion necessary in Java 1.5+ |
// to double d from int i
// no conversion necessary in Java 1.5+ |
// to String g from int i
// best for readability g = Integer.toString(i); // best for maintainability g = String.valueOf(i); // or g = Integer.toString(i, 7 /* radix */); // or g = Integer.toBinaryString(i); // or g = Integer.toOctalString(i); // or g = Integer.toHexString(i); // or kludgy and possibly slow g = "" + i; |
// to Boolean tt from int i
tt = (i != 0) ? Boolean.TRUE : Boolean.FALSE; |
// to /*signed*/ Byte bb from int i
bb = new Byte((byte)i); |
// to /*unsigned*/ Byte uu from int i
uu = new Byte((byte)i); |
// to Short ss from int i
ss = new Short((short)i); |
// to Character cc from int i
// 9 -> '9' cc = new Character((char)(i + '0')); // or to get Unicode value cc = new Character((char)i); |
// to Integer ii from int i
// no conversion necessary in Java 1.5+ |
// to Long nn from int i
// no conversion necessary in Java 1.5+ |
// to Float ff from int i
// no conversion necessary in Java 1.5+ |
// to Double dd from int i
// no conversion necessary in Java 1.5+ |
int i |
| long n | // to boolean t from long n
t = n != 0; |
// to /*signed*/ byte b from long n
b = (byte)n; |
// to /*unsigned*/ byte u from long n
u = (byte)n; |
// to short s from long n
s = (short)n; |
// to char c from long n
// 9 -> '9' c = (char)(n + '0'); // or to get Unicode value c = (char)n; |
// to int i from long n
i = (int)n; |
-------- | // to float f from long n
// no conversion necessary in Java 1.5+ |
// to double d from long n
// no conversion necessary in Java 1.5+ |
// to String g from long n
// best for readability g = Long.toString(n); // best for maintainability g = String.valueOf(n); // or g = Long.toString(n, 7 /* radix */); // or g = Long.toBinaryString(n); // or g = Long.toOctalString(n); // or g = Long.toHexString(n); // or kludgy and possibly slow g = "" + n; |
// to Boolean tt from long n
tt = (n != 0) ? Boolean.TRUE : Boolean.FALSE; |
// to /*signed*/ Byte bb from long n
bb = new Byte((byte)n); |
// to /*unsigned*/ Byte uu from long n
uu = new Byte((byte)n); |
// to Short ss from long n
ss = new Short((short)n); |
// to Character cc from long n
// 9 -> '9' cc = new Character((char)(n + '0')); // or to get Unicode value cc = new Character((char)n); |
// to Integer ii from long n
// best JDK 1.5+ ii = Integer.valueOf((int)n); // or JDK 1.4- ii = new Integer((int)n); |
// to Long nn from long n
// no conversion necessary in Java 1.5+ |
// to Float ff from long n
// no conversion necessary in Java 1.5+ |
// to Double dd from long n
// no conversion necessary in Java 1.5+ |
long n |
| float f | // to boolean t from float f
t = f != 0; |
// to /*signed*/ byte b from float f
b = (byte)f; |
// to /*unsigned*/ byte u from float f
u = (byte)f; |
// to short s from float f
s = (short)f; |
// to char c from float f
c = (char)f; |
// to int i from float f
// best i = (int)f; // or i = Math.round(f); // or i = (int)Math.ceil(f); // or i = (int)Math.floor(f); // to see the IEEE bits inside a float i = Float.floatToIntBits(f); |
// to long n from float f
// best n = (long)f; // or n = Math.round(f); // or n = (long)Math.ceil(f); // or n = (long)Math.floor(f); |
-------- | // to double d from float f
// no conversion necessary in Java 1.5+ |
// to String g from float f
// 2 decimal places, rounded, locale-sensitive. java.text.DecimalFormat df2 = new java.text.DecimalFormat("###,##0.00"); g = df2.format(f); // or exponential scientific format, locale-sensitive. java.text.DecimalFormat de = new java.text.DecimalFormat("0.000000E00"); g = de.format(f); // or best for readability, no loss of precision, locale-insensitive g = Float.toString(f); // or best for maintainability, no loss of precision, locale-insensitive g = String.valueOf(f); |
// to Boolean tt from float f
tt = (f != 0) ? Boolean.TRUE : Boolean.FALSE; |
// to /*signed*/ Byte bb from float f
bb = new Byte((byte)f); |
// to /*unsigned*/ Byte uu from float f
uu = new Byte((byte)f); |
// to Short ss from float f
ss = new Short((short)f); |
// to Character cc from float f
cc = new Character((char)f); |
// to Integer ii from float f
// best JDK 1.5+ ii = Integer.valueOf((int)f); // or JDK 1.4- ii = new Integer((int)f); |
// to Long nn from float f
// best JDK 1.5+ nn = Long.valueOf((long)f); // or JDK 1.4- nn = new Long((long)f); |
// to Float ff from float f
// no conversion necessary in Java 1.5+ |
// to Double dd from float f
// no conversion necessary in Java 1.5+ |
float f |
| double d | // to boolean t from double d
t = d != 0; |
// to /*signed*/ byte b from double d
b = (byte)d; |
// to /*unsigned*/ byte u from double d
u = (byte)d; |
// to short s from double d
s = (short)d; |
// to char c from double d
c = (char)d; |
// to int i from double d
// best i = (int)d; // or i = (int)Math.round(d); // or i = (int)Math.ceil(d); // or i = (int)Math.floor(d); |
// to long n from double d
// best n = (long)d; // or n = Math.round(d); // or n = (long)Math.ceil(d); // or n = (long)Math.floor(d); // to see the IEEE bits inside a double n = Double.doubleToLongBits(d); |
// to float f from double d
f = (float)d; |
-------- | // to String g from double d
// 2 decimal places, rounded, locale-sensitive. java.text.DecimalFormat df2 = new java.text.DecimalFormat("###,##0.00"); g = df2.format(d); // or exponential scientific format, locale-sensitive. java.text.DecimalFormat de = new java.text.DecimalFormat("0.0000000000E00"); g = de.format(d); // or best for readability, no loss of precision, locale-insensitive g = Double.toString(d); // or best for maintainability, no loss of precision, locale-insensitive g = String.valueOf(d); |
// to Boolean tt from double d
tt = (d != 0) ? Boolean.TRUE : Boolean.FALSE; |
// to /*signed*/ Byte bb from double d
bb = new Byte((byte)d); |
// to /*unsigned*/ Byte uu from double d
uu = new Byte((byte)d); |
// to Short ss from double d
ss = new Short((short)d); |
// to Character cc from double d
cc = new Character((char)d); |
// to Integer ii from double d
// best JDK 1.5+ ii = Integer.valueOf((int)d); // or JDK 1.4- ii = new Integer((int)d); |
// to Long nn from double d
// best JDK 1.5+ nn = Long.valueOf((long)d); // or JDK 1.4- nn = new Long((long)d); |
// to Float ff from double d
ff = new Float(d); |
// to Double dd from double d
// no conversion necessary in Java 1.5+ |
double d |
| String g | // to boolean t from String g
t = new Boolean(g.trim()).booleanValue(); // or t = g.trim().equalsIgnoreCase("true"); |
// to /*signed*/ byte b from String g
try { // best b = (byte)Integer.parseInt(g.trim()); // or b = (byte)Integer.parseInt(g.trim(), 16 /* radix */); } catch (NumberFormatException e){/* ... */} |
// to /*unsigned*/ byte u from String g
try { // best u = (byte)Integer.parseInt(g.trim()); // or u = (byte)Integer.parseInt(g.trim(), 16 /* radix */); } catch (NumberFormatException e){/* ... */} |
// to short s from String g
try { // best s = (short)Integer.parseInt(g.trim()); // or s = (short)Integer.parseInt(g.trim(), 16 /* radix */); } catch (NumberFormatException e){/* ... */} |
// to char c from String g
try { // "9" -> '9' c = g.charAt(0 /* position */); // or to get Unicode value c = (char)Integer.parseInt(g.trim()); // or to get Unicode hex value c = (char)Integer.parseInt(g.trim(), 16 /* radix */); } catch (NumberFormatException e){/* ... */} |
// to int i from String g
try { // best i = Integer.parseInt(g.trim()); // or i = Integer.parseInt(g.trim(), 16 /* radix */); } catch (NumberFormatException e){/* ... */} |
// to long n from String g
try { n = Long.parseLong(g.trim()); } catch (NumberFormatException e){/* ... */} |
// to float f from String g
try { // best locale-insensitive f = Float.parseFloat(g.trim()); // or locale-insensitive f = Float.valueOf(g.trim()).floatValue(); } catch (NumberFormatException e){/* ... */} |
// to double d from String g
try { // best, locale-insensitive d = Double.parseDouble(g.trim()); // or locale-insensitive d = Double.valueOf(g.trim()).doubleValue(); } catch (NumberFormatException e){/* ... */} |
-------- | // to Boolean tt from String g
// best tt = new Boolean(g.trim()); // or tt = Boolean.valueOf(g.trim()); |
// to /*signed*/ Byte bb from String g
try { // best bb = new Byte(Byte.parseByte(g.trim())); // or bb = new Byte(Byte.parseByte(g.trim(), 16 /* radix */)); // or bb = new Byte((byte)g.charAt(0 /* position */)); } catch (NumberFormatException e){/* ... */} |
// to /*unsigned*/ Byte uu from String g
try { // best uu = new Byte(Byte.parseByte(g.trim())); // or uu = new Byte(Byte.parseByte(g.trim(), 16 /* radix */)); // or uu = new Byte((byte)g.charAt(0 /* position */)); } catch (NumberFormatException e){/* ... */} |
// to Short ss from String g
try { // best ss = new Short(Short.parseShort(g.trim())); // or ss = new Short(Short.parseShort(g.trim(), 16 /* radix */)); // or ss = new Short((short)g.charAt(0 /* position */)); } catch (NumberFormatException e){/* ... */} |
// to Character cc from String g
try { // "9" -> '9' cc = new Character(g.charAt(0 /* position */)); // or to get Unicode value cc = new Character((char)Integer.parseInt(g.trim())); // or to get Unicode hex value cc = new Character((char)Integer.parseInt(g.trim(), 16 /* radix */)); } catch (NumberFormatException e){/* ... */} |
// to Integer ii from String g
try { // best, caches ii = Integer.valueOf(g.trim()); // or ii = new Integer(g.trim()); } catch (NumberFormatException e){/* ... */} |
// to Long nn from String g
try { // best, caches nn = Long.valueOf(g.trim()); // or nn = new Long(g.trim()); } catch (NumberFormatException e){/* ... */} |
// to Float ff from String g
try { // best, locale-insensitive. ff = new Float(g.trim()); // or locale-insensitive ff = Float.valueOf(g.trim()); } catch (NumberFormatException e){/* ... */} |
// to Double dd from String g
try { // best, locale-insensitive*/ dd = new Double(g); // or locale-insensitive dd = Double.valueOf(g); } catch (NumberFormatException e){/* ... */} |
String g |
| Boolean tt | // to boolean t from Boolean tt
// no conversion necessary in Java 1.5+ |
// to /*signed*/ byte b from Boolean tt
b = (byte)(tt.booleanValue()?1:0); |
// to /*unsigned*/ byte u from Boolean tt
u = (byte)(tt.booleanValue()?1:0); |
// to short s from Boolean tt
s = (short)(tt.booleanValue()?1:0); |
// to char c from Boolean tt
// to get '0' and '1' c = (char)(tt.booleanValue()?'1':'0'); // or to get Unicode 0 and 1 c = (char)(tt.booleanValue()?0:1); |
// to int i from Boolean tt
i = tt.booleanValue()?1:0; |
// to long n from Boolean tt
n = tt.booleanValue()?1:0; |
// to float f from Boolean tt
f = tt.booleanValue()?1:0; |
// to double d from Boolean tt
d = tt.booleanValue()?1:0; |
// to String g from Boolean tt
g = tt.toString(); |
-------- | // to /*signed*/ Byte bb from Boolean tt
bb = new Byte((byte)(tt.booleanValue()?1:0)); |
// to /*unsigned*/ Byte uu from Boolean tt
uu = new Byte((byte)(tt.booleanValue()?1:0)); |
// to Short ss from Boolean tt
ss = new Short((short)(tt.booleanValue()?1:0)); |
// to Character cc from Boolean tt
// to get '0' and '1' cc = new Character((char)(tt.booleanValue()?'1':'0')); // or to get Unicode 0 or 1 cc = new Character((char)(tt.booleanValue()?1:0)); |
// to Integer ii from Boolean tt
// best JDK 1.5+ ii = Integer.valueOf(tt.booleanValue()?1:0); // or JDK 1.4- ii = new Integer(tt.booleanValue()?1:0); |
// to Long nn from Boolean tt
// best JDK 1.5+ nn = Long.valueOf(tt.booleanValue()?1:0); // or JDK 1.4- nn = new Long(tt.booleanValue()?1:0); |
// to Float ff from Boolean tt
ff = new Float(tt.booleanValue()?1:0); |
// to Double dd from Boolean tt
dd = new Double(tt.booleanValue()?1:0); |
Boolean tt |
| /*signed*/ Byte bb | // to boolean t from /*signed*/ Byte bb
t = bb.byteValue() != 0; |
// to /*signed*/ byte b from /*signed*/ Byte bb
// no conversion necessary in Java 1.5+ |
// to /*unsigned*/ byte u from /*signed*/ Byte bb
// no conversion necessary in Java 1.5+ |
// to short s from /*signed*/ Byte bb
// no conversion necessary in Java 1.5+ |
// to char c from /*signed*/ Byte bb
// no conversion necessary in Java 1.5+ |
// to int i from /*signed*/ Byte bb
// no conversion necessary in Java 1.5+ |
// to long n from /*signed*/ Byte bb
// no conversion necessary in Java 1.5+ |
// to float f from /*signed*/ Byte bb
// no conversion necessary in Java 1.5+ |
// to double d from /*signed*/ Byte bb
// no conversion necessary in Java 1.5+ |
// to String g from /*signed*/ Byte bb
g = bb.toString(); |
// to Boolean tt from /*signed*/ Byte bb
// best tt = (bb.byteValue() != '0') ? Boolean.TRUE : Boolean.FALSE; // or tt = (bb.byteValue() != 0) ? Boolean.TRUE : Boolean.FALSE; |
-------- | // to /*unsigned*/ Byte uu from /*signed*/ Byte bb
// no conversion necessary in Java 1.5+ |
// to Short ss from /*signed*/ Byte bb
// no conversion necessary in Java 1.5+ |
// to Character cc from /*signed*/ Byte bb
// no conversion necessary in Java 1.5+ |
// to Integer ii from /*signed*/ Byte bb
// no conversion necessary in Java 1.5+ |
// to Long nn from /*signed*/ Byte bb
// no conversion necessary in Java 1.5+ |
// to Float ff from /*signed*/ Byte bb
// no conversion necessary in Java 1.5+ |
// to Double dd from /*signed*/ Byte bb
// no conversion necessary in Java 1.5+ |
/*signed*/ Byte bb |
| /*unsigned*/ Byte uu | // to boolean t from /*unsigned*/ Byte uu
t = uu.byteValue() != 0; |
// to /*signed*/ byte b from /*unsigned*/ Byte uu
// no conversion necessary in Java 1.5+ |
// to /*unsigned*/ byte u from /*unsigned*/ Byte uu
// no conversion necessary in Java 1.5+ |
// to short s from /*unsigned*/ Byte uu
// no conversion necessary in Java 1.5+ |
// to char c from /*unsigned*/ Byte uu
// no conversion necessary in Java 1.5+ |
// to int i from /*unsigned*/ Byte uu
// no conversion necessary in Java 1.5+ |
// to long n from /*unsigned*/ Byte uu
// no conversion necessary in Java 1.5+ |
// to float f from /*unsigned*/ Byte uu
// no conversion necessary in Java 1.5+ |
// to double d from /*unsigned*/ Byte uu
// no conversion necessary in Java 1.5+ |
// to String g from /*unsigned*/ Byte uu
g = uu.toString(); |
// to Boolean tt from /*unsigned*/ Byte uu
// best tt = (uu.byteValue() != '0') ? Boolean.TRUE : Boolean.FALSE; // or tt = (uu.byteValue() != 0) ? Boolean.TRUE : Boolean.FALSE; |
// to /*signed*/ Byte bb from /*unsigned*/ Byte uu
// no conversion necessary in Java 1.5+ |
-------- | // to Short ss from /*unsigned*/ Byte uu
// no conversion necessary in Java 1.5+ |
// to Character cc from /*unsigned*/ Byte uu
// no conversion necessary in Java 1.5+ |
// to Integer ii from /*unsigned*/ Byte uu
// no conversion necessary in Java 1.5+ |
// to Long nn from /*unsigned*/ Byte uu
// no conversion necessary in Java 1.5+ |
// to Float ff from /*unsigned*/ Byte uu
// no conversion necessary in Java 1.5+ |
// to Double dd from /*unsigned*/ Byte uu
// no conversion necessary in Java 1.5+ |
/*unsigned*/ Byte uu |
| Short ss | // to boolean t from Short ss
t = ss.shortValue() != 0; |
// to /*signed*/ byte b from Short ss
b = (byte)ss.shortValue(); |
// to /*unsigned*/ byte u from Short ss
u = (byte)ss.shortValue(); |
// to short s from Short ss
// no conversion necessary in Java 1.5+ |
// to char c from Short ss
// no conversion necessary in Java 1.5+ |
// to int i from Short ss
// no conversion necessary in Java 1.5+ |
// to long n from Short ss
// no conversion necessary in Java 1.5+ |
// to float f from Short ss
// no conversion necessary in Java 1.5+ |
// to double d from Short ss
// no conversion necessary in Java 1.5+ |
// to String g from Short ss
g = ss.toString(); |
// to Boolean tt from Short ss
// best tt = (ss.shortValue() != '0') ? Boolean.TRUE : Boolean.FALSE; // or tt = (ss.shortValue() != 0) ? Boolean.TRUE : Boolean.FALSE; |
// to /*signed*/ Byte bb from Short ss
bb = new Byte(ss.byteValue()); |
// to /*unsigned*/ Byte uu from Short ss
uu = new Byte(ss.byteValue()); |
-------- | // to Character cc from Short ss
// no conversion necessary in Java 1.5+ |
// to Integer ii from Short ss
// no conversion necessary in Java 1.5+ |
// to Long nn from Short ss
// no conversion necessary in Java 1.5+ |
// to Float ff from Short ss
// no conversion necessary in Java 1.5+ |
// to Double dd from Short ss
// no conversion necessary in Java 1.5+ |
Short ss |
| Character cc | // to boolean t from Character cc
// to convert '0' or '1' t = cc.charValue() != '0'; // to convert Unicode 0 or 1 t = cc.charValue() != 0; |
// to /*signed*/ byte b from Character cc
// international b = (byte)Character.digit(cc.charValue(), 10 /* radix */); // fastest '9' -> 9 b = (byte)(cc.charValue() - '0'); // or to get Unicode value b = (byte)cc.charValue(); |
// to /*unsigned*/ byte u from Character cc
// international u = (byte)Character.digit(cc.charValue(), 10 /* radix */); // fastest '9' -> 9 u = (byte)(cc.charValue() - '0'); // or to get Unicode value u = (byte)cc.charValue(); |
// to short s from Character cc
// no conversion necessary in Java 1.5+ |
// to char c from Character cc
// no conversion necessary in Java 1.5+ |
// to int i from Character cc
// no conversion necessary in Java 1.5+ |
// to long n from Character cc
// no conversion necessary in Java 1.5+ |
// to float f from Character cc
// no conversion necessary in Java 1.5+ |
// to double d from Character cc
// no conversion necessary in Java 1.5+ |
// to String g from Character cc
g = cc.toString(); |
// to Boolean tt from Character cc
// for '0' or '1' tt = (cc.charValue() != '0') ? Boolean.TRUE : Boolean.FALSE; // or for Unicode 0 or 1 tt = (cc.charValue() != 0) ? Boolean.TRUE : Boolean.FALSE; |
// to /*signed*/ Byte bb from Character cc
// international bb = new Byte((byte)Character.digit(cc.charValue(), 10 /* radix */)); // fastest '9' -> 9 bb = new Byte((byte)(cc.charValue() - '0')); // or to get Unicode value bb = new Byte((byte)cc.charValue()); |
// to /*unsigned*/ Byte uu from Character cc
// international uu = new Byte((byte)Character.digit(cc.charValue(), 10 /* radix */)); // fastest '9' -> 9 uu = new Byte((byte)(cc.charValue() - '0')); // or to get Unicode value uu = new Byte((byte)cc.charValue()); |
// to Short ss from Character cc
// no conversion necessary in Java 1.5+ |
-------- | // to Integer ii from Character cc
// no conversion necessary in Java 1.5+ |
// to Long nn from Character cc
// no conversion necessary in Java 1.5+ |
// to Float ff from Character cc
// no conversion necessary in Java 1.5+ |
// to Double dd from Character cc
// no conversion necessary in Java 1.5+ |
Character cc |
| Integer ii | // to boolean t from Integer ii
t = ii.intValue() != 0; |
// to /*signed*/ byte b from Integer ii
b = (byte)ii.intValue(); |
// to /*unsigned*/ byte u from Integer ii
u = (byte)ii.intValue(); |
// to short s from Integer ii
s = (short)ii.intValue(); |
// to char c from Integer ii
c = (char)ii.intValue(); |
// to int i from Integer ii
// no conversion necessary in Java 1.5+ |
// to long n from Integer ii
// no conversion necessary in Java 1.5+ |
// to float f from Integer ii
// no conversion necessary in Java 1.5+ |
// to double d from Integer ii
// no conversion necessary in Java 1.5+ |
// to String g from Integer ii
g = ii.toString(); |
// to Boolean tt from Integer ii
tt = (ii.intValue() != 0) ? Boolean.TRUE : Boolean.FALSE; |
// to /*signed*/ Byte bb from Integer ii
bb = new Byte(ii.byteValue()); |
// to /*unsigned*/ Byte uu from Integer ii
uu = new Byte(ii.byteValue()); |
// to Short ss from Integer ii
ss = new Short(ii.shortValue()); |
// to Character cc from Integer ii
// 9 -> '9' cc = new Character((char)(ii.intValue() + '0')); // or to get Unicode value cc = new Character((char)ii.intValue()); |
-------- | // to Long nn from Integer ii
// no conversion necessary in Java 1.5+ |
// to Float ff from Integer ii
// no conversion necessary in Java 1.5+ |
// to Double dd from Integer ii
// no conversion necessary in Java 1.5+ |
Integer ii |
| Long nn | // to boolean t from Long nn
t = nn.longValue() != 0; |
// to /*signed*/ byte b from Long nn
b = (byte)nn.intValue(); |
// to /*unsigned*/ byte u from Long nn
u = (byte)nn.intValue(); |
// to short s from Long nn
s = (short)nn.intValue(); |
// to char c from Long nn
// 9 -> '9' c = (char)(nn.intValue() + '0'); // or to get Unicode value c = (char)nn.intValue(); |
// to int i from Long nn
i = nn.intValue(); |
// to long n from Long nn
// no conversion necessary in Java 1.5+ |
// to float f from Long nn
// no conversion necessary in Java 1.5+ |
// to double d from Long nn
// no conversion necessary in Java 1.5+ |
// to String g from Long nn
g = nn.toString(); |
// to Boolean tt from Long nn
tt = (nn.longValue() != 0) ? Boolean.TRUE : Boolean.FALSE; |
// to /*signed*/ Byte bb from Long nn
bb = new Byte(nn.byteValue()); |
// to /*unsigned*/ Byte uu from Long nn
uu = new Byte(nn.byteValue()); |
// to Short ss from Long nn
ss = new Short(nn.shortValue()); |
// to Character cc from Long nn
// 9 -> '9' cc = new Character((char)(nn.intValue() + '0')); // or to get Unicode value cc = new Character((char)nn.intValue()); |
// to Integer ii from Long nn
// best JDK 1.5+ ii = Integer.valueOf(nn.intValue()); // or JDK 1.4- ii = new Integer(nn.intValue()); |
-------- | // to Float ff from Long nn
// no conversion necessary in Java 1.5+ |
// to Double dd from Long nn
// no conversion necessary in Java 1.5+ |
Long nn |
| Float ff | // to boolean t from Float ff
t = ff.floatValue() != 0; |
// to /*signed*/ byte b from Float ff
b = (byte)ff.intValue(); |
// to /*unsigned*/ byte u from Float ff
u = (byte)ff.intValue(); |
// to short s from Float ff
s = (short)ff.intValue(); |
// to char c from Float ff
c = (char)ff.intValue(); |
// to int i from Float ff
i = ff.intValue(); |
// to long n from Float ff
n = ff.longValue(); |
// to float f from Float ff
// no conversion necessary in Java 1.5+ |
// to double d from Float ff
// no conversion necessary in Java 1.5+ |
// to String g from Float ff
// 2 decimal places, rounded, locale-sensitive java.text.DecimalFormat df2 = new java.text.DecimalFormat("###,##0.00"); g = df2.format(ff.floatValue()); // or exponential scientific format, locale-sensitive. java.text.DecimalFormat de = new java.text.DecimalFormat("0.000000E00"); g = de.format(ff.floatValue()); // or best for readability and maintainability, locale-insensitive. g = ff.toString(); |
// to Boolean tt from Float ff
tt = (ff.floatValue() != 0) ? Boolean.TRUE : Boolean.FALSE; |
// to /*signed*/ Byte bb from Float ff
bb = new Byte(ff.byteValue()); |
// to /*unsigned*/ Byte uu from Float ff
uu = new Byte(ff.byteValue()); |
// to Short ss from Float ff
ss = new Short(ff.shortValue()); |
// to Character cc from Float ff
cc = new Character((char)ff.intValue()); |
// to Integer ii from Float ff
// best JDK 1.5+ ii = Integer.valueOf(ff.intValue()); // or JDK 1.4- ii = new Integer(ff.intValue()); |
// to Long nn from Float ff
// best JDK 1.5+ nn = Long.valueOf(ff.longValue()); // or JDK 1.4- nn = new Long(ff.longValue()); |
-------- | // to Double dd from Float ff
// no conversion necessary in Java 1.5+ |
Float ff |
| Double dd | // to boolean t from Double dd
t = dd.doubleValue() != 0; |
// to /*signed*/ byte b from Double dd
b = (byte)dd.intValue(); |
// to /*unsigned*/ byte u from Double dd
u = (byte)dd.intValue(); |
// to short s from Double dd
s = (short)dd.intValue(); |
// to char c from Double dd
c = (char)dd.intValue(); |
// to int i from Double dd
i = dd.intValue(); |
// to long n from Double dd
n = dd.longValue(); |
// to float f from Double dd
f = dd.floatValue(); |
// to double d from Double dd
// no conversion necessary in Java 1.5+ |
// to String g from Double dd
// 2 decimal places, rounded, locale-sensitive java.text.DecimalFormat df2 = new java.text.DecimalFormat("###,##0.00"); g = df2.format(dd.doubleValue()); // or exponential scientific format, locale sensitive. java.text.DecimalFormat de = new java.text.DecimalFormat("0.0000000000E00"); g = de.format(dd.doubleValue()); // or best for readability and maintainability, locale-insensitive g = dd.toString(); |
// to Boolean tt from Double dd
tt = (dd.doubleValue() != 0) ? Boolean.TRUE : Boolean.FALSE; |
// to /*signed*/ Byte bb from Double dd
bb = new Byte(dd.byteValue()); |
// to /*unsigned*/ Byte uu from Double dd
uu = new Byte(dd.byteValue()); |
// to Short ss from Double dd
ss = new Short(dd.shortValue()); |
// to Character cc from Double dd
cc = new Character((char)dd.intValue()); |
// to Integer ii from Double dd
// best JDK 1.5+ ii = Integer.valueOf(dd.intValue()); // or JDK 1.4- ii = new Integer(dd.intValue()); |
// to Long nn from Double dd
// best JDK 1.5+ nn = Long.valueOf(dd.longValue()); // or JDK 1.4- nn = new Long(dd.longValue()); |
// to Float ff from Double dd
ff = new Float(dd.floatValue()); |
-------- | Double dd |
| to: boolean t | to: /*signed*/ byte b | to: /*unsigned*/ byte u | to: short s | to: char c | to: int i | to: long n | to: float f | to: double d | to: String g | to: Boolean tt | to: /*signed*/ Byte bb | to: /*unsigned*/ Byte uu | to: Short ss | to: Character cc | to: Integer ii | to: Long nn | to: Float ff | to: Double dd | ||
| Java Primitives | |||||||
|---|---|---|---|---|---|---|---|
| Type | Signed? | Bits | Bytes | Digits | Lowest | Highest | Mnemonic |
| boolean | n/a | 1 | 1 | 1 | false | true | zero/one |
| char | unsigned Unicode | 16 | 2 | 4:5 | '\u0000' [0] aka Character.MIN_VALUE | '\uffff' [216-1] aka Character.MAX_VALUE | Unicode chars are twice as big as C’s. |
| byte | signed | 8 | 1 | 2:3 | -128 [-27] aka Byte.MIN_VALUE | +127 [27-1]aka Byte.MAX_VALUE | Bytes are signed, so half the usual 255 range. |
| short | signed | 16 | 2 | 4:5 | -32,768 [-215] aka Short.MIN_VALUE | +32,767 [215-1] aka Short.MAX_VALUE | 32K |
| int | signed | 32 | 4 | 9:10 | -2,147,483,648 [-231] aka Integer.MIN_VALUE | +2,147,483,647 [231-1] aka Integer.MAX_VALUE | 2 gig |
| long | signed | 64 | 8 | 18:19 | -9,223,372,036,854,775,808 [-263] aka Long. MIN_VALUE | +9,223,372,036,854,775,807 [263-1] aka Long. MAX_VALUE | 9 exabytes, or 9 billion gig |
| float | signed exponent and mantissa | 32 | 4 | 6:7 | ±1.40129846432481707e-45 aka Float.MIN_VALUE | ±3.40282346638528860e+38 aka Float.MAX_VALUE
or roughly ±2127 with 6 to 7 significant digits of accuracy. A float can exactly represent integers in the range -224 to +224. |
rough, compact float |
| double | signed exponent and mantissa | 64 | 8 | 14:15 | ±4.94065645841246544e-324 aka Double.MIN_VALUE | ±1.79769313486231570e+308 aka Double.MAX_VALUE
or roughly ±21023 with 14 to 15 significant digits of accuracy. A double can exactly represent integers in the range -253 to +253. |
high precision float |
| Mutable Primitives | Immutable Objects |
|---|---|
| boolean | Boolean |
| ordinary signed byte | Byte |
| unsigned byte | Byte |
| short | Short |
| char | Character |
| int | Integer |
| long | Long |
| float | Float |
| double | Double |
| char[] | String |
String s = "abc"; // string -> byte[] byte [] b = s.getBytes( "8859_1" /* encoding */ ); // byte[] -> String String t = new String( b , "Cp1252" /* encoding */ );
String s = "abc" ; // string -> char[] char[] ca = s.toCharArray(); // char[] -> String String s = new String( ca );
Beware of char[].toString(). It does not convert the character array to String. It prints out the address of the array — not useful for anything. Use new String ( chararray ) instead.
// Rounding to an integer: long n = Math.round(d); double d = Math.rint(d); // Rounding to two decimal places: long n = Math.round(d *100.); /* keep as "pennies" */ double d = Math.rint (d *100.)/100.;
floating point for more details.
public static String toLZ( int i, int len ) { // converts integer to left-zero padded string, len chars long. String s = Integer.toString(i); if ( s.length() > len ) return s.substring(0,len); else if ( s.length() < len ) // pad on left with zeros return "000000000000000000000000000".substring(0, len - s.length ()) + s; else return s; } // end toLZ
JDK 1.1+ has formatting picture classes such as java.util.DateFormat, SimpleDateFormat and DecimalFormat.
Formatting is such a common request, you might attain sainthood if you wrote a formatting class that gives you all the power of printf without the overhead of parsing strings for % produce a string. e.g.
Java 1.5+, java.io. PrintWriter. printf, java.io. PrintStream. printf and the java.util. Formatter class give you abilities similar to C’s printf. See printf for more details.
You have to roll your own method. You may be able to use my CSVReader class. Or use java.io.StreamTokenizer or java.util.StringTokenizer, perhaps in combination with readLine to get your data into strings. StreamTokenizer has bells and whistles to deal with parsing source code, including white space, comments and numbers. StringTokenizer just splits the text up based on delimiter characters. Then use the conversion methods in the table above to convert to integers etc. See below for a simplified examples of how you would do this.
![]() |
and suggestions to improve this page to Roedy Green : | ||
| Canadian Mind Products | |||
| mindprod.com IP:[65.110.21.43] | |||
| Your face IP:[38.103.63.62] | The information on this page is for non-military use only. | ||
| You are visitor number 32,917. | Military use includes use by defence contractors. | ||
| You can get a fresh copy of this page from: | or possibly from your local J: drive (Java virtual drive/mindprod.com website mirror) | ||
| http://mindprod.com/jgloss/convert15.html | J:\mindprod\jgloss\convert15.html | ||