• 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

Object references to arrays

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello folks,

I've been trying to find a senseful answer for this:



What is a good reason for me to want something like (2)?

What is the object that p effectively references to? I would expect that (1) and (2) gave me different results.

I appreciate if you can provide some material on this.

Thank you,
Hiram
arrays.jpg
[Thumbnail for arrays.jpg]
Arrays
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think "p" stores 5 int arrays, each int array is a Object, then you have a array of Objects
"s" does not work because you are tying to use a Object array to store int primitives.
Object[] s = new Integer[6]; is going to work because Integer extends Object.
 
Hiram Nascimento
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Guilherme,



There's 3 different int arrays of length 7 inside.

If it references to the one of the "internal" arrays, which one will it reference?

The result of the "sysout" is 3, which means that 'j' references a int[][] (that's also what we can see in the casting I made).

I confess, that kind of declaration still sounds very confuse to me. That was a simulation exam test.

Does anyone know if that's covered on some chapter on SCJP preparation book (Kathy/Bert)

Thanks,
Hiram
 
Guilherme Borges Lima
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
j refers to a Object that holds 3 int arrays, each int array(int[7]) is a Object.
In the example we have 4 Objects.
3 Object that hold int[7]
1 Object that holds the other 3 Objects
It's a array of Objects

Yes, that is covered at chapter 3 - Array Declaration, Construction, and Initialization
 
Hiram Nascimento
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That means:



And...



All Object[] is an Object, but NOT all Object is Object[].

In practice, the results obtained by using the Object[] or Object reference to an array is the same.

Do you agree?
 
Guilherme Borges Lima
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, an Object[] is more specific than an Object, but an Object is not necessarily an Object[].
The results are similar, but using the more specific version (Object[]) you could use things that are specific to an Object[]
In your second example you could do the following:
But the following is not allowed, because an Object has no length attribute
 
grapes are vegan food pellets. Eat this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic