Java int in string convertion

www.‮ual‬tturi.com
Java int in string convertion

In Java, you can convert an int value to a String using the valueOf() method of the String class. This method takes an int value as an argument and returns a String representation of the value.

Here is an example of how to convert an int to a String in Java:

int number = 42;
String str = String.valueOf(number);  // str is "42"

You can also use the Integer.toString() method to achieve the same result:

int number = 42;
String str = Integer.toString(number);  // str is "42"

Both of these methods return a new String object that contains the String representation of the int value.

Alternatively, you can use string concatenation to convert an int to a String. For example:

int number = 42;
String str = "" + number;  // str is "42"

This method involves creating a new String object for each concatenation, so it is not as efficient as the other methods.

For more information on converting data types in Java, you can refer to the Java documentation.

Created Time:2017-11-03 23:27:13  Author:lautturi