Showing posts with label valueOf(). Show all posts
Showing posts with label valueOf(). Show all posts

Tuesday, February 26, 2013

String.valueOf

DOWNLOAD THE CODE: StringValueOfPkg

Why use this function?
You want to treat a number like a String.

String.valueOf()
The String class has a function call valueOf(). This function will return the String representation of all the numeric data types (int, double, boolean, char, etc).

In the example below, two numbers are stored as int. The first print statement attempts to print each int as if they were strings. In this case, we would be hoping to see 100200 printed to screen. Notice that the concatenation operator is used - which, if you place between 2 numbers, becomes the addition operator.

The second print statement uses the String.valueOf() function. The function, as it is being used here, will return the String representation of each int.

The output for each statement is very different. The first print statement has caused the two int variables to be added (addition) together, whereas the second print statement has caused each int to be printed as a String.



Also see:
"Java - String valueOf() Method" ( http://bit.ly/YSRK2u )

Here's a cat: