• 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

Implicit and Explicit Casting

 
Ranch Hand
Posts: 295
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anyone please explain to me on how I can get myself even aquainted with the operator assigment stuffs? I'm still quite unsure on when I should cast or whether the casting is done implicitly for assignment of char to int, float to long, etc etc etc.
Thanks in advance.
 
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
Take a look at the JLS' section on Conversions and Promotions.
That should tell you all you need to know....
/rick
 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is a snippet from my very own java notes
* java 'implicit' up-casting rule is

The other type of casting is with refernce type, you don't need to do an explict cast with up-casting.
class A {}
class B extends A {}
A a1 = new B(); // no cast for up-casting
B b1 = new B();
A a2;
a2 = b1; // no casting required
B b2 = (B)a2; // need to cast when down-casting!
Hope this helps
[ March 09, 2002: Message edited by: Rajinder Yadav ]
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rajinder,
you mixed up/downcasting...
When casting an object of type B to a reference of type A, it is an upcast (no need to cast, it is in fact a widening reference conversion and no RuntimeException are ever thrown)
When casting an object of type A to a reference of type B, it is an downcast (casting required since it is a narrowing reference conversion and a ClassCastException may be thrown at runtime)
In the hierarchy, A is "higher" than B...
[ March 09, 2002: Message edited by: Valentin Crettaz ]
 
Rajinder Yadav
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Valentin, thanks for catching my mixup. I've fixed it now!
 
Steven Wong
Ranch Hand
Posts: 295
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot for the answers, guys.
 
Greenhorn
Posts: 23
  • 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 as the compiler will always cast automatically when you're going from small (byte) to big (int) (or widening).
When you're going from big to small (double -> byte) you're gonna have to explicitly cast because the double value might get cut off since byte is so much smaller than double.
 
reply
    Bookmark Topic Watch Topic
  • New Topic