Parsing in Java

Parsing in java is the way of conversion of one type of data to another type of data. In java the most common parsing conversions are :

  1. Parsing string to integer.
  2. Parsing string to long type.
  3. Parsing string to double type.

Let us look at a code to look at above parsing ways :

public class Parsing {

	public static void main(String[] args) {

		String str1 = "123";
		int integerNum = Integer.parseInt(str1);
		System.out.println("Integer value is " + integerNum);

		String str2 = "24.5";
		double doubleNum = Double.parseDouble(str2);
		System.out.println("Double value is " + doubleNum);

		String str3 = "993774832332";
		long longNum = Long.parseLong(str3);
		System.out.println("Long value is " + longNum);

	}
}

Output

Integer value is 123
Double value is 24.5
Long value is 993774832332

In the above code you can see that parseInt() is used to convert String to int. Similarly parseDouble() is used to parse String to double.

Methods to parse String to other data types are :

ParsingMethod
String -> IntegerInteger.parseInt()
String -> DoubleDouble.parseDouble()
String -> LongLong.parseLong()
String -> ByteByte.parseByte()
String -> FloatFloat.parseFloat()
String -> ShortShort.parseShort()
String -> BooleanBoolean.parseBoolean()

Leave a Comment