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:
Showing posts with label String functions. Show all posts
Showing posts with label String functions. Show all posts
Tuesday, February 26, 2013
Sunday, September 9, 2012
Concatenating adjacent numbers
Where a + symbol has a String on one or either sides, the + symbol is used as the concatenation operator. But where a + symbol has a number on either side, the + symbol is used as the addition operator.
Need to concatenate 2 adjacent numbers? Return the String value of one of the numbers in the following way:
Need to concatenate 2 adjacent numbers? Return the String value of one of the numbers in the following way:
Sunday, March 18, 2012
What is a String?
DOWNLOAD THE CODE: IntroToStringPkg.zip
A string is a collection of any keyboard characters such as "I love chocolate 100 times more than you !!!"
Where a String has not been assigned a value, by default it's value is null.
In the example below, 2 Strings are created (lines 1 and 2).
The 3rd statement concatenates (joins) the 2 Strings together (using the addition character).
The fourth statement returns the number of characters in the String called firstName, by calling the String length() function (all Strings have this function, it returns an int).
The fifth line finds the first character of the String by calling the String charAt() function (all Strings have this function, returns the character at the specified position).
The final statement concatenates all of the values together and prints to screen.
A variable, such as String, can be declared in one statement and assigned a value in another statement, as below:
String myString;
myString = "This is a String!";
...or it can be declared and assigned a value in a single statement:
String myString = "This is a String!";
Literal String values (the actual charcters) are always enclosed in double quotation marks.
See also: "Strings (The Java™ Tutorials > Learning the Java Language > Numbers and Strings)" ( http://bit.ly/K4kbDz )
"String (computer science) - Wikipedia, the free encyclopedia" ( http://bit.ly/AgRVuH )
Here's a cat:
A string is a collection of any keyboard characters such as "I love chocolate 100 times more than you !!!"
Where a String has not been assigned a value, by default it's value is null.
In the example below, 2 Strings are created (lines 1 and 2).
The 3rd statement concatenates (joins) the 2 Strings together (using the addition character).
The fourth statement returns the number of characters in the String called firstName, by calling the String length() function (all Strings have this function, it returns an int).
The fifth line finds the first character of the String by calling the String charAt() function (all Strings have this function, returns the character at the specified position).
The final statement concatenates all of the values together and prints to screen.
The output:
A variable, such as String, can be declared in one statement and assigned a value in another statement, as below:
String myString;
myString = "This is a String!";
...or it can be declared and assigned a value in a single statement:
String myString = "This is a String!";
Literal String values (the actual charcters) are always enclosed in double quotation marks.
See also: "Strings (The Java™ Tutorials > Learning the Java Language > Numbers and Strings)" ( http://bit.ly/K4kbDz )
"String (computer science) - Wikipedia, the free encyclopedia" ( http://bit.ly/AgRVuH )
Here's a cat:
Subscribe to:
Posts (Atom)



