Results 1 to 4 of 4
  1. #1
    sev51 is offline Member
    Join Date
    Jan 2009
    Posts
    7
    Rep Power
    0

    Default What does casting mean?

    I don't really understand what is meant by the term casting? I gather that it is converting from one data type to another.. but for example how might you use it for Classes and not necessarily primitive data types??

  2. #2
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    you got it, its converting one data type to another.
    all classes are child classes of java.lang.Object, so you can freely cast any class to Object class, or any class within the same family.
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  3. #3
    sev51 is offline Member
    Join Date
    Jan 2009
    Posts
    7
    Rep Power
    0

    Default

    Thanks for the confirmation :)

  4. #4
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    8

    Default

    Casting is used when you, as the developer, have a reference to an object of type ClassA, but you know that the object type is actually of type ClassB, where ClassB extends ClassA.

    This comes into play for generic methods that return Object. You want to invoke methods of the actual type, so you have to explicitly cast the Object to MyClass. Essentially, you are telling the compiler you know what you are doing.

    At runtime, if the Object is *not* a MyClass, you will get a RunTimeException.

Similar Threads

  1. casting help
    By soc86 in forum New To Java
    Replies: 4
    Last Post: 01-14-2009, 12:07 AM
  2. Object to Int casting
    By nn12 in forum New To Java
    Replies: 4
    Last Post: 12-06-2008, 11:58 PM
  3. type casting
    By alvations in forum New To Java
    Replies: 1
    Last Post: 10-13-2008, 08:07 PM
  4. 'Casting' couch !!!!
    By ajaygargnsit in forum Advanced Java
    Replies: 4
    Last Post: 01-04-2008, 05:54 PM
  5. Casting
    By leebee in forum New To Java
    Replies: 5
    Last Post: 08-10-2007, 01:24 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •