Tuesday, December 13, 2011

Data types conversions in java


In the last weeks I've been programming in Java for 2 school works and others 3 more projects, I'm the type of person how prefer thing more low-level, so I can say that I never used it so much in so little time. The good thing about that is that I'm finally learning many things about objects and events, it's just like a new world.
So I have been taking notes and I'll share a little about a thing that I always forget, how to convert between data types commonly used.





byte
short
int
long
float
Byte
byteValue()
shortValue()
intValue()
longValue()
floatValue()
Short
byteValue()
shortValue()
intValue()
longValue()
floatValue()
Integer
byteValue()
shortValue()
intValue()
longValue()
floatValue()
Long
byteValue()
shortValue()
intValue()
longValue()
floatValue()
Float
byteValue()
shortValue()
intValue()
longValue()
floatValue()
Double
byteValue()
shortValue()
intValue()
longValue()
floatValue()
Boolean
(byte) ((a)?1:0)
(short) ((a)?1:0)
((a)?1:0)
(long) ((a)?1:0)
(float) ((a)?1:0)
String
Byte.parseByte(a)
Short.parseShort(a)
Integer.parseInt(a)
Long.parseLong(a)
Float.parseFloat(a)




double
boolean
char
String
Byte
doubleValue()
!(compareTo(0)==0)
Character.forDigit(a,10)
String.ValueOf(a)
Short
doubleValue()
!(compareTo(0)==0)
Character.forDigit(a,10)
String.ValueOf(a)
Integer
doubleValue()
!(compareTo(0)==0)
Character.forDigit(a,10)
String.ValueOf(a)
Long
doubleValue()
!(compareTo(0)==0)
Character.forDigit(a,10)
String.ValueOf(a)
Float
doubleValue()
!(compareTo(0f)==0)
Character.forDigit(a,10)
String.ValueOf(a)
Double
doubleValue()
!(compareTo(0.0)==0)
Character.forDigit(a,10)
String.ValueOf(a)
Boolean
(double) ((a)?1:0)
a
 ((a)?’T’:’F’)
String.ValueOf(a)
String
Double.parseDouble(a)
!(compareTo(“0”)==0)
a.charAt(0);
String.ValueOf(a)

This explains how to convert from classes for primitive types, on the left side are the original classes and on top are the primitive types. Of course there are other important things, but is already a good summary.

No comments:

Post a Comment