w3resource

Java String: valueOf() Method

valueOf() Method

Contents:

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.

public class StringValueOfChar {

	public static void main(String[] args) {

		// declare a sample string value
		String strValue = "Python";
		// convert strValue to character array
		char[] values = strValue.toCharArray();
		// for each element on char array
		for(char c:values){
			// print the String value of char c
			System.out.println(String.valueOf(c));
		}

	}

}

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.

public class StringValueOfInteger {

	public static void main(String[] args) {

		int val = 1;
		Integer i = 50;
		System.out.println(String.valueOf(val));
		System.out.println(String.valueOf(i));
		System.out.println(String.valueOf(-3));
	}

}

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.

public class StringValueOfLong {

	public static void main(String[] args) {

		// primitive long declaration
		long val = 1;
		// Object of Long data type
		Long valueLong = 50l;
		System.out.println(String.valueOf(val));
		System.out.println(String.valueOf(valueLong));
		System.out.println(String.valueOf(12345));
	}

}

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.

public class StringValueOfFloat {

	public static void main(String[] args) {

		// primitive float declaration
		float val = 1.0f;
		// Object of Float data type
		Float valueFloat = 50.125f;
		System.out.println(String.valueOf(val));
		System.out.println(String.valueOf(valueFloat));
		System.out.println(String.valueOf(125.125f));
	}

}

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.

public class StringValueOfBoolean {

	public static void main(String[] args) {

		// declare a sample string value
		String strValue = " John Gilbert-Male-UK";
		// check if String value contains a specific string
		boolean bool = strValue.contains("Male");
		// print the string equivalent of our boolean check
		System.out.println(String.valueOf(bool));
	}

}

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.

public class StringValueOfFloat {

	public static void main(String[] args) {

		// primitive double declaration
		double val = 1.0123;
		// Object of Double data type
		Double valueDouble = 50.123125;
		System.out.println(String.valueOf(val));
		System.out.println(String.valueOf(valueDouble));
		System.out.println(String.valueOf(23.125125));
	}

}

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.

public class StringValueOfExample {

	public static void main(String[] args) {
		// declare the character array
		char[] data = new char[]{'m','n','o','p'};
		// getting the String equivalent of char array data
		String strVal = String.valueOf(data);
		// printing the String equivalent of char array
		System.out.println(strVal);

		// Change the value of char array
		// Convert a string to char array
		data = "test".toCharArray();
		// check if the strVal has been changed by
		// Reinitializing the data array
		System.out.println("After Reinitialization: "+strVal);

	}

}

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.

public class StringValueOfExample {
 
	public static void main(String[] args) {
		// declare the character array
		char[] data = new char[]{'w','3','r','e','s','o','u','r','c','e'};
		// declaring the index of our char array to start
		int offset = 0;
		// number of characters we are getting on the character array
		// starting from offset argument
		int count = 10;
		// getting the String equivalent of char array data
		String strVal = String.valueOf(data,offset,count);
		// printing the String equivalent of char array
		System.out.println();
		System.out.println(strVal);
		// change the value of char array
		// convert a string to char array
		data = "test".toCharArray();
		// check if the strVal has been changed by
		// reinitializing the data array
		System.out.println("After Reinitialization: "+strVal);
        System.out.println();
	}
 
}

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.

import java.lang.*;

public class StringExample {

   public static void main(String[] args) {
  
   String str = "Java String";
   Object objValue= str;
   System.out.println();
   // returns the string representation of the Object argument
   System.out.println("Value = " + str.valueOf(objValue));
   System.out.println();
   }
}

Output:

Value = Java String 

Java Code Editor:

Previous:trim Method
Next:Garbage Collection in Java



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/java-tutorial/string/string_valueof.php