Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Array of object. Question

 
Sergej Smoljanov
Ranch Hand
Posts: 467
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

why new Object[]{{}};//not valid?
(my first assumption was that it contain array of object in which each element is array of object, and it was wrong)
 
Maki Fej
Greenhorn
Posts: 9
Android Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
At line 3, you are trying to initialise a 2 dimenson array, but the reference is only a 1 dimension array.

You can define 1 dimension Arrays like:

You can define 2 dimension Arrays like:

It is no matter where you put the [] at the left site, but beware with:

However, your line 2 is very strange, because both of the next lines output true:

I am also curious about how is it possible
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maki Fej wrote:At line 3, you are trying to initialise a 2 dimenson array, but the reference is only a 1 dimension array.

You would assume it's related, but it isn't It would have been if the regular initialization syntax was used (and not the one to create an anonymous array).

Let's have some array fun!


Now back to the code snippet from the OP:


Yes, that's right! I just assigned a 2D-array to the 1D-array reference variable o2fix Not a problem at all. Don't forget: in Java every array is considered an Object. So a 2D-array can be seen as a 1D-array where each element is an Object that happens to be a 1D-array itself

So that results in some more array fun! Always be careful when casting arrays: dimension matters! If a cast uses a wrong dimension, you'll get a ClassCastException.


Hope it helps!
 
Prathima gaitonde
Ranch Hand
Posts: 130
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I have similar but not this advanced doubt in my mind, hope no one will mind posting it here, rather than a new post.

I know array is a object, but cant understand the concept of array of objects,

i.e
String s[];
Object[] o=s;

How does this work? Please help me understanding this.

Kind regards,
Prathima
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prathima gaitonde wrote:I know array is a object, but cant understand the concept of array of objects,

i.e
String s[];
Object[] o=s;

How does this work? Please help me understanding this.

For starters your example code snippet doesn't compile if s is a local variable, because it's used before it's initialized. So you'll get a compiler error.

This code snippet will compile successfully:


The concept of an array is fairly simple. An array is a container object that holds a fixed number of values of a single type. So if you need to store 10 integers, you could have 10 int primitive variables i1 until i10. Or you could have 1 int array with 10 elements (e.g. int[] array = new int[10];).

As you already pointed out, every array is an Object (with upper case o). So you can invoke all methods from Object on any array. Illustrated in this code snippet:


So there is no problem to assign an array to a reference variable of type Object:


If you work with classes (or interfaces), you can assign an array of Strings to an array of Objects, because String IS-A Object. That makes sense if you think about it: if you have a container with nothing but Strings (like a String array), each element of this container is a String, but it's also an Object, a CharSequence and a Serializable. So you can assign a String array to an array of any of these types. Illustrated in the following code snippet:


But a String IS-NOT-A Integer and IS-NOT-A IOException, so you can't assign a String array to an Integer array (or IOException array). Again illustrated with a code snippet:


And what about primitives? You can assign a byte to a short, but can you assign a byte array to a short array? The answer is simple and clear: no! So assigning an array of a subtype to an array of a supertype works only with classes (and interfaces), not with primitives.


Hope it helps!
Kind regards,
Roel
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic