java

How to convert an integer to a string in Java

In this tutorial, we are going to see different ways to convert an integer to a string in Java.

  • By using String.valueOf()
  • By using toString()

 

Method 1: By using String.valueOf()

Example to convert Integer to String using the String.valueOf() method.

public class Main {
    public static void main(String[] args) {
		Integer n = 25;
		String str = String.valueOf(n);
		System.out.println(str);
    }
}

Output:

25

 

 
Example to convert int to String using the String.valueOf() method.

public class Main {
    public static void main(String[] args) {
		int n = 25;
		String str = String.valueOf(n);
		System.out.println(str);
    }
}

Output:

25

 

Method 2 : By using toString()

Example to convert Integer to String using the toString() method.

public class Main {
    public static void main(String[] args) {
		Integer n = 25;
		String str = n.toString();
		System.out.println(str);
    }
}

Output:

25

 

 
Example to convert int to String using the toString() method.

public class Main {
    public static void main(String[] args) {
		int n = 25;
		String str = Integer.toString(n);
		System.out.println(str);
    }
}

Output:

25
mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

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