• Post Reply Bookmark Topic Watch Topic
  • New Topic

Still confused about the difference between casting and parsing  RSS feed

 
Ahmad Auada
Ranch Hand
Posts: 56
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I searched for this on google, most answers tend to say this :
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.

what?

From what I understood, parsing to an integer would convert a string "1234" to and int 1234, but would give an error if the string was "abc1234". Is that right?
 
Knute Snortum
Sheriff
Posts: 4091
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. You cannot cast a String to an int, so the method has to parse the String.
 
Tyson Lindner
Ranch Hand
Posts: 211
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ahmad Auada wrote:I searched for this on google, most answers tend to say this :
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.


That seems like more of a non-programming definition of parsing since "meaning" is so ambiguous. In programming parsing is more the creating of something based on something else(for example the creation of an integer based on a String). Casting on the other hand is more like deciding what to call something, is my car a Honda Civic, vehicle, or just a car?
 
Stephan van Hulst
Saloon Keeper
Posts: 7821
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tyson Lindner wrote:In programming parsing is more the creating of something based on something else.

Not just something else. Parsing refers specifically to creating an instance of some type out of textual data. Text means nothing if you don't attach meaning to it. Parsing attaches meaning to textual data. Whenever you're processing a String, you're often unconsciously parsing it, because you're attaching meaning to parts of it.
 
Tyson Lindner
Ranch Hand
Posts: 211
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:
Tyson Lindner wrote:In programming parsing is more the creating of something based on something else.

Not just something else. Parsing refers specifically to creating an instance of some type out of textual data.


Honestly it feels like the word always gets used in that context but I don't think it absolutely has to be used that way. For example if someone has an audio file of someone speaking a list of names and numbers we would still "parse" that file to get those values even if we aren't using some built in parse method.


Text means nothing if you don't attach meaning to it. Parsing attaches meaning to textual data.


See I feel like the data already has meaning. For example we would say the string "Apple" has meaning because it relates a known everyday object. So we could also say that "two" has meaning or that "2" has meaning. When we're "parsing" one of the latter values all we're really doing is converting it to another format.
 
Stephan van Hulst
Saloon Keeper
Posts: 7821
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would argue that the String "Apple" doesn't mean anything until you parse it in your mind to the abstract concept of the fruit. Context is also important, because the String may mean different things, or nothing at all in different languages.
 
Campbell Ritchie
Marshal
Posts: 55793
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:. . . the String "Apple" . . . may mean . . . nothing at all in different languages.
Which is one reason we have an FAQ telling you to avoid using Strings as far as possible.
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tyson Lindner wrote:Honestly it feels like the word always gets used in that context but I don't think it absolutely has to be used that way. For example if someone has an audio file of someone speaking a list of names and numbers we would still "parse" that file to get those values even if we aren't using some built in parse method.

Yes, I'd tend to agree with you there. I've always thought it of parsing as "analysis of structure", so I don't think that the object necessarily has to be text (although it usually is).

I also wouldn't categorise 'parseInt()' as a parsing method. It's simply a conversion - String in→Integer out.

HTML in→DOM structure out, on the other hand, is most definitely parsing ... and I suspect there's a lot of "grey area" in between.

Winston
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!