HOME
> System software
> Java: Convert Integer To String - Here's How
Java: Convert integer to string - here's how
Both integer and string are data types that you will encounter more often when working with Java. If you have an integer variable that you want to convert to a variable of type string, this can be done in different ways.
This is the function of integer and string in Java
If you use Java for programming, you will encounter different data types. In addition to eight primitive or simple data types, which include the type integer, Java also has complex data types, which include all string variables. While a variable of the type integer can only contain an integer from a certain value range, a string consists of a character string that contains a sequence of letters, digits or special characters.How to convert the type of a variable from integer to string
If you have a variable of the type integer, you can easily convert it into a string. There are three different methods available in Java for this purpose.- On the one hand, the conversion of an int value is possible with the help of the "valueOf()" command. To do this in Java, simply type the command "String s = String.valueOf(i);" and replace the placeholders "i" and "s" with the name of your int variable and the name of the new string variable, respectively.
- Alternatively, you can also make the conversion to a string by using the "toString()" method and typing the command "s= Interger.toString(i);" into the Java console.
- Even faster works the conversion of an integer variable by using a plus operator. To do this, run the command "s= "" +i;" to append the int value to an empty string and generate a string this way.