• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why arrays are object ?

 
Anto Telvin
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all can anyone tell me why it is saying that array's are object ...i am beginner in java..i read so many books but not getting a correct answer ?

can anyone help

thanks
 
fred rosenberger
lowercase baba
Bartender
Posts: 12186
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
do you have some reason to thing they should NOT be objects?
 
Campbell Ritchie
Sheriff
Pie
Posts: 49849
70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because the designers of the language decided to make arrays objects in their own right.
Try calling a method on an array, eg toString(); even though the result may be rather basic (eg [I@abc123de) it gives a result at all.
 
Prashanti Mukund
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java is pure object oriented language except primitive datatypes rest all can be represented as objects.So when we create a class and when we want an array of objects of that class to be created how can we do if Arrays are not objects don't you think that will create another concept in java and we need to again learn that so arrays are objects

Originally posted by Anto telvin Mathew:
hi all can anyone tell me why it is saying that array's are object ...i am beginner in java..i read so many books but not getting a correct answer ?

can anyone help

thanks
 
Pratik Surti
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Java, arrays are objects, it can be created dynamically and can be assigned to variables of type Object. All methods of class Object may be invoked on an array.

A variable of array type holds a reference to an object. Declaring a variable of array type does not create an array object or allocate any space for array components. It creates only the variable itself, which can contain a reference to an array. However, the initializer part of a declarator may create an array, a reference to which then becomes the initial value of the variable.

Example:
int[] ai;// array of int
Exception ae[] = new Exception[3];
 
Pratik Surti
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Java, arrays are objects, it can be created dynamically and can be assigned to variables of type Object. All methods of class Object may be invoked on an array.

A variable of array type holds a reference to an object. Declaring a variable of array type does not create an array object or allocate any space for array components. It creates only the variable itself, which can contain a reference to an array. However, the initializer part of a declarator may create an array, a reference to which then becomes the initial value of the variable.

Example:
int[] ai;// array of int
Exception ae[] = new Exception[3];
 
ravikdcs dixit
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
since java is object oriented you can acess instance members by creating objects or 'this' in some conditions
example
int a[]; //declares an array for integers
does not create an array but only a refernce of array that a is going to have integer type values
if array are not object how you are going to use it in your program?
example
int a[]=new int[10];//allocates memory for 10 integers
you can not access individual locations in the array if you not create object
you can also have array of Objects
example
Car[] c=new Car[10];
if it is not object then how you can create an array objects of your own class
 
Anto Telvin
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic