tags:

views:

560

answers:

3

Hi Just wondering what exactly is a Parseing or parsing? especially related to Java. Why are they used?

for example: Integer.parseInt, String parse.String etc

Thank you

+4  A: 

From dictionary.reference.com:

Computers. to analyze (a string of characters) in order to associate groups of characters with the syntactic units of the underlying grammar.

The context of the definition is the translation of program text or a language in the general sense into its component parts with respect to a defined grammar -- turning program text into code. In the context of a particular language keyword, though, it generally means to convert the string value of a fundamental data type into an internal representation of that data type. For example, the string "10" becomes the number (integer) 10.

tvanfosson
+8  A: 

Parsing is to read the value of one object to convert it to another type. For example you may have a string with a value of "10". Internally that string contains the Unicode characters '1' and '0' not the actual number 10. The method Integer.parseInt takes that string value and returns a real number.

String tenString = "10"

//This won't work since you can't add an integer and a string
Integer result = 20 + tenString;

//This will set result to 30
Integer result = 20 + Integer.parseInt(tenString);
Martin Harris
+9  A: 

You could say that parse is analyze a string to find tokens, or items and then create an structure from the result.

In your example, parsing is to analyzing a string to find integers ( Integer.parseInt )

So, if you have:

String someString = "123";

And then you invoke:

int i = Integer.parseInt( someString );

You're telling java to analyze the "123" string and find an integer there.

Other example:

One of the actions the java compiler does, when it compiles your source code is to "parse" your .java file and create tokens that match the java grammar.

When you fail to write the source code properly ( for instance forget to add a ";" at the end of a statement ), is the parser who identify the error.

Here's more information: http://en.wikipedia.org/wiki/Parse

OscarRyz