aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Does casting apply to references or to objects? (was Doubt???) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Does casting apply to references or to objects? (was Doubt???)" Watch "Does casting apply to references or to objects? (was Doubt???)" New topic
Author

Does casting apply to references or to objects? (was Doubt???)

Shiaber Shaam
Ranch Hand

Joined: Jun 16, 2006
Posts: 252
When we type cast object references..... we are casting the references or objects.... Plz clarify......
[ July 27, 2006: Message edited by: Barry Gaunt ]

$Hi/\bEr
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Plz? Postleitzahl? What place do you need the Postleitzahl of? Z�rich is 8001.

Please use real words - or are you using a mobile phone to access the internet?

Perhaps this will help you understand casting
[ July 27, 2006: Message edited by: Barry Gaunt ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Shiaber Shaam
Ranch Hand

Joined: Jun 16, 2006
Posts: 252
Plz apologize me if it is not meaningful or it violated the policies of the forum... But i am not clear with the doubt.... So plz clarify........
Ramamoorthy Periasamy
Ranch Hand

Joined: Feb 06, 2006
Posts: 30
we are actually casting a reference variables to objects.

class Animal {
}
class Dog extends Animal {
}
public class DogTest {
void go() {
Dog d = new Dog();
Animal a = new Dog();
Animal a1 = d; // doesn't require a cast for downcasting
Dog d1 = (Dog)a; // requires explicit cast for upcasting
}
}
Ramamoorthy Periasamy
Ranch Hand

Joined: Feb 06, 2006
Posts: 30
In the earlier given example, make sure the reference variable passes the is-a condition (instanceof) with the object to be typecasted, otherwise we will get runtime exception

Animal a = new Animal();
Dog d = (Dog)a; // throws runtime exception
Shiaber Shaam
Ranch Hand

Joined: Jun 16, 2006
Posts: 252
Can u make it more clear... I cant get ur point.......
Shiaber Shaam
Ranch Hand

Joined: Jun 16, 2006
Posts: 252
class A{}
class B extends A{}
class Test
{
public static void main(String args[])
{
A a1 = new A();
B b1 = new B();
A a1 = (A)b1; //after executing this the right hand side will
//have object of type A or B
//i.e the ref gets casted or the object itself
//gets casted
}
}
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11320
    
  16

Shiaber,

please read this. We really prefer people to spell out words, rather than using the shorthand style common to text messages.

thanks


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Does casting apply to references or to objects? (was Doubt???)