Java Conversions String to int|double|float|byte|long
Java Conversions int to Integer, Integet to int

public class WrapperDemo {
public static void main( String [] args ){


String intValue= "1212";
String doubleValue="121212.30";//double
String floatValue= "0.112";
String byteValue = "112";




//CONVERTING STRING TO NUMERIC VALUES
int toIntValue= Integer.parseInt(intValue);
double toDoubleValue = Double.parseDouble( doubleValue );
float toFloatValue = Float.parseFloat(floatValue);
byte toByteValue = Byte.parseByte( byteValue );

System.out.println( "--------------");
System.out.println( toDoubleValue );
System.out.println( toIntValue );
System.out.println( toFloatValue);
System.out.println( toByteValue );
System.out.println( "--------------");


//Conversion from NUMERIC primitive to NUMERIC Wrapper Object is very easy
//byte byteValue()


Byte toByte= new Byte( (byte)16 ); //cast mandatory for byte
Integer toInt = new Integer( 16 );
Double toDouble = new Double(Math.E);

//short shortValue()
short getShortValue = toInt.shortValue();
//int intValue()
int getIntValue = toInt.intValue();

//long longValue()
long getLong = toInt.longValue();

//float floatValue()
float getFloat = toDouble.floatValue();

//double doubleValue()
double getDouble = toDouble.doubleValue();

System.out.println( getDouble );

System.out.println( getIntValue );

}

}
Java ConversionsOutput
--------------
121212.3
1212
0.112
112
--------------
2.718281828459045
16
 

 

www.iforerunner.com