• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

2D Arrays

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why don't you try it?
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Spring Objective C
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wow, thank you!!
 
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: 394
Eclipse IDE Oracle Java
  • 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



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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic