posted 24 years ago
those are pretty broad questions, so it's unlikely you'll get a complete answer in one session. You'll have to study this.
Basically, casting is about converting variables from one type to another, related type. There are rules that govern casting, but if you learn the underlying concepts you won't have to just memorize a bunch of static casting rules.
The basic concept is that *all* values in java have a type. There are 2 basic types in java, primitive types, and reference types. Primitive types are all the java primitives : byte, short, char, int, long, float, double, and boolean. Reference types are basically every other type that is not a primitive, and include arrays, classes, and interfaces.
Casting is the act of explicitly converting a type to a different type. You can only convert things that have some type of logical connection. For example, you cannot cast a boolean value to anything other than a boolean value. You can cast numbers to other types of numbers. For example,
byte b = (byte) 12;
this casts the int literal 12 to type byte, and assings it to byte variable b.
There are many such casts that are possible. Some result in no data loss; some will loose data. You will have to learn how that all works.
Casting reference types is more complicated, because the relationships between them can be more complicated than primitive types. But you can't cast a primitive type to a reference type, or vice-versa.
As for instanceof, that is an operator, in the sense that "+" is an operator (called the assignement operator). It functions by returning true if the operand IS, or is a subclass or descendant of, the type immediately to the right of the operator.
For example,
String s;
if (s instanceof String) //true
if (s instanceof StringBuffer) //false
Again, your question is very broad, so this answer can't be that specific. But I hope this is a start. Do a search in this forum, and in the Programmer Certification Forum, for "casting" and read the existing threads for more info.
Good Luck!