• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Tim Cooke
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
Saloon Keepers:
  • Piet Souris
Bartenders:

casting objects

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am new to Java, things are going relatively well until I hit casting objects and the instanceof operator. Can anyone out there please help clear the air a bit for me. I do not know why but I simply cannot nail this thing down. PLEASE HELP!
 
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
reply
    Bookmark Topic Watch Topic
  • New Topic