Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community!

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial


Is there a preferred method of converting a value from one data type to another?

From what I have gathered, there are several ways to convert values from one data type to another:

String myStringValue = "123"; int myIntValue; //convert string to integer: myIntValue = Convert.ToInt32(myStringValue); //or myIntValue = (int)myStringValue; //or myIntValue = Integer.parseInt(myStringValue);

Is one method more prevalent than the others? Are they all equally capable?

I know that there is a toString() method that can be called on variables and literals of numeric type but is there a corresponding casting syntax as well, such as myStringValue = (String)myIntValue; ?

Thank you.

1 Answer

Rob Bridges
.a{fill-rule:evenodd;}techdegree seal-36
Rob Bridges
Full Stack JavaScript Techdegree Graduate 35,467 Points

Hey there Adiv,

Java does have it's specifics, thankfully like most programming languages there are many different ways to get the job done, whichever you feel the most comfortable doing.

Me personally I prefer Integet.parseInt(); this is my personal preference, use what works best for you.

like you hinted at, the two ways beneath would both work perfectly.

int intVal = 4;
String toString = Integer.toString(stringVal);

Would do the job perfectly,

as would.

String stringVal = "4";
int intval = Integer.parseInt(stringVal);

These are two of the most used methods to turn an W54INT to a String like you said.

Now what you were Originally talking about is more in line of int values.

Lets say we have a single value of 4. We origionally had it set as an int, but we now want to use it as 4.0 originally passing this value in might cause problems, So we'd type cast it like below.

int myInt = 4;

double myDouble = (double)myInt;

The above code would appropriately turn your int into a double.

Though there are many ways to typecast a string into an int (String)myIntValue; unfortunately is not one of them. This is mainly because what we can easily typecast, floats, doubles, and ints, because they what we call primitive types in Java, and were built into the program as data types, making conversion easy. String is not a primitive type, but a class built into java (hence why we capitalize it), however they did include multiple ways to turn an int into a string, with methods built into the Pre-defined class, without it being a primitive type we again can't unfortunately do


Thanks, give me a shout if this still have questions.

Thank you for the very helpful clarifications. I have learned from your discussion that the casting syntax (<data type>) varName works only with the primitive data types but not with String objects.

Regarding converting an int to a double, wouldn't a simple assignment statement suffice, since it is a widening conversion, i..e.

int myInt = 4; double myDouble = myInt; //myDouble is now 4.0

But perhaps the reverse process, double to int would throw an exception or at least cause a loss of precision since it is a narrowing conversion, e.g.

double myDouble = 4.56788; int myInt = myDouble; //will this throw an exception or just truncate the fractional component of myDouble?

Rob Bridges
.a{fill-rule:evenodd;}techdegree seal-36
Rob Bridges
Full Stack JavaScript Techdegree Graduate 35,467 Points

Hey again Adiv,

Glad I could help, in your example below that would just turn your 4.56788 into an int, so while the original value would still remain in myDouble, you would have the value 4 stored in my int, anything following the 4 would be disregarded by Java.