Converting and casting.

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

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

posted Jul 11, 2001 02:38:00

0

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

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.

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