Java Convert int to string




Java Tutorial for Beginners
Java Tutorial for Beginners

Basically there are two ways of converting int to string in Java. Both are listed below.

1. Java int to String using Integer.toString()

public static String toString(int i) Returns a String object representing the specified integer. The argument is converted to signed decimal representation and returned as a string, exactly as if the argument and radix 10 were given as arguments to the toString(int, int) method.

public class ConvertIntToString {
  public static void main(String[] args) {
    
    int mInt = 1556;
    
    String string = Integer.toString(mInt);
    System.out.println(string);
  }
}
output :
1556

2. Java int to String using String.valueOf()

public static String valueOf(int i) Returns the string representation of the int argument. The representation is exactly the one returned by the Integer.toString method of one argument.

/*
Java int to String using String.valueOf() with Example
*/

public class ConvertIntToString {
    public static void main(String args[]){
        int i=5555;
        String s=String.valueOf(i);
        System.out.println(i+200); // result = 5755 because + is binary plus operator
        System.out.println(s+200);// result = 5555200 because + is string concatenation operator
    }}

/*
Output:
5755
5555200

*/

 





Partner Sites

VideoToGifs.com

EasyOnlineConverter.com

SqliteTutorials.com





Be the first to comment

Leave a Reply

Your email address will not be published.


*