How to convert a Java Integer to String Data?

How to convert a Java Integer to String Data?, In this tutorial you will learn how to convert Java Integer value to String data.

How to convert a Java Integer to String Data?

Java Integer to String Data Conversion - How to convert a Java Integer to String Data?

In this tutorial I will provide example programs to convert Integer value to String data in Java program. Property converting data types in Java is very important to keep the data integrity intact. So, for the Java programmers understanding the data conversion process is very important and they should understand this very well.

In Java coding an Integer data represents numbers. For Integer conversion to Strings data there are several ways available. Here I will show you these methods of Integer data conversion to String. The most common way is to use the static method valueof() of String class. Static methods are class specific methods, it doesn't need a class object instance for calling. With just the class name any static method can be called. Here is the syntax for calling a static method.

ClassName.methodName(arguments).

Static method of String class returns String data output of its arguments. In the below code the method is invoked with just the class name String.


package net.roseindia;

class IntegerToStringValueOf {
	public static void main(String[] args) {

		int totalMembers = 66;
		String totalMembersValue = 
				String.valueOf(totalMembers);
		System.out.println("String value representation of Integers :" 
				+ totalMembersValue);
	}
}


After successful compilation and running of the program, following output comes on the console:

How to convert a Java Integer to String Data

There is another method called toString which does the same job as valueOf() method. The code below shows the invocation process of this method. You can use the toString() method of String class.



package net.roseindia;

public class IntegertoStringToStringMethod {
	public static void main(String[] args) {

		int integerValue = 108;
		String stringValue = Integer.toString(integerValue);
		System.out.println("String representation of Integer Value : "
		+ stringValue);
	}
}


Following output is displayed after compilation

How to convert a Java Integer to String Data

Inspite of the two functions invoked above, string representation of a integer value can achieved with simply String concatenation. Here is how the concatenation is done, in the code below


package net.roseindia;

public class IntegertoStringWithConcatenation {
	public static void main(String[] args) {
		int integerValue = 99999;
		String stringValue = " " + integerValue;
		System.out.println("String representation of Integer Value :" 
		+ stringValue);
	}

}

Here is the output display after compilation

How to convert a Java Integer to String Data

Related Tutorials: