Converting variables to String

20 January 2012 By Nithya Vasudevan 694 views No Comment
0 Flares Twitter 0 Facebook 0 Google+ 0 0 Flares ×

Converting primitive to String using valueOf()

The String class includes a set of static valueOf() methods that can be used to create strings from other Java objects and primitive types such as int, byte, char, double, boolean, etc.

MethodPurpose
valueOf(char)Returns a string containing the one specified character.
valueOf(char[])Returns a string with the same text as the specified character array.
valueOf(char[], int offset, int count)Returns a string with the same text as a subset of the specified character array.
valueOf(boolean)Returns a string containing either true or false.
valueOf(int)Returns a string containing the value of the int.
valueOf(long)Returns a string containing the value of the long.
valueOf(float)Returns a string containing the value of the float.
valueOf(double)Returns a string containing the value of the double.
valueOf(Object)If the argument is null, then a string equal to “null”; otherwise, the value of obj.toString() is returned.

Each of these methods is passed the variable to convert and returns a string representation of that variable. Because these methods are static, they are invoked using the name of the class rather than the name of an instance of the class.

Converting char to String

String charStr = String.valueOf(‘R’);

This method returns a string of length 1 containing as its single character argument.

Converting char[] to String

char charArray[] = {'A', 'r', 'r', 'a', 'y' };
String arrayStr1 = String.valueOf(charArray);
System.out.println(arrayStr1);//Array
	
String arrayStr2 = String.valueOf(charArray, 2, 3);
System.out.println(arrayStr2);//ray

Converting int to String

int i = 100;
String intStr = String.valueOf(i);

Converting long to String

long l = 1000988444;
String longStr = String.valueOf(l);

Converting boolean to String

String boolStr = String.valueOf(true);

Converting float to String

String floatStr = String.valueOf(98.6F);

Converting double to String

String doubleStr = String.valueOf(3.1416D);

Summary of the above is given below

package com.ibytecode.strings.methods;
public class VariablesToString {
	public static void main(String[] args) {
		int i = 100;
		String intStr = String.valueOf(i);
		System.out.println(intStr);
		
		long l = 1000988444;
		String longStr = String.valueOf(l);
		System.out.println(longStr);
		
		String floatStr = String.valueOf(98.6F);
		System.out.println(floatStr);
		
		String doubleStr = String.valueOf(3.1416D);
		System.out.println(doubleStr);
		
		String boolStr = String.valueOf(true);
		System.out.println(boolStr);
		
		String charStr = String.valueOf('R');
		System.out.println(charStr);
		
		char charArray[] = {'A', 'r', 'r', 'a', 'y' };
		String arrayStr1 = String.valueOf(charArray);
		System.out.println(arrayStr1);
		
		String arrayStr2 = String.valueOf(charArray, 2, 3);
		System.out.println(arrayStr2);
	}
}

100
1000988444
98.6
3.1416
true
R
Array
ray

Converting Object to String

package com.ibytecode.strings.methods;
class Person
{
	private int age;
	private String name;
	public Person(int age, String name)
	{
		this.age = age;
		this.name = name;
	}
	@Override
	public String toString() {
		return "Person [age=" + age + ", name=" + name + "]";
	}	
}
public class VariablesToString {
	public static void main(String[] args) {
		Person person = new Person(10, "Kumar");
		//Calls the toString() of Person class
		String objStr = String.valueOf(person);
		System.out.println(objStr);
	}
}

Person [age=10, name=Kumar]

If you want to convert object to String then that class must override toString() method. When the object is passed to the valueOf() method it automatically invokes the toString() method.

Converting primitive to String using plus (+) operator

Alternate and most straight forward way to convert primitive to String is to use string concatenation operator ‘+’ (plus operator).

package com.ibytecode.strings.methods;
class Person
{
	private int age;
	private String name;
	public Person(int age, String name)
	{
		this.age = age;
		this.name = name;
	}
	@Override
	public String toString() {
		return "Person [age=" + age + ", name=" + name + "]";
	}	
}
public class PrimitiveToString {
	public static void main(String[] args) {
	// Using + operator
	String s2 = "";
	
	s2 = ""+true;                         // true
	s2 = ""+((byte)0x12);                 // 18
	s2 = ""+((byte)0xFF);                 // -1
	s2 = ""+'a';                          // a
	s2 = ""+((short)123);                 // 123
	s2 = ""+123;                          // 123
	s2 = ""+123L;                         // 123
	s2 = ""+1.23F;                        // 1.23
	s2 = ""+1.23D;                        // 1.23
	System.out.println(s2);

	Person person = new Person(10, "Kumar");
	String s = person + "";
	System.out.println("S = " + s);	
	}
}

1.23
S = Person [age=10, name=Kumar]

Only last value of s2 is printed. If you use += operator then all the primitive values will be concatenated with the string.

Tags: