In this article we are going to discuss converting int (primitive type) and Object type (wrapper) Integer to String and vice versa. There are many ways to do it in Java.
How to convert int to string in Java
- Convert by adding an empty string.The easiest way to convert int to String is very simple. Just add to int or Integer an empty string "" and you’ll get your int as a String. It happens because adding int and String gives you a new String. That means if you have
int x = 5
, just definex + ""
and you’ll get your new String.Here is an example:The output is: - Object class is a root class in Java. That means every Java class is directly or indirectly inherited from the Object class and all Object class methods are available for all Java classes.Object has a special method
toString()
to represent any object as a string. So, every Java class inherits this method as well. However the good idea is to override this method in your own classes to have an appropriate result.The Integer class’toString()
method returns a String object representing the specified int or Integer parameter.Its Syntax:The method converts argument i and returns it as a string instance. If the number is negative, the sign will be kept.Example:You may use thetoString
method to convert an Integer (wrapper type) as well.The result is:You may use specialInteger.toString method toString(int i, int base)
that returns a string representation of the number i with the base base and than to String. For exampleThe syntax is:Here is an example:The output is a String binary representation of decimal number 255: - Convert int to String using String.valueOf(int)Method
String.valueOf(int)
returns the string representation of the int argument.The syntax of the method is:Here is an example of Java convert int to String usingString.valueOf(int)
:You may do the same with an Integer (wrapper type of int):The output will be: - Convert using DecimalFormat
java.text.DecimalFormat
is a class defined injava.text
package and a subclass ofNumberFormat
. It is used for formatting a decimal number to a string representing following a certain pattern. We can use it for Integers as well.Example:The output is: - Convert using String.format()
String.format()
is one more way to convert an Integer to String Object.SyntaxExampleThe output is:
How to convert String to int in Java
Here is a couple of ways to convert string to int, Java lets do it usingparseInt()
and valueOf()
methods.
- Java string to int using Integer.parseInt(String)
parseInt
is a static method of the Integer class that returns an integer object representing the specifiedString
parameter.Syntax:OrWherestr
is a String you need to convert andradix
is a base of the parced numberConverting String to Integer, Java example using parseInt()The output is:Here we’ve got 111 in the first variant and 7 for the second. 7 is a decimal form of binary 111. - Convert using Integer.valueOf(String)
Integer.valueOf(String s)
is a Java method of Integer class. It returns an Integer decimal interpretation of a String object so it is used to convert String to Integer.Syntax:Output:
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.