• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

about instanceof .

 
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, all, the following statements, if select the most appropriate one, which one should we select?

I think it is <code>2</code> since if <code>ref</code> is <code>null</code>, it will yield <code>false</code>.
What is your opinion?
Thanks in advance.
Guoqiao

[This message has been edited by Guoqiao Sun (edited August 16, 2001).]
 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guoqiao,
According to me 2 is correct. You can see it by just writing a small code.
Gurpreet Sachdeva
For mock exams and other useful pages related to JCP plz. visit: http://www.go4java.20m.com
[This message has been edited by Gurpreet Sachdeva (edited August 17, 2001).]
 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is easy to test, but #2 is the correct answer...
try:
String ref = null;
if( ref instanceof Object )
System.out.println(true);
else
System.out.println(false);
nullref instanceof anything will always be false...
even nullref instance of nullRefType
String ref = null;
if( ref instanceof String )
System.out.println(true);
else
System.out.println(false);
}
also prints out false...
The tricky part of your question, however, is should
String ref = null;
be considered a "reference"?


[This message has been edited by Guy Reynolds (edited August 17, 2001).]
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What if the ref is an interface?? would it reply false or true?
Thanks
 
Guy Reynolds
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Still false...
 
Your mother is a hamster and your father smells of tiny ads!
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic