This week's giveaway is in the Spring forum.
We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv 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 REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Converting and casting." Watch "Converting and casting." New topic

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?
// Snylt

Preparing for the Java 2 Certification exam
Kaspar Dahlqvist
Ranch Hand

Joined: Jun 18, 2001
Posts: 128
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:
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)...
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.
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!
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

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
They cast the same way as Classes.
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
I agree. Here's the link:
subject: Converting and casting.
It's not a secret anymore!