converting integer to string in java

7 Different Ways To Convert Int To String In Java

In this post, we are going to learn How to convert an int to a string in java in 7 ways. In Competitive Programming conversion of Int to string or string to Int is widely used.

So, without wasting your time these are the 7 ways

Different Ways Of Converting Int Into String

  • Using String.valueOf()
  • By creating Integer class object
  • Using Integer.toString()
  • With the help of StringBuffer
  • Using StringBuilder
  • String.format() function
  • Concatenating with empty string
Different ways of converting integer to string

Using String.valueOf()

With the help of String.valueOf() you can convert any Integer or double to String. This function takes single argument which you need to convert into string. This is the most used in Competitive programming

If you passed negative number inside the String.valueOf() the sign will be preserved. Let’s see some basic code to convert integers into string

Example – 1

class GeeksToCode
{ 
public static void main(String args[]) 
{ 
	int c = 12; 
	String str = String.valueOf(c); 
	System.out.println("String is = " + str); 
} 
} 

A very simple program. We pass integer and received a string

Output

String is = 12

Example – 2

class GeeksToCode
{ 
public static void main(String args[]) 
{ 
	double c = 12.3; 
	String str = String.valueOf(c); 
	System.out.println("String is = " + str); 
} 

Here I passed a double to String.valueOf() and we again received a string

Output

String is = 12.3

These are all format of String.valueOf() function given below. You must try to use all functions once and see the result.

public static String valueOf(boolean b): Used for converting boolean value to a String
public static String valueOf(char c): char to String
public static String valueOf(int i): int to String
public static String valueOf(long l): long to String
public static String valueOf(float f): float to String
public static String valueOf(double d): double to String

Example – 3

In given example we used almost all function of String.valueOf()

class GeeksToCode
{ 
public static void main(String args[]) 
{ 
	double c = 12.3; 
	String str = String.valueOf(c); 
	System.out.println("String is = " + str); 
    str = String.valueOf(true);
  System.out.println("String is = " + str); 
   str = String.valueOf('a');
  System.out.println("String is = " + str); 
   str = String.valueOf(8.12144f);
  System.out.println("String is = " + str); 
  
} 
} 

Output

String is = 12.3
String is = true
String is = a
String is = 8.12144

By Creating Integer Class Object

In this method, first, we create an object of Integer class by passing the integer as a constructor and then call the toString() method. toString() did not take any argument

It is an inbuilt method which returns String object, remember It return String Object. Let’s see some basic code to convert integers into string

Example

class Geekstocode
{ 
public static void main(String args[]) 
{ 
	int i = 9998; 
	Integer obj = new Integer(i); 
	String str = obj.toString(); 
	System.out.println("String is = " + str); 
} 
} 

Note: This method is depreciated now in New java version

Advantage

You can pass two arguments to toString(int a, int b). The first parameter denotes the number you want to cast and the other is the base that to be used on converting to String and return the String with a specified base.

// Java program to illustrate the 
// toString(int, int) Method 
import java.lang.*; 

public class GeeksToCode{ 

public static void main(String[] args) { 

		Integer i = new Integer(10); 
	
	// It returns a string representation 
		// in base 2 
	String returnvalue = i.toString(52, 2); 
	System.out.println("String Value = " + returnvalue); 

	// It returns a string representation 
		// in base 8 
	returnvalue = i.toString(35, 8); 
	System.out.println("String Value = " + returnvalue); 

	
} 
} 

Disadvantage

You can not convert Floating or double using this method. When you run using double you get the error: possibly lossy conversion from double to int

error: no suitable constructor found for Integer(double)
Integer obj3 = new Integer(10.23);
^
constructor Integer.Integer(int) is not applicable
(argument mismatch; possible lossy conversion from double to int)
constructor Integer.Integer(String) is not applicable
(argument mismatch; double cannot be converted to String)
Note: prog.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
1 error

Using Integer.toString()

We have another option for converting integer to String object with help of Integer Wrapper class. The Integer wrapper class has a static method which takes integer as argument and returns a string object.

If the number is negative then the sign will be preserved.

Example

class GeeksToCode 
{ 
public static void main(String args[]) 
{ 
	int a = 12; 
	int b = -34; 
	String str1 = Integer.toString(a); 
	String str2 = Integer.toString(b); 
	System.out.println("String str1 = " + str1); 
	System.out.println("String str2 = " + str2); 
} 
} 

Output

String str1 = 12
String str2 = -34

Passing a floating-point or double is not allowed. If you try to pass the double you will get Error: Possible lossy conversion from double to int

With The Help of stringBuffer

StringBuffer is one of the most used class in java. It is used to concatenate two strings. StringBuffer is similar to StringBuilder but StringBuffer is thread-safe. The best part of this method is that you can convert any double or float to String. Let’s see some basic code to convert integers into string

Example

class GeeksToCode
{ 
public static void main(String args[]) 
{ 
	int f = 134; 
	StringBuffer sb = new StringBuffer(); 
	sb.append(f); 
	String str = sb.toString(); 
	System.out.println("String is = " + str); 
} 
} 

We created a simple StringBuffer object and then append the integer to StringBuffer and later used toString() to get StringObject.

Output

String is = 134

With The Help Of StringBuilder

The StringBuilder in java is almost similar to StringBuffer because both of them are alternative of String Class. However, the basic difference is that StringBuilder does not support synchronization whereas StringBuffer does.

Let’s see some basic code to convert integers into string

Example

class GeeksToCode 
{ 
public static void main(String args[]) 
{ 
	int g = 14; 
	StringBuilder sb = new StringBuilder(); 
	sb.append(g); 
	String str = sb.toString(); 
	System.out.println("String str = " + str); 
} 
} 

The working of this is same as StringBuffer as discussed above.

Output

String str = 14

Using String.format()

This method is used for formatting the given input into String. This function is introduced after JDK 1.5

Let’s see some basic code to convert integers into string

Example

public class GeeksToCode{  
public static void main(String args[]){  
int i=20;  
String s=String.format("%d",i);  
System.out.println(s);  
}} 

This simply convert and produce output 20.

Output

20

Concatenate With Empty String

This is the very simple step to convert Integer or any other data types to string. You only need to concatenate with the empty string and it all done.

String str = "" + 123;

Example

public class GeeksToCode{
  public static void main(String[] args){
    
    Integer i = new Integer(123);

    System.out.println("Before conversion: " + i.getClass().getName());
    
    System.out.println("After conversion: " + ("" + i).getClass().getName());
  }
}

Output

Before conversion: java.lang.Integer
After conversion: java.lang.String

Which Method is best?

As I discussed the 7 different ways of converting Integer to String, but which is the best and you must use.

String.valueOf() is only the best way of converting the Integer to String because of Overloading. The advantage of String.valueOf() are given below

  1. This method is Overloaded for Float, long and double which means it provides a consistent way to convert a number to String in Java
  2. The valueOf() function is almost present in every class

This is all we know about Convert int to string in java. Comment your doubts here.

Leave a Comment

Your email address will not be published. Required fields are marked *