Parsing Java


String class plays an important role in Java coding. There are lot of string operations, a Programmer should be acquainted of. This Web site gives you all under the caption String and StringBuffer. One important manipulation on strings is Parsing Java where a string is parsed to data type.

Parsing operation converts string to primitive data type.

1. What is Parsing Java?

Converting string to data type is known as parsing in Java.

2. Why parsing is required?

Many a times, the user input is in string form. Many examples exist in Java. The command-line arguments are returned as string array. Reading from TextField (using getText()) is string. Reading from client HTML form by Servlet (using getParameter()) is string. This string value is good for printing but cannot be used in arithmetic operations. Here, parsing is required where string is converted to primitive data type.

3. Instead of parsing, can I do casting?

No casting cannot be done. Casting is possible between compatibles. For example, boolean cannot be converted into any other data type. We say, boolean is incompatible for conversion. Except boolean, all other 7 data types can be converted to one another either implicitly or explicitly. In the same way, String is incompatible to convert into any data type. It requires extra coding known as parsing.

3. Why it is called parsing and not something else?

It is because we use methods of type parseXXX() like parseInt(), parseDouble() and parseByte() etc. The operation is named as parsing. These methods are available in Wrapper classes.

Following Example on Parsing Java converts many strings into data types.
public class ParsingDemo
{
  public static void main(String args[])
  {                                        // PARSING STRING TO INT
    String str1 = "10";
    System.out.println("\nString str1 is " + str1);  //  printing is no problem
    // System.out.println(str1 * str1);              //  raises compilation error
    int x = Integer.parseInt(str1);
    System.out.println("int parsing: x * x is " + x*x);   

                                           // PARSING STRING TO DOUBLE
    String str2 = "10.5";
    double y = Double.parseDouble(str2);
    System.out.println("double parsing: y * y is " + y*y);   

                                           // PARSING STRING TO LONG
    String str3 = "45";
    long z = Long.parseLong(str3);
    System.out.println("long parsing: z * z is " + z*z);   
  }
}

Parsing JavaOutput Screenshot on Parsing Java

int x = Integer.parseInt(str1);
double y = Double.parseDouble(str2);
long z = Long.parseLong(str3);

paarseInt() is a method of Integer class that converts string into data type int form. Similarly parseDouble() and parseLong() are methods of Double and Long classes converting string to double and long data types. Integer, Double and Long are known as wrapper classes.

Refer more on Wrapper classes in detail at wrapper classes from java.lang package.

Following links give you the parsing operations of string to all data types.

1. converting string to boolean
2. converting string to byte
3. converting string to short
4. converting string to int
5. converting string to long
6. converting string to float
7. converting string to double
8. converting string to char

Leave a Comment

Your email address will not be published.