This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Casting Versus Parsing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Casting Versus Parsing" Watch "Casting Versus Parsing" New topic
Author

Casting Versus Parsing

zoheb hassan
Ranch Hand

Joined: Apr 01, 2009
Posts: 152

Hi Guys can anyone please explain to me What exactly is the difference between Parsing and casting because they seem very similar to me as they involve tryin to match incompatible Data types
Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

well there are some things that can't be cast to another type.

if you are trying to get an integer from a string for instance

Casting:


Parsing:


Hope this helped.
Hunter.


"If the facts don't fit the theory, get new facts" --Albert Einstein
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38517
    
  23
They are totally different.
Casting means taking a variable of one type and turning it to another type. There are obviously some combinations which can be cast, and some which cannot.
Parsing means reading text and deciding what the different parts of it mean. In the case of methods like Integer#parseInt(String) it needs to work out what the whole of the text means.
Amit Ghorpade
Bartender

Joined: Jun 06, 2007
Posts: 2716
    
    6

In addition to what people said above, parsing is no way related to casting, the Integer. parseInt() is the point of confusion.
That method is supposed to parse the input string for the presence of numbers and return an integer corresponding to the value of the string.
Simply "1234" will return int 1234. Returns an error (throws exception)if input is "123abc".

But this does not mean that the input string is cast to an int, no such cast is possible. Similar behavior can be observed for Wrapper class constructors.

Parsing, simply put is scanning the given string to look for/get known patterns which are usually substring(s) of the given string.
Eg finding number of occurrences of the string "abc" in "abcdefabcdabc" would require parsing if done by hand.


SCJP, SCWCD.
|Asking Good Questions|
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11256
    
  16

well... when you cast something, you don't REALLY change what it is. the thing on the heap will always be what it is.

But when you cast it, you tell the compiler "I'm going to pretend this object is this other type, and I really do know what I'm doing". for example, if class B extends A {}, I can make a collection that holds objects of type 'A'.

I can put a 'B' into that collection, since a 'B' is a 'A'. When I pull it out, Java thinks it's an 'A'. I can then cast it to a 'B', because it really is one.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
zoheb hassan
Ranch Hand

Joined: Apr 01, 2009
Posts: 152

Guys thank you all very much for putting in your time and answering my question

But Guys

I will BE BACK
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38517
    
  23
You're welcome
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Casting Versus Parsing