• 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 and Arrays

 
Greenhorn
Posts: 3
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
According to k&B Java 5 table 4-1 pg 286 if first operand is ref of Foo[] and instanceof operand is Foo Bar Face, it will return false but it is returning true. Can you explain?

interface Face{}

class Bar implements Face{}

public class Foo extends Bar{

public static void main(String [] args){

Foo[] f = new Foo[5];

if (f instanceof Foo[]) System.out.println("true"); // Prints true

// if (f instanceof Foo) System.out.print("1 "); // it does not compile



}

}
What should I do fix the code?

Thanks
 
Ranch Hand
Posts: 42
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As you have jotted down - " ... instanceof operand is Foo Bar Face". But in your code listing, the line prints true is using "Foo[]" instead of "Foo", so I see no problem in this line.

The statement in your code listing that does not compile is the right code snippet to look at, I think K&B book should say the expected result is "compile error" instead of "false".
 
Author
Posts: 375
22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ruchika,

You can also refer to the following thread, to understand why and when instanceof operator returns true/ false and fails compilation:

https://coderanch.com/t/481356/java-programmer-SCJP/certification/not-understanding-why-instanceof-operator

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

Ruchika Si wrote:According to k&B Java 5 table 4-1 pg 286 if first operand is ref of Foo[] and instanceof operand is Foo Bar Face, it will return false but it is returning true. Can you explain?

interface Face{}

class Bar implements Face{}

public class Foo extends Bar{

public static void main(String [] args){

Foo[] f = new Foo[5];

if (f instanceof Foo[]) System.out.println("true"); // Prints true

// if (f instanceof Foo) System.out.print("1 "); // it does not compile



}

}
What should I do fix the code?

Thanks



Look at it this way: a non-array is NOT an instance of an array, even if the non-array is type Foo, and the array holds references of type Foo
 
Ruchika Si
Greenhorn
Posts: 3
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks everyone for your feedback
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic