• 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 & Conversion

 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi 'All..
Iam digging through lot of material and javaranch regarding difference between Casting and Conversion of object references but Iam unable to figure it out the difference...Iam messed up
It would be great help if any one could provide a detailed exaplanation with an example ..even a good reference on this will be appreciated.
thnx
rishi
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Well, colloquially there's no difference at all, and this is probably why you're confused. Let's see if I can make a useful distinction.
To "convert" one type to another means the actual bits have to change. For example, the computer's internal representation for integer 3 and double 3.0 are different. If you have a variable of type int, and want to assign the value 3.0 to it, then the value has to be converted. This requires actual computation. In Java, it only makes sense to talk about converting from one numeric primitive type to another: long to int, double to float, etc. You can't convert from an object type to a primitive type, or vice versa.
To "cast" from one type to another can mean to tell the Java compiler to just treat a value that it thinks is of some type T1 as if it were actually of type T2. For example, a variable "o" of type Object might be referring to a value of type String. To tell the Java compiler to treat the value in o as a String, you can use a cast:

Here T1 is Object, T2 is String, and the form "(String)" is the cast. Note that nothing gets converted or changed -- you're just telling the compiler to treat this object reference as a reference to another type.
Now, here's where it gets tricky. The cast syntax is also used to tell the compiler that a value should be converted from one primitive type to another. In the "3.0" example before, the cast is required.

So here a cast is used to tell the compiler to do a conversion.
See the difference?
 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
byte
/
double->float->long->Integer->short->
\
char
while following the arrow you don't need to cast i.e. its implicit cast but while going in opposite direction you need an explicit cast.
Hence as mentioned by Ernest you need a cast for
double d = 3.0;
int i = (double) d;
Hope that helps
-PC
 
Priyanka Chopda
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oops! that byte and char should be at the other end i.e. towards int!
seems it didn't formatted the text!
-PC
 
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ernest,


Well, colloquially there's no difference at all, and this is probably why you're confused. Let's see if I can make a useful distinction.


Correct me if I am wrong, but I thought there were some differences between conversion and casting:
1�) Generally, for both primitives and object references,
conversion means implicit, automatic type changing
casting means explicit, non-automatic type changing and is used when you want to perform a narrowing conversion.
Narrowing (thus casting) runs the risk of losing information, conversion does not.
2�) More especially, in the case of object references,
reference conversion (like primitive conversion) takes place at compile time, because the compiler has all the information it needs to determine whether the conversion is legal.
It is not the case for object casting, check of which can not occur before run-time.
I supposed - up to now - these points were important differences, aren't they?
 
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I kind of view it differently as we are playing with words:
1. The terms conversion and casting are interchangeable.
2. You have two types of casting (or conversion):
- implicit: compiler does it without programmer telling
- explicit: programmer has to instruct otherwise a compiler
3. These castings ( or conversions) can be applied to primitives or Objects. Java is very clear about
a. which ones are implicit castings (or conversions)
1. when it comes to primitives (e.g. narrow type to wider type also termed widening... byte to short) and
2. when it comes to Objects (e.g. subtypes can be deemed as supertypes while actual object is still subtype....treat a man as human);
b. which ones are explicit casting (or conversions)
1. for primitives (e.g. wider types to narrower types... short to byte) and
2. for Objects (e.g. supertype is deemed as subtype...for example a man that was being treated as a human, now can be treated as man again).
I hope it makes sense.
Barkat
 
cyril vidal
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK Barkat,


1. The terms conversion and casting are interchangeable.


I didn't know that because of my Complete Java 2 Certification Study Guide (chapter 4 Converting and Casting) that makes clearly the difference between both terms.
Thanks for your restatement.
Cyril.
 
Rishi Wright
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi 'll,
till now what I understood is
Conversion and Casting are NOT two different terms they implicate the same
only thing is either they can be explicit or implicit
*when primitive conversion/casting takes place the bits pattern will change accordingly
*In both explicit and implicit casting/conversion
In Case Of primitives
there will be a check at compile time if both are of same data type
In Case Of Object Type
the compiler checks if both are of same reference type
at run time the object references will be checked for the object they really pointing to
Pl..correct me if Iam wrong or anything important to be added
Thnx
rishi
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic