How to do conversion between a String and a Number in Java

We generally struck while handling values obtained from various sources like reading a value from a File, reading inputs from Web Forms etc, as we get only String values from there and we need to convert them to a Number( Byte, Short, Integer, Double, Float, Long – These 6 classes are also known as Wrapper Classes for Numbers) so that we can perform any Arithmatic or other operations on them if we want to.

For example- In a shopping portal when a person enters the quantity of the product it is recieved as a String and we need to convert it into Integer object or int(a primitive data type) so as to multiply it to price of single item.

strings-in-java
strings-in-java

Similarly we need to convert String into other subclasses of Number Class which are Byte, Short, Integer, Double, Float, Long. and in the other situations we need to convert objects of these subclasses into a String object while storing the data in a File etc.

  1. Convert String into Number.

  2. Convert Number into String.

Convert String into Number

In this we will use three methods that are defined in all the Wrapper classes.

  • valueOf() – Converts String into destination Wrapper class object. This method Throws NumberFormatException if conversion is not possible.
  • XXXValue() – Converts Wrapper class object into its primitive data type. It could also be done automatically by unboxing. So even if we don’t use this method, our program will work fine (Replace XXX with  float, int, byte etc.)
  • parseXXX() – Directly converts String into primitive data type of destination class. This method Throws NumberFormatException if conversion is not possible.(Replace XXX with  Float, Int, Byte etc.).

Lets see an example now on Converting String to Integer and you can convert String to any other Wrapper class using the same methods..

package codingeekStringTutorials;

public class StringToNumber {

	public static void main(String[] args) {
		String input = "3267575";

		// Conversion in Integer Wrapper class
		Integer intObject1 = Integer.valueOf(input);
		Integer intObject2 = Integer.valueOf(input).intValue(); //AutoBoxing
		Integer intObject3 = Integer.parseInt(input); //AutoBoxing

		// Conversion in int primitive data type
		int intprimitive1 = Integer.valueOf(input); //UnBoxing
		int intprimitive2 = Integer.valueOf(input).intValue();
		int intprimitive3 = Integer.parseInt(input);

		System.out.println("Wrapper class objects -> " + intObject1 + ", " + intObject2 + ", " + intObject3);
		System.out.println("Primitive data type -> " + intprimitive1 + ", " + intprimitive2 + ", " + intprimitive3);
	}

}
Output:-
Wrapper class objects -> 3267575, 3267575, 3267575
Primitive data type -> 3267575, 3267575, 3267575

Convert Number into String

Now this work of converting a number to a String object is I think much easier than what we have discussed till now.

Various methods we can use for this purpose are…

  • Use ‘+’ operator to concatenate any Number to an empty String
    String abc = “” + 123;
  • Use String.valueOf(Number) method to convert any Number to String
    String abc = String.valueOf(123);
  • Use .toString() method of any Wrapper class.
    String abc = Integer.toString(123);
  • Use StringBuilder or StringBuffer
    StringBuffer abc = new StringBuffer().append(123);

Recommended – Click here to read more about StringBuffer and StringBuilder

public class NumberToString {

	public static void main(String[] args) {

		Integer number = 123;

		String s1 = "" + number;		// Concatenation method
		String s2 = Integer.toString(number);		//Using Wrapper class method
		String s3 = String.valueOf(number);			//Using String Class method
		StringBuffer s4 = new StringBuffer().append(number);		//Using StringBuffer
		StringBuilder s5 = new StringBuilder().append(number);		//Using StringBuffer

		System.out.println("Number after converting into Strings ");
		System.out.println("s1 -> " + s1);
		System.out.println("s2 -> " + s2);
		System.out.println("s3 -> " + s3);
		System.out.println("s4 -> " + s4);
		System.out.println("s5 -> " + s5);
	}
}
Output:-
Number after converting into Strings 
s1 -> 123
s2 -> 123
s3 -> 123
s4 -> 123
s5 -> 123

=> In the above example we show conversion of only Integer but you can do for all other Wrapper classes using the same methods.

Recommended -

Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
kavya tg
kavya tg
6 years ago

Parsing Methods in Java Convert String to Int
In Java there are two methods to convert String to Int, they are.

Integer.parseInt()
Integer.valueOf()
1. Convert String to Int using Integer.parseInt()
The Integer.parseInt() is a static method of Integer class used in java to converts string to primitive integer.

Example

String ‘100’ to primitive integer 100

Note:Parsing is a process of converting one data type to another. For example String to integer.
For codding it best way to convert string to int i hope this may be use full.

1
0
Would love your thoughts, please comment.x
()
x
Index