Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Converting & casting help needed!

 
Dave Johnson
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am having a bit of trouble grasping converting & casting objects. I have just read the relavent chapter in the exam cram book.
To be honest 95% of it has sunk in. I wondered if any of you know of any links that I could read to help me.
Many thanks, Dave.
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Dave.
The Essence of OOP using Java, Polymorphism, Type Conversion, Casting, Etc. by Richard G. Baldwin.
Thinking in Java (TOC) by Bruce Eckel. There looks for "casting"

JLS 5.5 Casting Conversions
If you have any practical doubt feel free to post it.
 
Roger Chung-Wee
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am having a bit of trouble grasping converting & casting objects.

Don't know if this has any relevance to any problem you have, but objects are never cast.
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Roger Chung-Wee:

Don't know if this has any relevance to any problem you have, but objects are never cast.



How about:
Object obj = new String("Cast Me");
String s = (String)obj;
 
Roger Chung-Wee
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What I meant was that some people think that objects get changed by casting. Of course, this is not the case - we are talking about object references.
 
Kaz Yosh
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roger,
I'm not quite sure what you are saying here.
 
Roger Chung-Wee
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

When a1 is cast to B on line 3, does this affect the object on line 1? Not in the slightest: the runtime type of the object that a1 references is determined by the argument to new, which is B. And it remains of type B until destroyed.
Casting does, however, affect object references because you are assigning the value of one reference variable to another.
Incidentally, note that the cast is mandatory because even though a1 contains references to B, we are doing a downcast from a superclass to a subclass.
 
Dave Johnson
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Many thanks for replies, Dave.
 
Dave Johnson
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reason I started this thread was a sample of code previously posted here.
Forgive me I cant remember the posters name. But the code looked something like this.

Admittedly I inserted the System.out.println(c1.getClass line myself. What I couldn't understand was the variable c1 was using the method of class A but was printing it was a instance of class C.
Thanks to the reading I've done (provided by you guys) I feel a bit surer.
It seems (correct me if I wrong (again)) the c1 is still an instance of C and like it has been said in this thread earlier, that cannot be changed. But the line: A c1 = new C(); has put that object into a reference of type A, therefore using A's void m1 method.
Thanks for the links, especially this one http://www.developer.com/tech/article.php/974871. I might not be a ranch hand but I feel a lot more in the know.
Cheers my friends, Dave.
 
Saket Barve
Ranch Hand
Posts: 229
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It seems (correct me if I wrong (again)) the c1 is still an instance of C and like it has been said in this thread earlier, that cannot be changed.

Yes, this is true unless your code explicitly sets the reference of c1 elsewhere.

It seems (correct me if I wrong (again)) the c1 is still an instance of C and like it has been said in this thread earlier, that cannot be changed. But the line: A c1 = new C(); has put that object into a reference of type A, therefore using A's void m1 method.

Well, the reference type of c1 is A hence the method lookup occurs in class A. c1 is an object of class C, this answers your question regarding the getClass() return type.
We could reference c1 to type A simply because C IS A.
[ May 31, 2003: Message edited by: Saket Barve ]
 
Roger Chung-Wee
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is how you can think of it.
Do we have early or late binding? (Connecting a method call to a method body is called binding.) You automatically have late binding in Java with non-final, overridden instance methods, as Java provides the means to determine the type of the object at run time.
In your example code, there is early binding because method m1 is overloaded, not overridden. In this case, the compiler can sort out the binding by referring to the type of object reference. The object reference c1 is of type A, so it is A's m1 which is invoked.
Hope this makes sense.
 
Dave Johnson
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is another example of code that I find a little confusing.

If only the reference gets changed and the object stays the same, why does the code print out that of is an instance of the floats class?
Sorry for being thick, but as I said in this thread earlier this area of Java keeps tripping me up.
Thanks again, Dave.
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The runtime type of the instance pointed to by f, ar, o and x keeps on being floats all the time. getClass() reports this type. Each of these variables has compile type compatible with the runtime type of the instance they hold.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic