• Post Reply Bookmark Topic Watch Topic
  • New Topic

array is an Object But  RSS feed

 
naveen yadav
Ranch Hand
Posts: 384
Java MyEclipse IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi ranchers ,
i have some theoretical doubts over phrase "array is an object".
my doubt is if array is an object , then whose object it is? object of which class ?

lets say i have
int[] val = new int[3];

val is not a variable , it is an object ,right? but what is the type of val ? certainly not int(primitive) type.


Please clear thing up.
 
Nicola Garofalo
Ranch Hand
Posts: 308
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
val is a variable of class int[]

it references an object, int[3]

int[3] is an object of class int[]

 
naveen yadav
Ranch Hand
Posts: 384
Java MyEclipse IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nicola Garofalo wrote:

int[3] is an object of class int[]



class int[] ? what is that ?
 
Nicola Garofalo
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry if i reply with a question:

Bike b

b is a variable of class Bike.

Bike? What is that?
 
naveen yadav
Ranch Hand
Posts: 384
Java MyEclipse IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class Bike{


}

here Bike is declared as a class. but what about "class int[]" ? int is not declared as a class anywhere ?
 
Nicola Garofalo
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry i wasn't clear, i used term class improperly, i thought it could help you.

Think of an int[] as the abstraction of the aggregation of (fixed size) integer numbers, as well as Bike is the abstraction of the concept of Bicycles.

One of the many objects of integer arrays ,int[] , can be an array of 3 integer numbers, i.e. the object int[3]

You don't need to define int[], it exists in the language, it's an array...i don't know how to say it.


Anyway the main difference on which you could point your attention is that.

An instance of a class, an object,is created by an object creation expression. For example Bike b = new Bike();
An instance of an array, an object too, is created by an array creation expression. For example int[] val = new int[3];
 
naveen yadav
Ranch Hand
Posts: 384
Java MyEclipse IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nicola Garofalo wrote:.....................
You don't need to define int[], it exists in the language, it's an array...i don't know how to say it.
...........................................


this is from java specs

Every array has an associated Class object
int[] ia = new int[3];
System.out.println(ia.getClass());

which prints:
class [I
where the string “[I” is the run-time type signature for the class object “array
with component type int”.



confusion remains.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
naveen yadav wrote:
Nicola Garofalo wrote:.....................
You don't need to define int[], it exists in the language, it's an array...i don't know how to say it.
...........................................


this is from java specs

Every array has an associated Class object
int[] ia = new int[3];
System.out.println(ia.getClass());

which prints:
class [I
where the string “[I” is the run-time type signature for the class object “array
with component type int”.



confusion remains.



Not sure what the confusion is. Basically, in the class hierachy there is a class for every array of primative type that subclass the Object class. There is a class for Object array that subclass the Object class. And there is a parallel hierarchy for arrays of all class types that subclasses from the Object array class.

Henry
 
dennis deems
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
..
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nicola Garofalo wrote:Sorry i wasn't clear, i used term class improperly

Actually, you didn't. Class int[] very much exists. You can even get the class literal using int[].class.
 
naveen yadav
Ranch Hand
Posts: 384
Java MyEclipse IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:................ Class int[] very much exists....................


where ? in which package ?

One more thing , please keep patience.
All arrays have this public final field length, this field must be specified in some class. In which class it is ?


 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16059
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's not like a regular class (you won't find an int[].class file for it anywhere), but rather a built-in thing in the compiler.

You can do things like this:
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
naveen yadav wrote:One more thing , please keep patience. ...

And you EaseUp (←click).

Winston
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
naveen yadav wrote:
Rob Spoor wrote:................ Class int[] very much exists....................


where ? in which package ?

One more thing , please keep patience.
All arrays have this public final field length, this field must be specified in some class. In which class it is ?


Well, as Jesper said, this is something internal to the compiler.... and using reflection doesn't seem to help much either...



According to this, the primative int array class returns the class descriptor name as the class name. And doesn't return anything for the package name.

Henry
 
naveen yadav
Ranch Hand
Posts: 384
Java MyEclipse IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
now i am getting.

i have discovered one more thing about array : the overridden clone() method of Object class is available to the arrays.
clone() method must be overridden somewhere OR Is that too internal to the compiler ? just like length filed is.
 
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!