Java String: valueOf() Method
valueOf() Method
Contents:
- public static String valueOf(char c)
- public static String valueOf(int i)
- public static String valueOf(long l)
- public static String valueOf(float f)
- public static String valueOf(boolean b)
- public static String valueOf(double d)
- public static String valueOf(char[] data)
- public static String valueOf(char[] data, int offset, int count)
- public static String valueOf(Object obj)
public static String valueOf(char c)
The valueOf() method is used to get the string representation of the char argument.
Java Platform: Java SE 8
Syntax:
valueOf(char c)
Parameters:
Name | Description | Type |
---|---|---|
C | a char. | char |
Return Value: A string of length 1 containing as its single character the argument c.
Return Value Type: char
Example: Java String valueOf(char c) Method
The following example shows the usage of java String() method.
Output:
P y t h o n
public static String valueOf(int i)
Returns the string representation of the int argument.
The representation is exactly the one returned by the Integer.toString method of one argument.
Java Platform: Java SE 8
Syntax:
valueOf(int i)
Parameters:
Name | Description | Type |
---|---|---|
i | an int. | int |
Return Value: A string representation of the int argument.
Return Value Type: int
Example: Java String valueOf(int i) Method
The following example shows the usage of java String() method.
Output:
1 50 -3
public static String valueOf(long l)
Returns the string representation of the long argument.
The representation is exactly the one returned by the Long.toString method of one argument.
Java Platform: Java SE 8
Syntax:
valueOf(long l)
Parameters:
Name | Description | Type |
---|---|---|
l | a long. | String |
Return Value: A string representation of the long argument.
Return Value Type: String
Example: Java String valueOf(long l) Method
The following example shows the usage of java String() method.
Output:
1 50 12345
public static String valueOf(float f)
Returns the string representation of the float argument. The representation is exactly the one returned by the Float.toString method of one argument.
Java Platform: Java SE 8
Syntax:
valueOf(float f)
Parameters:
Name | Description | Type |
---|---|---|
f | a float. | String |
Return Value: A string representation of the float argument.
Return Value Type: String
Example: Java String valueOf(float f) Method
The following example shows the usage of java String() method.
Output:
1.0 50.125 125.125
public static String valueOf(boolean b)
Returns the string representation of the boolean argument.
Java Platform: Java SE 8
Syntax:
valueOf(boolean b)
Parameters:
Name | Description | Type |
---|---|---|
b | a boolean. | String |
Return Value: If the argument is true, a string equal to "true" is returned; otherwise, a string equal to "false" is returned.
Return Value Type: boolean
Example: Java String valueOf(boolean b) Method
The following example shows the usage of java String() method.
Output:
true
public static String valueOf(double d)
Returns the string representation of the double argument.
The representation is exactly the one returned by the Double.toString method of one argument.
Java Platform: Java SE 8
Syntax:
valueOf(double d)
Parameters:
Name | Description | Type |
---|---|---|
d | a double. | String |
Return Value: A string representation of the double argument.
Return Value Type: String
Example: Java String valueOf(double d) Method
The following example shows the usage of java String() method.
Output:
1.0125 50.123125 23.125125
public static String valueOf(char[] data)
Returns the string representation of the char array argument.
The contents of the character array are copied; subsequent modification of the character array does not affect the returned string.
Java Platform: Java SE 8
Syntax:
valueOf(char[] data)
Parameters:
Name | Description | Type |
---|---|---|
data | the character array. | String |
Return Value: a String that contains the characters of the character array.
Return Value Type: String
Example: Java String valueOf(char[] data) Method
The following example shows the usage of java String() method.
Output:
mnop After Reinitialization: mnop
public static String valueOf(char[] data, int offset, int count)
Returns the string representation of a specific subarray of the char array argument.
The offset argument is the index of the first character of the subarray. The count argument specifies the length of the subarray. The contents of the subarray are copied; subsequent modification of the character array does not affect the returned string.
Java Platform: Java SE 8
Syntax:
valueOf(char[] data, int offset, int count)
Parameters:
Name | Description | Type |
---|---|---|
data | the character array. | String |
offset | initial offset of the subarray. | int |
count | the length of the subarray. | int |
Return Value: a String that contains the characters of the character array.
Return Value Type:
Throws:
IndexOutOfBoundsException - if offset is negative, or count is negative, or offset+count is larger than data.length.
Example: Java String valueOf(char[] data, int offset, int count) Method
The following example shows the usage of java String() method.
Output:
w3resource After Reinitialization: w3resource
Example of Throws: valueOf(char[] data, int offset, int count) Method
IndexOutOfBoundsException - if offset is negative, or count is negative, or offset+count is larger than data.length.
Let
int offset = -1; int count = 12;
in the above example.
Output:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String in dex out of range: -1 at java.lang.String.<init>(String.java:192) at java.lang.String.valueOf(String.java:3032) at StringValueOfExample.main(StringValueOfExample.java:12)
public static String valueOf(Object obj)
Returns the string representation of the Object argument.
Java Platform: Java SE 8
Syntax:
valueOf(Object obj)
Parameters:
Name | Description | Type |
---|---|---|
obj | an Object. | String |
Return Value: if the argument is null, then a string equal to "null"; otherwise, the value of obj.toString() is returned.
Return Value Type: String
The following example shows the usage of java String() method.
Output:
Value = Java String
Java Code Editor:
Previous:trim Method
Next:Garbage Collection in Java