• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Casting Versus Parsing

 
zoheb hassan
Ranch Hand
Posts: 154
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49841
70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2854
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12186
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
zoheb hassan
Ranch Hand
Posts: 154
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guys thank you all very much for putting in your time and answering my question

But Guys

I will BE BACK
 
Campbell Ritchie
Sheriff
Pie
Posts: 49841
70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic