wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Illegal and Legal Casting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Illegal and Legal Casting" Watch "Illegal and Legal Casting" New topic
Author

Illegal and Legal Casting

Zhumpex X Lead
Greenhorn

Joined: Mar 29, 2013
Posts: 3
Hei all, I'm beginner in here. Can anyone help me to explain about illegal casting and legal casting in Java. And don't forget to give an example of code. Thanks
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Zhumpex X Lead wrote:Hei all, I'm beginner in here. Can anyone help me to explain about illegal casting and legal casting in Java. And don't forget to give an example of code. Thanks


Hi, and welcome to the Ranch!

In the future, please SearchFirst(⇐click) and then post more specific questions about the parts you couldn't understand after doing some initial research.

The brief answer to your question is:
  • It is always safe and almost never necessary to upcast a reference--that is to cast a reference to an ancestor type, such as casting ArrayList to List, or MyFooBar to Object.
  • It is always legal at compile time to downcast--that is, to cast a reference of one type to a descendent type, such as casting List to ArrayList, or Object to MyFooBar.
  • Downcasting will succeed at runtime if the object being pointed to is in fact of the type we cast to, and will cause ClassCastException if it's not.
  • It is never legal to do a cast that does not move in a single direction, in the class hierarchy. Either up or down, but not both in one cast. We cannot cast List to String or vice versa.


  • For more details, do that search, and then come back if you still have questions.

    Amit Ghorpade
    Bartender

    Joined: Jun 06, 2007
    Posts: 2716
        
        6

    Hello and Welcome to Javaranch

    I would prefer the terms valid and invalid cast (type-cast). Here is the explanation with example.

    Happy ranching


    SCJP, SCWCD.
    |Asking Good Questions|
    Zhumpex X Lead
    Greenhorn

    Joined: Mar 29, 2013
    Posts: 3
    Jeff Verdegan wrote:
    Zhumpex X Lead wrote:Hei all, I'm beginner in here. Can anyone help me to explain about illegal casting and legal casting in Java. And don't forget to give an example of code. Thanks


    Hi, and welcome to the Ranch!

    In the future, please SearchFirst(⇐click) and then post more specific questions about the parts you couldn't understand after doing some initial research.

    The brief answer to your question is:
  • It is always safe and almost never necessary to upcast a reference--that is to cast a reference to an ancestor type, such as casting ArrayList to List, or MyFooBar to Object.
  • It is always legal at compile time to downcast--that is, to cast a reference of one type to a descendent type, such as casting List to ArrayList, or Object to MyFooBar.
  • Downcasting will succeed at runtime if the object being pointed to is in fact of the type we cast to, and will cause ClassCastException if it's not.
  • It is never legal to do a cast that does not move in a single direction, in the class hierarchy. Either up or down, but not both in one cast. We cannot cast List to String or vice versa.


  • For more details, do that search, and then come back if you still have questions.




    Oke, thanks for your explanation Jeff. And thanks for your tips
    Zhumpex X Lead
    Greenhorn

    Joined: Mar 29, 2013
    Posts: 3
    Amit Ghorpade wrote:Hello and Welcome to Javaranch

    I would prefer the terms valid and invalid cast (type-cast). Here is the explanation with example.

    Happy ranching


    Thanks for the link Amit Ghorpade . Its very useful. Happy ranching too
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Illegal and Legal Casting