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 */
- Java Simple Programs And Examples
- Java Example – Java Hello World Example
- Java Example – Math and Arithmetic Operators in Java
- Java Example – Variables and Types in Java
- Java Example – Scanner class and Getting User Input using Java
- Java Example – The If-Else If Statement, Nested If Statements, Logical Operators
- Java Example – Arrays in Java Example
- Java Example – ListIterator in Java Example
- Java Example – How to get current timestamp using Java
- Java Example – HashSet in Java with Example
- Java Example – How to read file in Java using BufferedReader
- Java Example – How to get Current Directory in Linux/Windows
- Java Example – Program to reverse an array or string
- Java Example – Sort String Array
- Java Example – Comparing two strings
- Java Example – String concatenation (join strings)
- Java Example – Java String Contains example
- Java Conversion
- Java Example – Convert String to int
- Java Example – Convert Date to String
- Java Example – Convert String to Character Array
- Java Example – Convert into string
- Java Example – Convert ArrayList to String Array
- Java Example – Convert Char Array To String
- Java Example – String Array To List
- Java Sorting algorithms & Techniques
- Java Example – Bubble Sort Algorithm
- Java Example – Insertion Sort Algorithm
- Java Example – Selection Sort Algorithm
- Java Example – Quick Sort Algorithm
- Java Example – Merge Sort Algorithm
- Java Handling Files
- Java I/O – Check If File Path Absolute or not
- Java I/O – Get file name and file path
- Java I/O – Get parent directory of the file or directory
- Java I/O – How to write to a file using BufferedWriter
- Java I/O – How to write to a file using FileOutputStream
- Java I/O – Check file permission and Set file permission
- Java I/O – Read File Using Java BufferedInputStream
- Java I/O – How to create a file in Java
Leave a Reply