File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Converting & casting help needed! 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 "Converting & casting help needed!" Watch "Converting & casting help needed!" New topic
Author

Converting & casting help needed!

Dave Johnson
Ranch Hand

Joined: May 25, 2003
Posts: 111
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

Joined: Jul 03, 2001
Posts: 2120
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.


SCJP2. Please Indent your code using UBB Code
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
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.


SCJP 1.4, SCWCD 1.3, SCBCD 1.3
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
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;


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
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

Joined: May 22, 2003
Posts: 63
Roger,
I'm not quite sure what you are saying here.


"If anything can go wrong, it will"
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683

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

Joined: May 25, 2003
Posts: 111
Many thanks for replies, Dave.
Dave Johnson
Ranch Hand

Joined: May 25, 2003
Posts: 111
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

Joined: Dec 19, 2002
Posts: 229
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

Joined: Sep 29, 2002
Posts: 1683
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

Joined: May 25, 2003
Posts: 111
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

Joined: Jul 03, 2001
Posts: 2120
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Converting & casting help needed!
 
Similar Threads
cast
Convert PDF to RTF using itext
Convert PDF to RTF using itext
Abhilash's Site ( Explicit Cast Is Not Needed in Some Cases )
a question about conversion