Software Technology Tips

 There are times while coding when we need to convert an entity of one datatype to another.  For instance, in the following example, assigning an integer value to a string would throw exception, so what we would do instead is convert the integer into a string value.
 
integer to String :
int i = 42;
String str = Integer.toString(i);
   or
String str = "" + i;


Below are additional examples of conversion from one data type to another in java.
 
double to String :
String str = Double.toString(i);
 
long to String :
String str = Long.toString(l);
 
float to String :
String str = Float.tString(f);
 
String to integer : str = "25";
   int i = Integer.valueOf(str).intValue();
   or
   int i = Integer.parseInt(str);
 
String to double :
Double d = Double.valueOf(str).doubleValue();
 
String to long:
 long l = Long.valueOf(str).longValue();
   or
Long L = Long.parseLong(str);
 
String to float :
Float f = Float.valueOf(str).floatValue();
 
decimal to binary : int i = 42;
String bin = Integer.toBinaryString(i);

 


Related Tags:

Java1.6

Author: Tanmayee Sahoo

Related Pages

  • Java Web Development

Java

Let us Connect!

Awards and Achievements

Red Herringcolor DeloitteFast50 DB ZInnov1   Nascome

Events

serf DDM

This site uses cookies. We respect your privacy.copyright (c) Mindfire Solutions 2007-2015. Login