954,536 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Casting and Parsing

Here's an easy one for you experts :P

What is casting and parsing? How exactly are they different?

After explaining, could anyone possibly tell me how to cast/parse the various types from and to each other? (the types being: int, double, char and String)

Also, in which situations would I end up using either one of these?

Thanks :D

insanely_sane
Junior Poster in Training
60 posts since Jun 2010
Reputation Points: 17
Solved Threads: 3
 

Parsing - changes a String into a primitive data type(may be other uses)
Casting - changes from one primitive data type to another, can also be used to cast objects.

Use, if you have a string and need to convert it into a primitive type us Parsing
i.e.

String s = "23";
int i = Integer.parseInt(s);

If you have a primitive type and need to convert it to another primitive type, use casting.

To take a primitive type to a String you have to use a Wrapper class. A Wrapper class allows you to make an object out of a primitive type.

String s = Integer.toString(8);//toString() is a method that most(if not all) objects have

As far as listing a bunch of conversion types, you can do a Google search as needed.

Mattox
Junior Poster in Training
57 posts since Jan 2011
Reputation Points: 10
Solved Threads: 15
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: