<< Chapter < Page Chapter >> Page >

Array object is not a subclass of class Object

An array object can be treated as type Object for purposes of calling the methods of the Object class on the reference to the array object. However, it would probably be misleading to say that an array object isinstantiated from a subclass of the Object class.

The new operator and the constructor name

Ordinary objects are created by applying the new operator to the constructor for a class, where the name of the constructor is always the same asthe name of the class. That is not the case with array objects. Array objects are created by applying the new operator to the name of the type of data to be encapsulated in the array object.

Passing parameters versus square-bracket notation

In addition, whereas the instantiation of ordinary objects involves parameters passed in parentheses, a square-bracket notation is used instead ofparentheses to create an array object. The value in the square brackets specifies the length of the array.

Creating an array object

Array objects (with default initialization values) are created by applying the new operator to the name of the data type to be stored in the array, using a square-bracket notation. An example is shown by theright-hand portion of the first statement in Listing 3 .

Listing 3 . Creating array objects.
int[] x1 = new int[5]; int[]x2 = {1,2,3,4,5};

A five-element array object

The first statement in Listing 3 creates an array object capable of storing five values of type int . The statement also assigns the array object's reference to the newly-declared reference variable named x1 .

Default initial values

Each element in the array is initialized to the default value zero.

(All array elements created in this manner receive a default initial value. Numeric primitive types receive an initial value of zero. Elements oftype boolean receive an initial value of false . Elements whose type is the name of a class or the name of an interface receive an initial valueof null .)

Explicit initialization

The second statement in Listing 3 also creates an array object capable of storing five values of type int , but in this case, the values in the elements are explicitly initialized to the values shown.

(Note that the new operator is not used in the second statement in Listing 3 . This is also a significant departure from the syntax used to instantiate ordinary objects.)

This array object's reference is assigned to the reference variable named x2 .

Note the empty square brackets in the variable declarations

The syntax of the type specification for the reference variable in each statement in Listing 3 is different from the syntax used in the type specification for either aprimitive variable or an ordinary class type reference variable (note the square brackets on the left in Listing 3 ) . In Listing 3 , the type specifications indicate that each variable is capable of holding areference to an array object.

The size of the array

Furthermore, the empty square brackets (in the declaration of the reference variable) indicate that the reference variable doesn't know (and doesn't care) about the size of the array to which it may refer. Each of the reference variables declared in Listing 3 can refer to a one-dimensional array object of any size. Also, each of the referencevariables can refer to different array objects at different points in time during the execution of the program.

Questions & Answers

if three forces F1.f2 .f3 act at a point on a Cartesian plane in the daigram .....so if the question says write down the x and y components ..... I really don't understand
Syamthanda Reply
hey , can you please explain oxidation reaction & redox ?
Boitumelo Reply
hey , can you please explain oxidation reaction and redox ?
Boitumelo
for grade 12 or grade 11?
Sibulele
the value of V1 and V2
Tumelo Reply
advantages of electrons in a circuit
Rethabile Reply
we're do you find electromagnetism past papers
Ntombifuthi
what a normal force
Tholulwazi Reply
it is the force or component of the force that the surface exert on an object incontact with it and which acts perpendicular to the surface
Sihle
what is physics?
Petrus Reply
what is the half reaction of Potassium and chlorine
Anna Reply
how to calculate coefficient of static friction
Lisa Reply
how to calculate static friction
Lisa
How to calculate a current
Tumelo
how to calculate the magnitude of horizontal component of the applied force
Mogano
How to calculate force
Monambi
a structure of a thermocouple used to measure inner temperature
Anna Reply
a fixed gas of a mass is held at standard pressure temperature of 15 degrees Celsius .Calculate the temperature of the gas in Celsius if the pressure is changed to 2×10 to the power 4
Amahle Reply
How is energy being used in bonding?
Raymond Reply
what is acceleration
Syamthanda Reply
a rate of change in velocity of an object whith respect to time
Khuthadzo
how can we find the moment of torque of a circular object
Kidist
Acceleration is a rate of change in velocity.
Justice
t =r×f
Khuthadzo
how to calculate tension by substitution
Precious Reply
hi
Shongi
hi
Leago
use fnet method. how many obects are being calculated ?
Khuthadzo
khuthadzo hii
Hulisani
how to calculate acceleration and tension force
Lungile Reply
you use Fnet equals ma , newtoms second law formula
Masego
please help me with vectors in two dimensions
Mulaudzi Reply
how to calculate normal force
Mulaudzi
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Object-oriented programming (oop) with java. OpenStax CNX. Jun 29, 2016 Download for free at https://legacy.cnx.org/content/col11441/1.201
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?

Ask