JDK 1.4- 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 = new Byte(t?(byte)1:(byte)0); |
// to /*unsigned*/ Byte uu from boolean t uu = new Byte(t?(byte)1:(byte)0); |
// to Short ss from boolean t ss = new Short(t?(short)1:(short)0); |
// to Character cc from boolean t // to get '0' or '1' cc = new Character(t?'1':'0'); // or to get Unicode 0 or 1 cc = new Character(t?(char)1:(char)0); |
// 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?1L:0L); // or JDK 1.4- nn = new Long(t?1L:0L); |
// to Float ff from boolean t ff = new Float(t?1.f:0.0f); |
// to Double dd from boolean t dd = new Double(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 = new Byte(b); |
// to /*unsigned*/ Byte uu from byte b uu = new Byte(b); |
// to Short ss from byte b ss = new Short(b); // sign extends |
// to Character cc from byte b // 9 -> '9' cc = new Character((char)(b + '0')); // or to get Unicode value cc = new Character((char)b); |
// to Integer ii from byte b // best JDK 1.5+ ii = Integer.valueOf(b); // or JDK 1.4- ii = new Integer(b); // sign extends. |
// to Long nn from byte b // best JDK 1.5+ nn = Long.valueOf(b); // or JDK 1.4- nn = new Long(b); // sign extends. |
// to Float ff from byte b ff = new Float(b); // sign extends. |
// to Double dd from byte b dd = new 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 = ((long)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 = new Byte(u); |
// to /*unsigned*/ Byte uu from /*unsigned*/ byte u uu = new Byte(u); |
// to Short ss from /*unsigned*/ byte u ss = new Short((short)(u & 0xff)); // does not sign extend. |
// to Character cc from /*unsigned*/ byte u // 9 -> '9' cc = new Character((char)((u & 0xff) + '0')); // or to get Unicode value cc = new Character((char)(u & 0xff)); |
// to Integer ii from /*unsigned*/ byte u // best JDK 1.5+ ii = Integer.valueOf(u & 0xff); // or JDK 1.4- ii = new Integer(u & 0xff); // does not sign extend. |
// to Long nn from /*unsigned*/ byte u // best JDK 1.5+ nn = Long.valueOf(((long)u) & 0xff); // or JDK 1.4- nn = new Long(((long)u) & 0xff); // does not sign extend. |
// to Float ff from /*unsigned*/ byte u ff = new Float(((long)u) & 0xff); // does not sign extend. |
// to Double dd from /*unsigned*/ byte u dd = new Double(((long)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 = new Byte((byte)s); |
// to /*unsigned*/ Byte uu from short s uu = new Byte((byte)s); |
// to Short ss from short s ss = new Short(s); |
// to Character cc from short s // 9 -> '9' cc = new Character((char)(s + '0')); // or to get Unicode value cc = new Character((char)s); |
// to Integer ii from short s // best JDK 1.5+ ii = Integer.valueOf(s); // or JDK 1.4- ii = new Integer(s); |
// to Long nn from short s // best JDK 1.5+ nn = Long.valueOf(s); // or JDK 1.4- nn = new Long(s); |
// to Float ff from short s ff = new Float(s); /* locale-insensitive */ |
// to Double dd from short s dd = new 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 = new Byte((byte)c); |
// to /*unsigned*/ Byte uu from char c uu = new Byte((byte)c); |
// to Short ss from char c // international ss = new Short((short)Character.digit(c, 10 /* radix */)); // fastest '9' -> 9 ss = new Short((short)(c - '0')); // or to get Unicode value ss = new Short((short)c); |
// to Character cc from char c cc = new Character(c); |
// to Integer ii from char c // international, JDK 1.5+ ii = Integer.valueOf(Character.digit(c, 10 /* radix */)); // fastest '9' -> 9, JDK 1.5+ ii = Integer.valueOf(c - '0'); // or to get Unicode value JDK 1.5+ ii = Integer.valueOf(c); // international, JDK 1.4- ii = new Integer(Character.digit(c, 10 /* radix */)); // fastest '9' -> 9, JDK 1.4- ii = new Integer(c - '0'); // or to get Unicode value JDK 1.4- ii = new Integer(c); |
// to Long nn from char c // international, JDK 1.5+ nn = Long.valueOf(Character.digit(c, 10 /* radix */)); // fastest '9' -> 9, JDK 1.5+ nn = Long.valueOf(c - '0'); // or to get Unicode value JDK 1.5+ nn = Long.valueOf(c);// international, JDK 1.4- nn = new Long(Character.digit(c, 10 /* radix */)); // fastest '9' -> 9, JDK 1.4- nn = new Long(c - '0'); // or to get Unicode value JDK 1.4- nn = new Long(c); |
// to Float ff from char c ff = new Float(c); // does not sign extend. |
// to Double dd from char c dd = new 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; |
// 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 = 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 // best JDK 1.5+ ii = Integer.valueOf(i); // or JDK 1.4- ii = new Integer(i); |
// to Long nn from int i // best JDK 1.5+ nn = Long.valueOf(i); // or JDK 1.4- nn = new Long(i); |
// to Float ff from int i ff = new Float(i); |
// to Double dd from int i dd = new 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; |
// 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 = 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 // best JDK 1.5+ nn = Long.valueOf(n); // or JDK 1.4- nn = new Long(n); |
// to Float ff from long n ff = new Float(n); |
// to Double dd from long n dd = new 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 // 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 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 = 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 ii = Integer.valueOf((int)f); |
// to Long nn from float f nn = Long.valueOf((long)f); |
// to Float ff from float f ff = new Float(f); |
// to Double dd from float f dd = new 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 // 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 ii = Integer.valueOf((int)d); |
// to Long nn from double d nn = Long.valueOf((long)d); |
// to Float ff from double d ff = new Float(d); |
// to Double dd from double d dd = new Double(d); |
double d |
String g | // to boolean t from String g t = Boolean.valueOf(g.trim()).booleanValue(); // or t = g.trim().equalsIgnoreCase("true"); |
// to 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 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 t = tt.booleanValue(); |
// to byte b from Boolean tt b = tt.booleanValue()?(byte)1:(byte)0; |
// to /*unsigned*/ byte u from Boolean tt u = tt.booleanValue()?(byte)1:(byte)0; |
// to short s from Boolean tt s = tt.booleanValue()?(short)1:(short)0; |
// to char c from Boolean tt // to get '0' and '1' c = tt.booleanValue()?'1':'0'; // or to get Unicode 0 and 1 c = tt.booleanValue()?(char)1:(char)0; |
// to int i from Boolean tt i = tt.booleanValue()?1:0; |
// to long n from Boolean tt n = tt.booleanValue()?1L:0L; |
// to float f from Boolean tt f = tt.booleanValue()?1.0f:0.0f; |
// to double d from Boolean tt d = tt.booleanValue()?1.0d:0.0d; |
// to String g from Boolean tt g = tt.toString(); |
-------- | // to /*signed*/ Byte bb from Boolean tt bb = new Byte(tt.booleanValue()?(byte)1:(byte)0); |
// to /*unsigned*/ Byte uu from Boolean tt uu = new Byte(tt.booleanValue()?(byte)1:(byte)0); |
// to Short ss from Boolean tt ss = new Short(tt.booleanValue()?(short)1:(short)0); |
// to Character cc from Boolean tt // to get '0' and '1' cc = new Character(tt.booleanValue()?'1':'0'); // or to get Unicode 0 or 1 cc = new Character(tt.booleanValue()?(char)1:(char)0); |
// to Integer ii from Boolean tt ii = Integer.valueOf(tt.booleanValue()?1:0); |
// to Long nn from Boolean tt nn = Long.valueOf(tt.booleanValue()?1L:0L); |
// to Float ff from Boolean tt ff = new Float(tt.booleanValue()?1.0f:0.0f); |
// to Double dd from Boolean tt dd = new Double(tt.booleanValue()?1.0d:0.0d); |
Boolean tt |
/*signed*/ Byte bb | // to boolean t from /*signed*/ Byte bb t = bb.byteValue()!=0; |
// to byte b from /*signed*/ Byte bb b = bb.byteValue(); |
// to /*unsigned*/ byte u from /*signed*/ Byte bb u = bb.byteValue(); |
// to short s from /*signed*/ Byte bb s = bb.shortValue(); |
// to char c from /*signed*/ Byte bb // 9 -> '9' c = (char)(bb.byteValue() + '0'); // or to get Unicode value c = (char)bb.byteValue(); |
// 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 // 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 uu = bb; |
// to Short ss from /*signed*/ Byte bb ss = new Short(bb.shortValue()); |
// to Character cc from /*signed*/ Byte bb // 9 -> '9' cc = new Character((char)(bb.byteValue() + '0')); // or to get Unicode value cc = new Character((char)bb.shortValue()); |
// to Integer ii from /*signed*/ Byte bb // best JDK 1.5+ ii = Integer.valueOf(bb.byteValue()); // or JDK 1.4- ii = new Integer(bb.byteValue()); |
// to Long nn from /*signed*/ Byte bb // best JDK 1.5+ nn = Long.valueOf(bb.byteValue()); // or JDK 1.4- nn = new Long(bb.byteValue()); |
// to Float ff from /*signed*/ Byte bb ff = new Float(bb.byteValue()); |
// to Double dd from /*signed*/ Byte bb dd = new Double(bb.byteValue()); |
/*signed*/ Byte bb |
/*unsigned*/ Byte uu | // to boolean t from /*unsigned*/ Byte uu t = uu.byteValue()!=0; |
// to byte b from /*unsigned*/ Byte uu b = uu.byteValue(); |
// to /*unsigned*/ byte u from /*unsigned*/ Byte uu u = uu.byteValue(); |
// 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 // 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 bb = uu; |
-------- | // to Short ss from /*unsigned*/ Byte uu ss = new Short((short)(uu.byteValue() & 0xff)); |
// to Character cc from /*unsigned*/ Byte uu // 9 -> '9' cc = new Character((char)((uu.intValue() + '0') & 0xff)); // or to get Unicode value cc = new Character((char)(uu.intValue() & 0xff)); |
// to Integer ii from /*unsigned*/ Byte uu // best JDK 1.5+ ii = Integer.valueOf(uu.intValue() & 0xff); // or JDK 1.4- ii = new Integer(uu.intValue() & 0xff); |
// to Long nn from /*unsigned*/ Byte uu // best JDK 1.5+ nn = Long.valueOf(uu.longValue() & 0xff); // or JDK 1.4- nn = new Long(uu.longValue() & 0xff); |
// to Float ff from /*unsigned*/ Byte uu ff = new Float(uu.intValue() & 0xff); |
// to Double dd from /*unsigned*/ Byte uu dd = new Double(uu.intValue() & 0xff); |
/*unsigned*/ Byte uu |
Short ss | // to boolean t from Short ss t = ss.shortValue()!=0; |
// to 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 s = ss.shortValue(); |
// to char c from Short ss // 9 -> '9' c = (char)(ss.shortValue() + '0'); // or to get Unicode value c = (char)ss.shortValue(); |
// 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 // 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 // 9 -> '9' cc = new Character((char)(ss.shortValue() + '0')); // or to get Unicode value cc = new Character((char)ss.shortValue()); |
// to Integer ii from Short ss // best JDK 1.5+ ii = Integer.valueOf(ss.shortValue()); // or JDK 1.4- ii = new Integer(ss.shortValue()); |
// to Long nn from Short ss // best JDK 1.5+ nn = Long.valueOf(ss.shortValue()); // or JDK 1.4- nn = new Long(ss.shortValue()); |
// to Float ff from Short ss ff = new Float(ss.shortValue()); |
// to Double dd from Short ss dd = new Double(ss.shortValue()); |
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 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 // international s = (short)Character.digit(cc.charValue(), 10 /* radix */); // fastest '9' -> 9 s = (short)(cc.charValue() - '0'); // or to get Unicode value s = (short)cc.charValue(); |
// to char c from Character cc c = cc.charValue(); |
// to int i from Character cc // international i = Character.digit(cc.charValue(), 10 /* radix */); // fastest '9' -> 9 i = cc.charValue() - '0'; // or to get Unicode value i = cc.charValue(); // does not sign extend. |
// to long n from Character cc // international n = Character.digit(cc.charValue(), 10 /* radix */); // fastest '9' -> 9 n = cc.charValue() - '0'; // or to get Unicode value n = cc.charValue(); // does not sign extend. |
// to float f from Character cc f = cc.charValue(); // does not sign extend. |
// to double d from Character cc d = cc.charValue(); // 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.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 // international ss = new Short((short)Character.digit(cc.charValue(), 10 /* radix */)); // fastest '9' -> 9 ss = new Short((short)(cc.charValue() - '0')); // or to get Unicode value ss = new Short((short)cc.charValue()); |
-------- | // to Integer ii from Character cc // international, JDK 1.5+ ii = Integer.valueOf(Character.digit(cc.charValue(), 10 /* radix */)); // fastest '9' -> 9, JDK 1.5+ ii = Integer.valueOf(cc.charValue() - '0'); // or to get Unicode value JDK 1.5+ ii = Integer.valueOf(cc.charValue());// international, JDK 1.4- ii = new Integer(Character.digit(cc.charValue(), 10 /* radix */)); // fastest '9' -> 9, JDK 1.4- ii = new Integer(cc.charValue() - '0'); // or to get Unicode value JDK 1.4- ii = new Integer(cc.charValue()); |
// to Long nn from Character cc // international, JDK 1.5+ nn = Long.valueOf(Character.digit(cc.charValue(), 10 /* radix */)); // fastest '9' -> 9, JDK 1.5+ nn = Long.valueOf(cc.charValue() - '0'); // or to get Unicode value JDK 1.5+ nn = Long.valueOf(cc.charValue());// international, JDK 1.4- nn = new Long(Character.digit(cc.charValue(), 10 /* radix */)); // fastest '9' -> 9, JDK 1.4- nn = new Long(cc.charValue() - '0'); // or to get Unicode value JDK 1.4- nn = new Long(cc.charValue()); |
// to Float ff from Character cc ff = new Float(cc.charValue()); // does not sign extend. |
// to Double dd from Character cc dd = new Double(cc.charValue()); // does not sign extend. |
Character cc |
Integer ii | // to boolean t from Integer ii t = ii.intValue()!=0; |
// to byte b from Integer ii b = ii.byteValue(); |
// to /*unsigned*/ byte u from Integer ii u = (byte)(ii.byteValue() & 0xff); |
// to short s from Integer ii s = ii.shortValue(); |
// to char c from Integer ii c = (char)ii.intValue(); |
// to int i from Integer ii i = ii.intValue(); |
// 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.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 // best JDK 1.5+ nn = Long.valueOf(ii.intValue()); // or JDK 1.4- nn = new Long(ii.longValue()); |
// to Float ff from Integer ii ff = new Float(ii.floatValue()); |
// to Double dd from Integer ii dd = new Double(ii.doubleValue()); |
Integer ii |
Long nn | // to boolean t from Long nn t = nn.longValue()!=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.longValue(); |
// 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.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 ff = new Float(nn.longValue()); |
// to Double dd from Long nn dd = new Double(nn.longValue()); |
Long nn |
Float ff | // to boolean t from Float ff t = ff.floatValue()!=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.floatValue(); |
// 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.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 dd = new Double(ff.floatValue()); |
Float ff |
Double dd | // to boolean t from Double dd t = dd.doubleValue()!=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.doubleValue(); |
// 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: 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 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 |
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 |
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.
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 */ );
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.
The radix feature of the toString and parseInt methods lets you handle hexadecimal numbers. Just set the radix to 16.
Strings and StringBuffers can be interconverted, as can Strings and StringBuilders
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.
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.
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.
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.
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.
You can convert metric to Imperial or other measurements by putting expressions in ordinary Google search boxes:
This page is posted |
http://mindprod.com/jgloss/convert14.html | |
Optional Replicator mirror
|
J:\mindprod\jgloss\convert14.html | |
Please read the feedback from other visitors,
or send your own feedback about the site. Contact Roedy. Please feel free to link to this page without explicit permission. | ||
Canadian
Mind
Products
IP:[65.110.21.43] Your face IP:[3.14.134.18] |
| |
Feedback |
You are visitor number | |