int to String Java


Converting int to sting and also string to int required very often in coding. To do the job, Java comes with valueOf() method. valueOf() is overloaded that can take any data type or object as parameter and converts.

The other way is converting primitive data types like int, double, boolean etc. into string form. Sometimes, it is also required in Java; that is, just opposite of parsing.

For this, String API (java.lang.String methods are known as String API) gives a static method – valueOf(). A full program is given on this topic earlier – Converting Data types into Strings.

Anyhow, in the following program, valueOf() is used to convert data type int to string .

public class Demo
{
  public static void main(String args[])
  {                                           // first and general style
    int marks = 50;                           // marks is in int form

    String marks1 = String.valueOf(marks);    // conversion int to String
    System.out.println("\nFirst style marks in string form: " + marks1);
    // int k = marks1 * marks1;               // raises error as marks1 is in string form
                                              // second and less used style
    String marks2 = ""+marks;
    System.out.println("Second style marks in string form: " + marks2);
  }
}


int to String
Output screenshot on int to String Java

In the second style, marks variable is concatenated to an empty string. Anything in Java, when concatenated to string becomes a string.

In the first style, we used valueOf() method. It is a static method of String class which can convert any data type to string. This method is also capable to convert any object to string. A complete program on overloaded valueOf() method is available at Converting Data types into Strings.

Anyhow, you can use the following links also:

1. byte to stirng
2. short to stirng
3. int to stirng
4. long to stirng
5. float to stirng
6. double to stirng
7. char to stirng
8. boolean to stirng

============================================================

Your one stop destination for all data type conversions

byte TO short int long float double char boolean
short TO byte int long float double char boolean
int TO byte short long float double char boolean
float TO byte short int long double char boolean
double TO byte short int long float char boolean
char TO byte short int long float double boolean
boolean TO byte short int long float double char

String and data type conversions

String TO byte short int long float double char boolean
byte short int long float double char boolean TO String

Leave a Comment

Your email address will not be published.