Page Stats
Visitor: 259
Java Type Casting
Type Casting is use to convert value of one data type into another data type. The process of assigning a smaller data type into larger data type is known as widening or promotion. For example:
byte b = 10; int i = b;
Assigning a larger data type value into a smaller data type is known as narrowing. In this type of assigning, loss of information may be possible.
int i = 10; byte b = (byte)i
Convert string to numeric value
int i = Integer.valueOf("22").intValue(); long l = Long.valueOf("22").longValue(); double d = Double.valueOf("22.5").doubleValue(); float f = Float.valueOf("22.5").floatValue();
int i = Integer.parseInt (str); long l = Long.parseLong (str); parseInt() and parseLong() methods throw a NumberFormatException if the value of str does not present an integer.
Converting Number to String
string str = Integer.toString(i); string str = Float.toString(f); string str = Double.toString(d); string str = Long.toString(l);