• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Scott Selikoff
Bartenders:
  • Piet Souris
  • Jj Roberts
  • fred rosenberger

The "==" operator. Why does not it work?

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

Hi people! First, sorry for me bad english.

I want to get the certification, but I have some troubles.

I can not understand why this code NOT compile in the lines 20 and 21, but it compile in line 19.
Can anyone help me? Thank a lot!



 
author
Posts: 23923
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

al am wrote:
Hi people! First, sorry for me bad english.

I want to get the certification, but I have some troubles.

I can not understand why this code NOT compile in the lines 20 and 21, but it compile in line 19.
Can anyone help me? Thank a lot!





Line 20: An Integer array object is NOT an array of array of Integer array object. And the compiler can determine that at compile time. Hence, it is a compile time error.

Line 21: An int array object is NOT an array of array of int array object. And the compiler can determine that at compile time. Hence, it is a compile time error.

Line 19: An Object Array object may be an array of array of Object array object, as an array of array of Object IS-A Object. Hence, the compiler will generate the code, to perform the check at runtime.

Henry
 
Alam Ameghino
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Henry.

I can understand the things that you are saying about lines 20 and 21, but I can not understand the things that you are saying about line 19.

If line 20 NOT compile ok, so, We could say "X[] == X[][]" will not compile for any X (X is an Object, not a primitive type), but it is not true.

I can not understand why it NOT compile for any X, witt exception of X = Object.

Again, sorry for me bad enlgish. I hope you can read it ok.

Really, a lot of thanks.
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry firstly.

error:Incompatible operand types Integer[] and Integer[][][]
so it is wrong.
 
Henry Wong
author
Posts: 23923
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

al am wrote:
I can understand the things that you are saying about lines 20 and 21, but I can not understand the things that you are saying about line 19.

If line 20 NOT compile ok, so, We could say "X[] == X[][]" will not compile for any X (X is an Object, not a primitive type), but it is not true.

I can not understand why it NOT compile for any X, witt exception of X = Object.

Again, sorry for me bad enlgish. I hope you can read it ok.
.



First, examine the inheritance tree for Integer. I assume that you already know that an Integer IS-A Number, and Number IS-A Object, so hence, Integer IS-A Object. If we draw it out, along with other Number types, it looks like this...



Second, remember that arrays are objects. This means that an int[] is an object -- and not a primitive. In fact, the array versions of all of the primitive types are objects. Like so...



And finally, remember that an Object[] IS-A Object, so filling in the inheritance tree for that...



So... can you noticed that an Integer[] is NOT IS-A Integer? One is not a subclass of the other. However, an Object[] IS-A Object. One is a subclass of the other. Does that help? The Object class type is the only type with this relationship with its element type.

Henry
 
Alam Ameghino
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First, I want to grateful to you A LOT.
But, there are not way. I can not understand it! I am so angry with me!

Let me explain to you through the next chart what is my problem.

For resolve it I had the next premise: "Two object are comparable (== operator) if they are in the same hierarchy"

I suppose that the next hierarchy chart that I did is correct, if it is not correct, please, let me know it.



In the hierarchy chart we can see:

- IN RED COLOR, that Integer[] and Integer[][] are not in the same hierarchy, so hence, is correct that it do NOT compile.

- IN GREEN COLOR, that Object[] and Object[][] are not in the same hierarchy, so hence, Why does it compile?

Again, sorry for me bad english!

THANKS!
 
Enthuware Software Support
Posts: 4623
50
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

al am wrote:
- IN GREEN COLOR, that Object[] and Object[][] are not in the same hierarchy, so hence, Why does it compile?

THANKS!


Object[] and Object[][] ARE in the same hierarchy because as explained by Henry above, Object[] IS-A Object.
 
Henry Wong
author
Posts: 23923
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

al am wrote:
Let me explain to you through the next chart what is my problem.

For resolve it I had the next premise: "Two object are comparable (== operator) if they are in the same hierarchy"

I suppose that the next hierarchy chart that I did is correct, if it is not correct, please, let me know it.




As mentioned, the basic concept to remember is An Object[] IS-A Object. Also, by deduction...

An Object[][] IS-A Object[]
An Object[][] IS-A Object

An Object[][][] IS-A Object[][]
An Object[][][] IS-A Object[]
An Object[][][] IS-A Object

An Object[][][][] IS-A Object[][][]
An Object[][][][] IS-A Object[][]
An Object[][][][] IS-A Object[]
An Object[][][][] IS-A Object

It is probably much easier to think about it in terms of just deducing it from the basic An Object[] IS-A Object concept instead of a class hierarchy, but if you want to use a class hierarchy, then your drawing is wrong. To draw the class hierarchy, the important relationships are listed in green.

Henry
 
Alam Ameghino
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Paul,

Henry said that Object[] is an Object, and Object[][] is an Object. I am agree with it. But he never say that they are in the same hierarchy!


THANKS TO ALL PEOPLE!
 
Henry Wong
author
Posts: 23923
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

al am wrote:
Henry said that Object[] is an Object, and Object[][] is an Object. I am agree with it. But he never say that they are in the same hierarchy!



The only rule that you need to remember is an Object[] is an Object. Everything else you should be able to deduce it -- otherwise, it is a lot of rules to remember.

Henry
 
Alam Ameghino
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henrry, I think that I could understand to the end!

You chart help me a lot!

I did and example with their own hierarchy chart. You can see it in the image.

For me is new it. I thought that X[] is and X[][] was always false. But I can see now that X[] is and X[][], for X = Object is true!!




Please, if I am wrong let me see!

A lot of thanks!
 
Alam Ameghino
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hi people,

I noticed that the previous hierarchy chart was wrong. I did another example, and I fixed it.
This is the new hierarchy chart:





I want to show gratitude to all people that helped me understand it, in special to Henrry.

THANKS.
reply
    Bookmark Topic Watch Topic
  • New Topic