• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

instanceof confusion

 
Ranch Hand
Posts: 200
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


The above coding is very confusing to me. No problem in the operators used in the above program. I know the precedence and how the &&,&,|| and | will work.

All my confusion is in instanceof thing!!
What the below things are exactly mean?

1. (a1 = new D()) instanceOf C
2. a2 = new B()) instanceof A
3. a2 instanceof B
4. a3 = new C()) instanceof A
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

So,
any instance of B is also an instance of A, and
any instance of D is also an instance of A, and
any instance of C is also an instance of B, and
any instance of C is also an instance of A.

According to the Java Language Specification, "the result of the instanceof operator is true if the value of the ... [first operand, which must be a reference] is not null and the reference could be cast (�15.16) to the ... [second operand, which must be a type] without raising a ClassCastException. Otherwise the result is false." (The bold is mine.)

"a1 = new D()" creates a new instance of type D, and assigns it to a variable of type A (an upcast). So, "(a1 = new D()) instanceof C" returns false, because the true runtime type of a1 is D, and an instance of D is not an instance of C. In particular, a reference to an instance of D cannot be cast to type C without a ClassCastException.

Now, here's the trick: Because this evaluates to false, the second boolean is not evaluated due to short-circuiting of the && operator. In particular, "a2 = new B()" does not execute, and a2 remains a null reference. Therefore, "a2 instance of B" actually returns false.


Ref: http://java.sun.com/docs/books/jls/second_edition/html/expressions.doc.html#80289
[ August 23, 2005: Message edited by: marc weber ]
 
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are situations where you have a general type and need something more specific. Consider a method that counts ten-speed bikes but it gets a list containing all different makes and models.



There is no "type" attribute in Bike so you are stuck testing the type using instanceof. Any TenSpeed or any bike that inherits TenSpeed will be counted. If a Buzzard inherits from Bird then Buzzard IS-A bird.

http://java.sun.com/docs/books/tutorial/java/concepts/inheritance.html]http://java.sun.com/docs/books/tutorial/java/concepts/inheritance.html]http://java.sun.com/docs/books/tutorial/java/concepts/inheritance.html
[ August 22, 2005: Message edited by: Rick O'Shay ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic