PDA

View Full Version : Converting datatype in Java



bijayanib
05-24-2010, 02:13 AM
Hi All,

I came across a Java tips and would like to share with you. The tips is regarding "Converting datatype in Java". here is the few detail about the 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;


Hope you find it useful and of assistance.

Regards,
Bijayani

thersey
06-28-2010, 06:43 AM
Another method of integer to string conversion in Java is:

String abc= String.valueOf(i);

wige
06-28-2010, 10:23 AM
I believe you should also be able to accomplish the same thing using explicit casting, the same way you can in PHP (although you rarely need to since PHP uses implicit casting):

int i = 42;
String phrase = "Life, the universe and everything = ";
System.out.println(phrase + (String) i)

Granted, yes, the + operator will actually call the toString method automatically

chinhphucgiacmo
09-02-2010, 10:31 PM
I think , String abc= String.valueOf(i);