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

instanceOf operator question

 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why the output is Browser rather than IE.Since at runtime according to VMI it is selected as a IE.

 
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you post the complete code alongwith the class definitions for Browser etc?

We will be able to help you & explain you better.
 
Ranch Hand
Posts: 153
Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
instanceof operator checks for IS Relation here. In your code Browser comes before IE in "if else" block change the order and try, I mean put IE before Browser.

I think that helps you

Check out this from K& B book

Look for instanceof questions that test whether an object is an instance of an interface, when the object's class
implements the interface indirectly. An indirect implementation occurs when one of an object's superclasses
implements an interface, but the actual class of the instance does not—for example,
interface Foo { }
class A implements Foo { }
class B extends A { }
...
A a = new A();
B b = new B();
the following are true:
a instanceof Foo
b instanceof A
b instanceof Foo // implemented indirectly
An object is said to be of a particular interface type (meaning it will pass the instanceof test) if any of the
object's superclasses implement the interface.

 
thejaka samarakoon
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have not understand the answer.Can anyone help?
 
Malatesh Karabisti
Ranch Hand
Posts: 153
Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Try this and spot the difference. If else block always executes only one condition at time if it executes first one it won't executes second one. In your code "ref instanceof Browser" is true hence it won't even do a check for "ref instanceof IE". I think this time I explained better.
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Malatesh Karabisti wrote:
Try this and spot the difference. If else block always executes only one condition at time if it executes first one it won't executes second one. In your code "ref instanceof Browser" is true hence it won't even do a check for "ref instanceof IE". I think this time I explained better.









i have tried this code and its printing IE

please try again
 
Malatesh Karabisti
Ranch Hand
Posts: 153
Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes "IE" is expected output.
 
Ranch Hand
Posts: 754
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By the way, there is a few compilations erros in your code! [=

Maybe thats why you got the wrong result.
 
Malatesh Karabisti
Ranch Hand
Posts: 153
Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thejaka,

Yes as Hebert told your code has the compilation errors. resolve the compilation error and post the source code if you face any problem.
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thejaka, one advice for you, always use propper code fragments before post the code here, it'll be readable, you'll get quick response from ranchers.

Here your code snaps, I don't know whether, the code you mentioned is same as mine.


And you are asking, why the output is Browser rather that IE? Is that so, If I ask a question, a ball is dropped from top of a building, and there is guy in the next floor to the top floor, having a big net to catch the ball, and there is another guy, waiting to catch the ball at the next floor the previous guy. Who will catch the ball?

Same behavior here.

BTW, why do you expect the VMI and other stuff here?
 
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

thejaka samarakoon wrote:Why the output is Browser rather than IE.Since at runtime according to VMI it is selected as a IE.




How you are able to run the program,there is a compiler error.int this code..4
Also please QuoteYourSources from where you get this question
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is from the Khalid Mughal's book "At runtime, it is the type of the actual object denoted by the reference on the left-hand side that is compared with the type specified on the right-hand side. In other words, what matters at runtime is the type of the actual object denoted by the reference, not the declared type of the reference."

try changing code to

Browser ref= new Broser();

you will understand it much better.

I hope this answers your original question.
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Sid Dev
Thanks for quoting the source.
Welcome to JavaRanch
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
see the below code..you will easliy figure it out what is happening..





If that much if-statements are true why the output is Browser.Think you will easliy understand...

Line no.9 to 23 worth watching
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic