Conversion 1.5+ : Java Glossary

*0-9ABCDEFGHIJKLMNOPQRSTUVWXYZ (all)

Conversion 1.5+
This table will help you convert from any of the basic data types to any other in Java 1.5 or later. This table is also available in a Java version 1.4 or earlier version. Please report any errors or omissions. This table is also available as an amanuensis Applet or as a download with source code.
Conversion Amanuensis Rounding
Conversion Matrix for Java 1.4- Left Zeros
Conversion Matrix for Java 1.5+ Formatted Output
Primitives Reading Numeric Data
Basic Types StreamTokenizer
Truncation StringTokenizer
byte[] ⇔ String Binary
char[] ⇔ String Metric
Hexadecimal Links
StringBuffers & StringBuilders
JDK 1.5+ Convert To
From to: boolean t to: 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 byte b from boolean t
b = t ? (byte)1 : (byte)0;
// to /*unsigned*/ byte u from boolean t
u = t ? (byte)1 : (byte)0;
// to short s from boolean t
s = t ? (short)1 : (short)0;
// to char c from boolean t
// to get '0' and '1'
c = t ? '1' : '0';
// or to get Unicode value 0 or 1
c = t ? (char)1 : (char)0;
// to int i from boolean t
i = t ? 1 : 0;
// to long n from boolean t
n = t ? 1L : 0L;
// to float f from boolean t
f = t ? 1.0f : 0.0f;
// to double d from boolean t
d = t ? 1.0d : 0.0d;
// to String g from boolean t
g = String.valueOf(t);
// to Boolean tt from boolean t
tt = t ? Boolean.TRUE : Boolean.FALSE;
// to /*signed*/ Byte bb from boolean t
bb = t ? (byte)1 : (byte)0;
// to /*unsigned*/ Byte uu from boolean t
uu = t ? (byte)1 : (byte)0;
// to Short ss from boolean t
ss = t ? (short)1 : (short)0;
// to Character cc from boolean t
// to get '0' or '1'
cc = t ? (char)1 : (char)0;
// or to get Unicode 0 or 1
cc = t ? (char)1 : (char)0;
// to Integer ii from boolean t
ii = t ? 1 : 0;
// to Long nn from boolean t
nn = t ? 1L : 0L;
// to Float ff from boolean t
ff = t ? 1.0f : 0.0f;
// to Double dd from boolean t
dd = t ? 1.0d : 0.0d;
boolean t
byte b // to boolean t from byte b
t = b!=0;
-------- // to /*unsigned*/ byte u from byte b
u = b;
// to short s from byte b
s = b;
// to char c from byte b
// 9 -> '9'
c = (char)(b + '0');
// or to get Unicode value
c = (char)b;
// to int i from byte b
i = b; // sign extends.
// to long n from byte b
n = b; // sign extends.
// to float f from byte b
f = b;
// to double d from byte b
d = b;
// to String g from 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 byte b
tt = (b!=0) ? Boolean.TRUE : Boolean.FALSE;
// to /*signed*/ Byte bb from byte b
bb = b;
// to /*unsigned*/ Byte uu from byte b
uu = b;
// to Short ss from byte b
ss = (short)b; // sign extends
// to Character cc from byte b
// 9 -> '9'
cc = (char)(b + '0');
// or to get Unicode value
cc = (char)b;
// to Integer ii from byte b
ii = (int)b;
// to Long nn from byte b
nn = (long)b;
// to Float ff from byte b
ff = (float)b; // sign extends.
// to Double dd from byte b
dd = (double)b; // sign extends.
byte b
/*unsigned*/ byte u // to boolean t from /*unsigned*/ byte u
t = u!=0;
// to byte b from /*unsigned*/ byte u
b = u;
-------- // to short s from /*unsigned*/ byte u
s = (short)(u & 0xff);
// to char c from /*unsigned*/ byte u
// 9 -> '9'
c = (char)((u & 0xff) + '0');
// or to get Unicode value
c = (char)(u & 0xff);
// to int i from /*unsigned*/ byte u
i = u & 0xff; // does not sign extend
// to long n from /*unsigned*/ byte u
n = u & 0xff; // does not sign extend.
// to float f from /*unsigned*/ byte u
f = u & 0xff; // does not sign extend.
// to double d from /*unsigned*/ byte u
d = u & 0xff; // does not sign extend.
// 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
bb = u;
// to /*unsigned*/ Byte uu from /*unsigned*/ byte u
uu = u;
// to Short ss from /*unsigned*/ byte u
ss = (short)(u & 0xff); // does not sign extend.
// to Character cc from /*unsigned*/ byte u
// 9 -> '9'
cc = (char)((u & 0xff) + '0');
// or to get Unicode value
cc = (char)(u & 0xff);
// to Integer ii from /*unsigned*/ byte u
ii = u & 0xff;
// to Long nn from /*unsigned*/ byte u
nn =(long)(u & 0xff);
// to Float ff from /*unsigned*/ byte u
ff = (float)(u & 0xff); // does not sign extend.
// to Double dd from /*unsigned*/ byte u
dd = (double)(u & 0xff); // does not sign extend.
/*unsigned*/ byte u
short s // to boolean t from short s
t = s!=0;
// to byte b from short s
b = (byte)s;
// to /*unsigned*/ byte u from short s
u = (byte)s;
-------- // to char c from short s
// 9 -> '9'
c = (char)(s + '0');
// or to get Unicode value
c = (char)s;
// to int i from short s
i = s;
// to long n from short s
n = s;
// to float f from short s
f = s;
// to double d from short s
d = s;
// 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 = (byte)s;
// to /*unsigned*/ Byte uu from short s
uu = (byte)s;
// to Short ss from short s
ss = s;
// to Character cc from short s
// 9 -> '9'
cc = (char)(s + '0');
// or to get Unicode value
cc = (char)s;
// to Integer ii from short s
ii = (int)s;
// to Long nn from short s
nn = (long)s;
// to Float ff from short s
ff = (float)s; /* locale-insensitive */
// to Double dd from short s
dd = (double)s; /* locale-insensitive */
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 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
// international
s = (short)Character.digit(c, 10 /* radix */);
// fastest '9' -> 9
s = (short)(c - '0');
// or to get Unicode value
s = (short)c;
-------- // to int i from char c
// international
i = Character.digit(c, 10 /* radix */);
// fastest '9' -> 9
i = c - '0';
// or to get Unicode value
i = c; // does not sign extend.
// to long n from char c
// international
n = Character.digit(c, 10 /* radix */);
// fastest '9' -> 9
n = c - '0';
// or to get Unicode value
n = c; // does not sign extend.
// to float f from char c
f = c; // does not sign extend.
// to double d from char c
d = c; // does not sign extend.
// 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 = (byte)c;
// to /*unsigned*/ Byte uu from char c
uu = (byte)c;
// to Short ss from char c
// international
ss = (short)Character.digit(c, 10 /* radix */);
// fastest '9' -> 9
ss = (short)(c - '0');
// or to get Unicode value
ss = (short)c;
// to Character cc from char c
cc = c;
// to Integer ii from char c
// international
ii = Character.digit(c, 10 /* radix */);
// fastest '9' -> 9
ii = c - '0';
// or to get Unicode value
ii = (int)c;
// to Long nn from char c
// international,
nn = (long)Character.digit(c, 10 /* radix */);
// fastest '9' -> 9
nn = (long)(c - '0');
// or to get Unicode value
nn = (long)c;
// to Float ff from char c
ff = (float)c; // does not sign extend.
// to Double dd from char c
dd = (double)c; // does not sign extend.
char c
int i // to boolean t from int i
t = i!=0;
// to byte b from int i
b = (byte)i;
// to /*unsigned*/ byte u from int i
u = (byte)(i & 0xff);
// 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
n = i;
// to float f from int i
f = i;
// to construct a float out of IEEE bits
f = Float.intBitsToFloat(i);
// to double d from int i
d = i;
// 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 =(byte)i;
// to /*unsigned*/ Byte uu from int i
uu = (byte)(i & 0xff);
// to Short ss from int i
ss = (short)i;
// to Character cc from int i
// 9 -> '9'
cc = (char)(i + '0');
// or to get Unicode value
cc = (char)i;
// to Integer ii from int i
ii = i;
// to Long nn from int i
nn = (long)i;
// to Float ff from int i
ff = (float)i;
// to Double dd from int i
dd = (double)i;
int i
long n // to boolean t from long n
t = n!=0;
// to byte b from long n
b = (byte)n;
// to /*unsigned*/ byte u from long n
u = (byte)(n & 0xff);
// 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
f = n;
// to double d from long n
d = n;
// to construct a double out of IEEE bits
d = Double.longBitsToDouble (n);
// 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 = (byte)n;
// to /*unsigned*/ Byte uu from long n
uu = (byte)n;
// to Short ss from long n
ss = (short)n;
// to Character cc from long n
// 9 -> '9'
cc = (char)(n + '0');
// or to get Unicode value
cc = (char)n;
// to Integer ii from long n
ii = (int)n;
// to Long nn from long n
nn = n;
// to Float ff from long n
ff = (float)n;
// to Double dd from long n
dd = (double)n;
long n
float f // to boolean t from float f
t = f!=0;
// to 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
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
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
d = f;
// 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 = (byte)f;
// to /*unsigned*/ Byte uu from float f
uu = (byte)f;
// to Short ss from float f
ss = (short)f;
// to Character cc from float f
cc = (char)f;
// to Integer ii from float f
ii = (int)f;
// to Long nn from float f
nn = (long)f;
// to Float ff from float f
ff = f;
// to Double dd from float f
dd = (double)f;
float f
double d // to boolean t from double d
t = d!=0;
// to 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
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
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 = (byte)d;
// to /*unsigned*/ Byte uu from double d
uu = (byte)d;
// to Short ss from double d
ss = (short)d;
// to Character cc from double d
cc = (char)d;
// to Integer ii from double d
ii = (int)d;
// to Long nn from double d
nn = (long)d;
// to Float ff from double d
ff = (float)d;
// to Double dd from double d
dd = d;
double d
String g // to boolean t from String g
t = Boolean.valueOf( g.trim() );
// or
t = g.trim().equalsIgnoreCase("true");
// to byte b from String g
try {
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 {
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 {
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 {
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 {
// locale-insensitive
f = Float.parseFloat(g.trim());
// locale-insensitive
f = Float.valueOf(g.trim());
} catch (NumberFormatException e){/* ... */}
// to double d from String g
try {
// locale-insensitive
d = Double.parseDouble(g.trim());
} catch (NumberFormatException e){/* ... */}
-------- // to Boolean tt from String g
tt = Boolean.valueOf(g.trim());
// to /*signed*/ Byte bb from String g
try {
bb = Byte.parseByte(g.trim());
// or
bb = Byte.parseByte(g.trim(), 16 /* radix */);
// or
bb = (byte)g.charAt(0 /* position */);
} catch (NumberFormatException e){/* ... */}
// to /*unsigned*/ Byte uu from String g
try {
uu = Byte.parseByte(g.trim());
// or
uu = Byte.parseByte(g.trim(), 16 /* radix */);
// or
uu = (byte)g.charAt(0 /* position */);
} catch (NumberFormatException e){/* ... */}
// to Short ss from String g
try {
ss = Short.parseShort(g.trim());
// or
ss = Short.parseShort(g.trim(), 16 /* radix */);
// or
ss = (short)g.charAt(0 /* position */);
} catch (NumberFormatException e){/* ... */}
// to Character cc from String g
try {
// "9" -> '9'
cc = g.charAt(0 /* position */);
// or to get Unicode value
cc = (char)Integer.parseInt(g.trim());
// or to get Unicode hex value
cc = (char)Integer.parseInt(g.trim(), 16 /* radix */);
} catch (NumberFormatException e){/* ... */}
// to Integer ii from String g
try {
ii = Integer.valueOf(g.trim());
} catch (NumberFormatException e){/* ... */}
// to Long nn from String g
try {
nn = Long.valueOf(g.trim());
} catch (NumberFormatException e){/* ... */}
// to Float ff from String g
try {
// locale-insensitive.
ff = Float.valueOf(g.trim());
} catch (NumberFormatException e){/* ... */}
// to Double dd from String g
try {
// locale-insensitive
dd = Double.valueOf(g.trim());
} catch (NumberFormatException e){/* ... */}
String g
Boolean tt // to boolean t from Boolean tt
t = tt;
// to byte b from Boolean tt
b = tt ? (byte)1 : (byte)0;
// to /*unsigned*/ byte u from Boolean tt
u = tt ? (byte)1 : (byte)0;
// to short s from Boolean tt
s = tt ? (short)1 : (short)0;
// to char c from Boolean tt
// to get '0' and '1'
c = tt ? '1' : '0';
// or to get Unicode 0 and 1
c = tt ? (char)1 : (char)0;
// to int i from Boolean tt
i = tt ? 1 : 0;
// to long n from Boolean tt
n = tt ? 1L : 0L;
// to float f from Boolean tt
f = tt ? 1.0f : 0.0f;
// to double d from Boolean tt
d = tt ? 1.0d : 0.0d;
// to String g from Boolean tt
g = tt.toString();
-------- // to /*signed*/ Byte bb from Boolean tt
bb = tt ? (byte)1 : (byte)0;
// to /*unsigned*/ Byte uu from Boolean tt
uu = tt ? (byte)1 : (byte)0;
// to Short ss from Boolean tt
ss = tt ? (short)1 : (short)0;
// to Character cc from Boolean tt
// to get '0' and '1'
cc = tt ? '1' : '0';
// or to get Unicode 0 or 1
cc = tt ? (char)1 : (char)0;
// to Integer ii from Boolean tt
ii = tt ? 1 : 0;
// to Long nn from Boolean tt
nn = tt ? 1L : 0L;
// to Float ff from Boolean tt
ff = tt ? 1.0f : 0.0f;
// to Double dd from Boolean tt
dd = tt ? 1.0d : 0.0d;
Boolean tt
/*signed*/ Byte bb // to boolean t from /*signed*/ Byte bb
t = bb!=0;
// to byte b from /*signed*/ Byte bb
b = bb;
// to /*unsigned*/ byte u from /*signed*/ Byte bb
u = bb;
// to short s from /*signed*/ Byte bb
s = bb.shortValue();
// to char c from /*signed*/ Byte bb
// 9 -> '9'
c = (char)(bb.intValue() + '0');
// or to get Unicode value
c = (char)bb.intValue();
// to int i from /*signed*/ Byte bb
i = bb.intValue();
// to long n from /*signed*/ Byte bb
n = bb.longValue();
// to float f from /*signed*/ Byte bb
f = bb.floatValue();
// to double d from /*signed*/ Byte bb
d = bb.doubleValue();
// to String g from /*signed*/ Byte bb
g = bb.toString();
// to Boolean tt from /*signed*/ Byte bb
tt = (bb!='0') ? Boolean.TRUE : Boolean.FALSE;
-------- // to /*unsigned*/ Byte uu from /*signed*/ Byte bb
uu = bb;
// to Short ss from /*signed*/ Byte bb
ss = bb.shortValue();
// to Character cc from /*signed*/ Byte bb
// 9 -> '9'
cc = (char)(bb + '0');
// or to get Unicode value
cc = (char)bb.intValue();
// to Integer ii from /*signed*/ Byte bb
ii = bb.intValue();
// to Long nn from /*signed*/ Byte bb
nn = bb.longValue();
// to Float ff from /*signed*/ Byte bb
ff = bb.floatValue();
// to Double dd from /*signed*/ Byte bb
dd = bb.doubleValue();
/*signed*/ Byte bb
/*unsigned*/ Byte uu // to boolean t from /*unsigned*/ Byte uu
t = uu!=0;
// to byte b from /*unsigned*/ Byte uu
b = uu;
// to /*unsigned*/ byte u from /*unsigned*/ Byte uu
u = uu;
// to short s from /*unsigned*/ Byte uu
s = (short)(uu.intValue() & 0xff);
// to char c from /*unsigned*/ Byte uu
// 9 -> '9'
c = (char)((uu.intValue() + '0') & 0xff);
// or to get Unicode value
c = (char)(uu.intValue() & 0xff);
// to int i from /*unsigned*/ Byte uu
i = uu.intValue() & 0xff;
// to long n from /*unsigned*/ Byte uu
n = uu.longValue() & 0xff;
// to float f from /*unsigned*/ Byte uu
f = (float)(uu.intValue() & 0xff);
// to double d from /*unsigned*/ Byte uu
d = (double)(uu.intValue() & 0xff);
// to String g from /*unsigned*/ Byte uu
g = uu.toString();
// to Boolean tt from /*unsigned*/ Byte uu
tt = (uu!='0') ? Boolean.TRUE : Boolean.FALSE;
// to /*signed*/ Byte bb from /*unsigned*/ Byte uu
bb = uu;
-------- // to Short ss from /*unsigned*/ Byte uu
ss = (short)(uu & 0xff);
// to Character cc from /*unsigned*/ Byte uu
// 9 -> '9'
cc = (char)((uu.intValue() + '0') & 0xff);
// or to get Unicode value
cc = (char)(uu.intValue() & 0xff);
// to Integer ii from /*unsigned*/ Byte uu
ii = uu.intValue() & 0xff;
// to Long nn from /*unsigned*/ Byte uu
nn = uu.longValue() & 0xff;
// to Float ff from /*unsigned*/ Byte uu
ff = (float)(uu.intValue() & 0xff);
// to Double dd from /*unsigned*/ Byte uu
dd = (double)(uu.intValue() & 0xff);
/*unsigned*/ Byte uu
Short ss // to boolean t from Short ss
t = ss!=0;
// to byte b from Short ss
b = ss.byteValue();
// to /*unsigned*/ byte u from Short ss
u = ss.byteValue();
// to short s from Short ss
s = ss;
// to char c from Short ss
// 9 -> '9'
c = (char)(ss.intValue() + '0');
// or to get Unicode value
c = (char)ss.intValue();
// to int i from Short ss
i = ss.intValue();
// to long n from Short ss
n = ss.longValue();
// to float f from Short ss
f = ss.floatValue();
// to double d from Short ss
d = ss.doubleValue();
// to String g from Short ss
g = ss.toString();
// to Boolean tt from Short ss
tt = ss!=0 ? Boolean.TRUE : Boolean.FALSE;
// to /*signed*/ Byte bb from Short ss
bb = ss.byteValue();
// to /*unsigned*/ Byte uu from Short ss
uu = ss.byteValue();
-------- // to Character cc from Short ss
// 9 -> '9'
cc = (char)(ss.intValue() + '0');
// or to get Unicode value
cc = (char)ss.intValue();
// to Integer ii from Short ss
ii = ss.intValue();
// to Long nn from Short ss
nn = ss.longValue();
// to Float ff from Short ss
ff = ss.floatValue();
// to Double dd from Short ss
dd = ss.doubleValue();
Short ss
Character cc // to boolean t from Character cc
// to convert '0' or '1'
t = cc!='0';
// to convert Unicode 0 or 1
t = cc!=0;
// to byte b from Character cc
// international
b = (byte)Character.digit(cc, 10 /* radix */);
// fastest '9' -> 9
b = (byte)(cc - '0');
// or to get Unicode value
b = (byte)cc.charValue();
// to /*unsigned*/ byte u from Character cc
// international
u = (byte)Character.digit(cc, 10 /* radix */);
// fastest '9' -> 9
u = (byte)(cc - '0');
// or to get Unicode value
u = (byte)cc.charValue();
// to short s from Character cc
// international
s = (short)Character.digit(cc, 10 /* radix */);
// fastest '9' -> 9
s = (short)(cc - '0');
// or to get Unicode value
s = (short)cc.charValue();
// to char c from Character cc
c = cc;
// to int i from Character cc
// international
i = Character.digit(cc, 10 /* radix */);
// fastest '9' -> 9
i = cc - '0';
// or to get Unicode value
i = cc; // does not sign extend.
// to long n from Character cc
// international
n = Character.digit(cc, 10 /* radix */);
// fastest '9' -> 9
n = cc - '0';
// or to get Unicode value
n = cc; // does not sign extend.
// to float f from Character cc
f = cc; // does not sign extend.
// to double d from Character cc
d = cc; // does not sign extend.
// to String g from Character cc
g = cc.toString();
// to Boolean tt from Character cc
// for '0' or '1'
tt = cc!='0' ? Boolean.TRUE : Boolean.FALSE;
// or for Unicode 0 or 1
tt = cc!=0 ? Boolean.TRUE : Boolean.FALSE;
// to /*signed*/ Byte bb from Character cc
// international
bb =(byte)Character.digit(cc, 10 /* radix */);
// fastest '9' -> 9
bb = (byte)(cc - '0');
// or to get Unicode value
bb = (byte)cc.charValue();
// to /*unsigned*/ Byte uu from Character cc
// international
uu = (byte)Character.digit(cc, 10 /* radix */);
// fastest '9' -> 9
uu = (byte)(cc - '0');
// or to get Unicode value
uu = (byte)cc.charValue();
// to Short ss from Character cc
// international
ss = (short)Character.digit(cc, 10 /* radix */);
// fastest '9' -> 9
ss = (short)(cc - '0');
// or to get Unicode value
ss = (short)cc.charValue();
-------- // to Integer ii from Character cc
// international,
ii = Character.digit(cc, 10 /* radix */);
// fastest '9' -> 9
ii = cc - '0';
// or to get Unicode value
ii = (int)cc;
// to Long nn from Character cc
// international
nn = (long)Character.digit(cc, 10 /* radix */);
// fastest '9' -> 9
nn = (long)(cc - '0');
// or to get Unicode value
nn = (long)cc;
// to Float ff from Character cc
ff = (float)cc; // does not sign extend.
// to Double dd from Character cc
dd = (double)cc; // does not sign extend.
Character cc
Integer ii // to boolean t from Integer ii
t = ii!=0;
// to 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
i = ii;
// to long n from Integer ii
n = ii.longValue();
// to float f from Integer ii
f = ii.floatValue();
// to double d from Integer ii
d = ii.doubleValue();
// to String g from Integer ii
g = ii.toString();
// to Boolean tt from Integer ii
tt = (ii!= 0) ? Boolean.TRUE : Boolean.FALSE;
// to /*signed*/ Byte bb from Integer ii
bb = ii.byteValue();
// to /*unsigned*/ Byte uu from Integer ii
uu = ii.byteValue();
// to Short ss from Integer ii
ss = ii.shortValue();
// to Character cc from Integer ii
// 9 -> '9'
cc = (char)(ii + '0');
// or to get Unicode value
cc = (char)ii.intValue();
-------- // to Long nn from Integer ii
nn = ii.longValue();
// to Float ff from Integer ii
ff = ii.floatValue();
// to Double dd from Integer ii
dd = ii.doubleValue();
Integer ii
Long nn // to boolean t from Long nn
t = nn!=0;
// to 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
n = nn;
// to float f from Long nn
f = nn.floatValue();
// to double d from Long nn
d = nn.doubleValue();
// to String g from Long nn
g = nn.toString();
// to Boolean tt from Long nn
tt = (nn!= 0) ? Boolean.TRUE : Boolean.FALSE;
// to /*signed*/ Byte bb from Long nn
bb = nn.byteValue();
// to /*unsigned*/ Byte uu from Long nn
uu = nn.byteValue();
// to Short ss from Long nn
ss = nn.shortValue();
// to Character cc from Long nn
// 9 -> '9'
cc = (char)(nn.intValue() + '0');
// or to get Unicode value
cc = (char)nn.intValue();
// to Integer ii from Long nn
ii = nn.intValue();
-------- // to Float ff from Long nn
ff = nn.floatValue();
// to Double dd from Long nn
dd = nn.doubleValue();
Long nn
Float ff // to boolean t from Float ff
t = ff!=0;
// to 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
f = ff;
// to double d from Float ff
d = ff.doubleValue();
// 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);
// or exponential scientific format, locale-sensitive.
java.text.DecimalFormat de
= new java.text.DecimalFormat("0.000000E00");
g = de.format(ff);
// or best for readability and maintainability, locale-insensitive.
g = ff.toString();
// to Boolean tt from Float ff
tt = (ff!=0) ? Boolean.TRUE : Boolean.FALSE;
// to /*signed*/ Byte bb from Float ff
bb = ff.byteValue();
// to /*unsigned*/ Byte uu from Float ff
uu = ff.byteValue();
// to Short ss from Float ff
ss = ff.shortValue();
// to Character cc from Float ff
cc = (char)ff.intValue();
// to Integer ii from Float ff
ii = ff.intValue();
// to Long nn from Float ff
nn = ff.longValue();
-------- // to Double dd from Float ff
dd = ff.doubleValue();
Float ff
Double dd // to boolean t from Double dd
t = dd!=0;
// to 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
d = dd;
// 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);
// or exponential scientific format, locale sensitive.
java.text.DecimalFormat df
= new java.text.DecimalFormat("0.0000000000E00");
g = df.format(dd);
// or best for readability and maintainability, locale-insensitive
g = dd.toString();
// to Boolean tt from Double dd
tt = (dd!=0) ? Boolean.TRUE : Boolean.FALSE;
// to /*signed*/ Byte bb from Double dd
bb = dd.byteValue();
// to /*unsigned*/ Byte uu from Double dd
uu = dd.byteValue();
// to Short ss from Double dd
ss = dd.shortValue();
// to Character cc from Double dd
cc = (char)dd.intValue();
// to Integer ii from Double dd
ii = dd.intValue();
// to Long nn from Double dd
nn = dd.longValue();
// to Float ff from Double dd
ff = dd.floatValue();
-------- Double dd
  to: boolean t to: 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  

Primitives

Primitive variables include Boolean, char, byte, short, int, long, float and double. Strings, arrays and Objects are not considered primitives.
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 aka -2 gig, roughly -2 billion +2,147,483,647 [231-1] aka Integer. MAX_VALUE. aka 2 gig, roughly 2 billion 2 gig
long signed 64 8 1:19 -9,223,372,036,854,775,807 [-263] aka Long. MIN_VALUE about -9×1018 9,223,372,036,854,775,808 [+263-1] aka Long. MAX_VALUE about 9×1018 9 exabytes, or 9 billion gig
float signed exponent and mantissa 32 4 7 ±1.40129846432481707e-45 aka Float. MIN_VALUE ±3.40282346638528860e+38 aka Float. MAX_VALUE
or roughly ±2127
with about 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 16 ±4.94065645841246544e-324 aka Double. MIN_VALUE ±1.79769313486231570e+308 aka Double. MAX_VALUE
or roughly ±21023
with 15 significant digits of accuracy, almost 16 with 15.95 significant digits.
A double can exactly represent integers
in the range -253 to +253.
high precision float

Primitives vs Immutable Wrapper Objects

Contrast that table of primitives, with this table of basic Java types:

Immutable 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

Truncation

If a long is converted to an int, or an int to a byte, the high order bits are simply truncated. This can result in surprising results including sign reversal.

byte[] ⇔ String

Interconverting byte [] and String is tricky because there is always an implied encoding translation. "8859_1" encoding simply chops the high byte off or pads the high byte with 0s, what newbies erroneously imagine happens all the time.

String s = "abc";

// string -> byte[]
byte [] b = s.getBytes( "8859_1" /* encoding */ );

// byte[] -> String
String t = new String( byteArray, "Cp1252" /* encoding */ );

// subset of byte[] -> String
String t = new String( byteArray, offset, length, "UTF-8" /* encoding */ );

char[] ⇔ String

Interconverting char [] and String is easier because there is no encoding involved. Encoding is about how to represent 16-bit Unicode in 8-bit bytes.

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.

Hexadecimal

The radix feature of the toString and parseInt methods lets you handle hexadecimal numbers. Just set the radix to 16.

StringBuffers & StringBuilders

Strings and StringBuffers can be interconverted, as can Strings and StringBuilders

Rounding

Rounding often surprises because fractions like 0.1 cannot be precisely be represented in IEEE (Institute of Electrical & Electronics Engineers) floating point format. Often you find yourself having to add tiny numbers just prior to printing to get the desired effects.

// 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.

Left Zeros

The standard conversions give you no leading blanks or leading zeroes. Here is a code snippet to convert an int to a String padded with leading left zeroes. With an obvious modification it would give you lead blanks.

Formatted Output

JDK (Java Development Kit) 1.0.2 has no function like C’s printf that lets you control how many positions and decimal places you want in your output. You have to roll your own.

Java version 1.1 or laterhas 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.

Reading Numeric Data From an ASCII (American Standard Code for Information Interchange) file

Java has no built-in methods for reading data of the form: 123 456,-4.

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.

StreamTokenizer Method of Reading Integers From An ASCII File

StringTokenizer Method of Reading Integers From An ASCII file

Binary

Binary is a compact, machine-friendly, human-unintelligible format. For human readable i/o, Java works with Strings of characters. You separately convert these to and from internal binary format e.g. int. See the conversion Amanuensis for how.

Metric Conversions

You can convert metric to Imperial or other measurements by putting expressions in ordinary Google search boxes:


This page is posted
on the web at:

http://mindprod.com/jgloss/convert15.html

Optional Replicator mirror
of mindprod.com
on local hard disk J:

J:\mindprod\jgloss\convert15.html
logo
Please the feedback from other visitors, or your own feedback about the site.
Contact Roedy. Please feel free to link to this page without explicit permission.
Blog
IP:[65.110.21.43]
Your face IP:[54.198.139.30]
You are visitor number