String valueof() in java

This method is used to converts different types of values into a string. You can convert different type of data value into String data value. The String valueOf() java method is overloaded in the String class.

String valueof() in java

By use of valueOf() method of String class we can do following conversions:

  • Java int to string
  • Java long to string
  • java boolean to string
  • char to string java
  • float to string java
  • java double to string
  • convert char array to string

It returns a String type value. As we said it is the overload method in the String class and it can accept different types of values as a parameter. Let’s see the String valueof() in java example

String.valueOf(DataType value);
class ExampleValueOf
{  
  public static void main(String args[])
  {  
	int intValue = 1;
	float floatValue = 1.1f;
	double doubleValue = 1.2;
	char charValue = 'a';
	char charArray[] = {'J','A','V', 'A'};
	boolean booleanValue = true;
			
       System.out.println("Converting int to String = "+ String.valueOf(intValue));
       System.out.println("Converting float to String = "+ String.valueOf(floatValue));
       System.out.println("Converting double to String = "+ String.valueOf(doubleValue));
       System.out.println("Converting char to String = "+ String.valueOf(charValue));
       System.out.println("Converting char Array to String = "+ String.valueOf(charArray));
       System.out.println("Converting boolean  to String = "+ String.valueOf(booleanValue));
			
   }  
}  

Output:
Converting int to String = 1
Converting float to String = 1.1
Converting double to String = 1.2
Converting char to String = a
Converting char Array to String = JAVA
Converting boolean  to String = true

Leave a Comment