Win a copy of Learning Java by Building Android Games this week in the Android forum!
  • 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
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Devaka Cooray
Saloon Keepers:
  • Ganesh Patekar
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
Bartenders:
  • Ron McLeod
  • Frits Walraven
  • Pete Letkeman

2D Arrays  RSS feed

 
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!
 
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?
 
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?
 
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!!
 
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


 
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.
 
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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!