This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Converting and casting. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Converting and casting." Watch "Converting and casting." New topic
Author

Converting and casting.

Snylt Master
Ranch Hand

Joined: Jul 07, 2001
Posts: 55
Hi ranchers!
I'm reading a chapter in Complete java 2 certification study guide about converting and casting. When it comes to primitives it think I can manage to remember the rules of thumbs. But when it comes to Objects , interfaces , arrays ... then I don't havea a clue how to remeber all rules of casting.
How do you manage to keep this in mind?
Thanks
// Snylt


Preparing for the Java 2 Certification exam
Kaspar Dahlqvist
Ranch Hand

Joined: Jun 18, 2001
Posts: 128
Howdy!
I can't speak for the others, but my simple answer is that I can't. I try to remember the most common cases, and if I'm ever uncertain I try it out or look it up.
The most important rules are:
First:
You can always go up the class hierarcy. This is very similar to what you can do with primitives (you can always put a smaller type in a larger type, eg you can put a byte, short and char in an int without casting). This means that you can always assign a String object to an Object object, since this is going 'up' the tree (String inherits from Object)...
Second:
If you want to go the other way, you have to cast explicitly. This is NOT allowed:
String s = new String();
Object o = new Object();
s = o; //Illegal!
But this is:
s = (String) o;
This is legal at COMPILE TIME! The compiler does a check to see if the two classes are related, and if they are, the statement passes. The compiler hopes that the programmer knows what he/she is doing.
At RUNTIME
String s = new String();
Object o = new Object();
s = (String) o;
won't pass, since the reference o does not refer to a String object. The only way you can make it pass during runtime is to assign a String object (or an object of a subclass of String) to o:
String s = new String();
Object o = new Object();
o = "Hello!";
s = (String) o;
This will pass...
Then, of course, there are a multitude of rules more... But you will come far with these two!
Hope this helps!
/Kaspar
Snylt Master
Ranch Hand

Joined: Jul 07, 2001
Posts: 55
Thank You kaspar!
I can manage to remeber this rule. But the exam has questions like. When can you convert a class to an interface? It is impossible to remember it... At least for me! When studying for the exam it feels like the spirit you have to have to take the cert just dissapears!
// Snylt
Michaele Gardner
Greenhorn

Joined: Jul 18, 2001
Posts: 2
Hi Snylt,
I saw your posting and was hoping that you had found the secret trick, because I get confused on this question too.
One thing that helps me remember is to think of the UML diagrams that represent each class. If B extends A, then arrow points up from B to A then casting is ok.
If a class B implements interface A, it is the same diagram, only with a dotted-line arrow connecting the two. So, following the arrow is always ok--its upcasting. Drawing the UML also clearly points out that siblings that inherit from the same class can't be cast to each other.
Basic UML diagramming like this is pretty easy to pick up.
Hope this helps.
Paul Selby
Ranch Hand

Joined: Oct 20, 2000
Posts: 41
Interfaces:
They cast the same way as Classes.
Arrays:
Arrays are their own heirachy, so you can not cast them to non-array (except Object).
I think you can cast individual elements of an Array.
Try displaying the following casts. I concede that casting String objects as String objects is probably not a good example.
String myS [] = {"A","B","C"};
(Object) myS; // accepts cast
(String) myS; // rejects cast
(String) myS[0]; // accepts cast
Snylt Master
Ranch Hand

Joined: Jul 07, 2001
Posts: 55
Thank you all! I think iI got it know.

------------------
Preparing for the Java 2 Certification exam
 
Consider Paul's rocket mass heater.
 
subject: Converting and casting.
 
Similar Threads
Casting and Interfaces
Abhilash's Site ( Explicit Cast Is Not Needed in Some Cases )
How should I use my last 23hours??
casting and converting
interface within a class