Win a copy of Java 9 Modularity: Patterns and Practices for Developing Maintainable Applications this week in the Java 9 forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

2D Arrays  RSS feed

 
k reeta
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, I was wondering if an array(of primitives/references) is an Object, then is a 2d array an Object as well or an array of Objects i.e.

int[] i;
Object o=i

But if we have-

int[][] ii;

Which of the following is true -
Object[] o=ii;
OR
Object o-ii;

Thanks!
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why don't you try it?
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you sure that it's one or the other? Can't it be both?
 
Sylvain Bouchard
Ranch Hand
Posts: 41
Java ME Objective C Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wouter is right, it can be both.

This code output:
ii is an Object
ii is an Object[]
ii[0] is an Object




 
k reeta
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow, thank you!!
 
Ruchika Si
Greenhorn
Posts: 3
Java
  • Mark post as helpful
  • send pies
  • 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


 
Ikpefua Jacob-Obinyan
Ranch Hand
Posts: 394
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • 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



Ikpefua wrote:

Hello Ruchika, I want to try and help by explaining my understanding of arrays based on what I studied in the K & B book.
should be read as follows: "Declaration of a reference variable f of that refers to an object of type Foo".
should be read as follows: "Declaration of a reference variable f that refers to an ARRAY OBJECT that holds a total of 5 'f' of type Foo".

Hint: Remember that an array[] is an object whose principal duty -amongst others- is to hold reference variables of a particular type,
so this '[5]' means: "Hey I am an Object (NOT A FOO OBJECT), and I contain a total of 5 'f' of type Foo".

Summary: An array Object is NOT the same thing as a Foo Object. (Precisely what the instanceof test is telling you)

I hope this helps, sincerely speaking arrays gave me headache at a time till I got to understand it.
 
Andreas Svenkson
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ruchika.

instanceof can only be used with an object that can possibly be of the righthand operand. You would get the same result if you tried:



These are two entirely different inheritance trees, and therefor will not compile.
However, if you would cast the Integer to Object, then the compiler has no idea if the object is indeed a String, so it will compile it and let the instanceof operator decide at runtime if it is true or false.



The same thing goes for your code, because an object of type: Foo[] is not in the inheritance tree of Foo. This goes for the array of any object type; an array object is not in the same tree as the class itself. The tree looks as follows for a class A that extends Object:

A[] <- Object[] <- Object

As you can see, class A is not in that tree, and therefor the code will not compile the instanceof test because the compiler knows that it can't be true.

// Andreas
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!