Write a program to Convert any type of value to string value using String.valueOf() method


This Java code demonstrates how to convert variables of different data types into a string using the String.valueOf() method. The code declares four variables i, f, d, and b of integer, float, double, and boolean data types respectively. It then prints the string value of these variables using the String.valueOf() method.

The String.valueOf() method is a static method of the String class that takes a value of any data type and returns a string representation of that value. In this code, the method is used to convert the values of variables i, f, d, and b into their corresponding string representations.

The output of the code will display the string values of the variables i, f, d, and b. For example, the output for the integer variable i will be: "String Value of Int : 23". Similarly, the output for the float variable f will be: "String Value of Float : 78.54". The same applies to the double and boolean variables d and b respectively.

Source Code

class Convert_AnyType
{
	public static void main(String args[])
	{
		int i = 23;
		float f = 78.54f;
		double d = 1374.13d;
		boolean b = false;
		System.out.println("After Converting into String");
		System.out.println("String Value of Int : "+ String.valueOf(i));
		System.out.println("String Value of Float : "+ String.valueOf(f));
		System.out.println("String Value of Double : "+ String.valueOf(d));
		System.out.println("String Value of Boolean : "+ String.valueOf(b));  
	}
}

Output

After Converting into String
String Value of Int : 23
String Value of Float : 78.54
String Value of Double : 1374.13
String Value of Boolean : false

Example Programs