• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Casting Versus Parsing

 
Ranch Hand
Posts: 154
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 492
Firefox Browser VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 2856
10
Firefox Browser Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic