• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

instanceof operator

 
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
interface I {};
class C {};

public class Test4 {

public static void main(String[] args) {

C c = new C();
System.out.println("(1): " +
(c instanceof I)); // line (1)
}
}

Why the above code doesnot give compile time error even though their is no common subtype of interface I and class C???
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ,


I have tried with the interface on the right hand side of instanceof operator ,it doesnot give complier error as well as runtime error .
But if instead of interface type if we put class type on RHS,it gives complie error .
Don't know why ?

Regards
Rupal
 
Girish Nagaraj
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes you are right.For class type on the right of instanceof operator->compilation files as expected.

Can any one answer why it is not the for interfaces.
 
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
C is not final so it can be subclassed and there can be a subclass of C which implements I.

The rules about compilation errors are defined in Casting conversion (Java Language Specification). The same rules apply to instanceof operator.
[ May 02, 2006: Message edited by: Vlado Zajac ]
 
Girish Nagaraj
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes you ppl are right when I make class C final.

It will not compile as expected.
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
interface I {};
class C {};

public class Test6 {

public static void main(String[] args) {

C c = new C();
System.out.println(c instanceof I);
System.out.println(c instanceof C);
}
}

i tested both and idd not give me neother compiler nor runtime error
reply
    Bookmark Topic Watch Topic
  • New Topic