File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Casting Versus Parsing" Watch "Casting Versus Parsing" New topic

Casting Versus Parsing

zoheb hassan
Ranch Hand

Joined: Apr 01, 2009
Posts: 154

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



Hope this helped.

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

Joined: Oct 13, 2005
Posts: 46334
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

Joined: Jun 06, 2007
Posts: 2851

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.

|Asking Good Questions|
fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11955

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: 154

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

But Guys

I will BE BACK
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46334
You're welcome
I agree. Here's the link:
subject: Casting Versus Parsing
It's not a secret anymore!