<< Chapter < Page Chapter >> Page >

Create the array object

First consider the statement shown in Listing 15 . Compare this statement with the statements shown earlier in Listing 10 and Listing 13 .

Listing 15 . Create the array object.
Object[] v3 = new Object[2];

No double square brackets

Note in particular that the statement in Listing 15 does not make use of double square brackets, as was the case in Listing 10 and Listing 13 . Thus, the statement show in Listing 15 is entirely different from the statements shown in Listing 10 and Listing 13 .

Declare a reference variable

That portion of the statement to the left of the equal sign (=) declares a reference variable capable of storing a reference to an array object whose elements are capable of storing references of the type Object (not type Object[] as in the previous examples).

Refer to the root object

This reference variable will refer to an array object that forms the root of the tree structure. However, the root object in this case will be considerablydifferent from the root objects in the previous two cases.

In the previous two cases, the elements of the root object were required to store references of type Object[] (note the square brackets). In other words, an array object whose elements are of type Object[] can only store references to other array objects whose elements are of type Object .

A more general approach

However, an array object whose elements are of type Object (as is the case here), can store:

  • References to any object instantiated from any class
  • References to array objects whose elements are of any type (primitive or reference)
  • A mixture of the two kinds of references .

Therefore, this is a much more general, and much more powerful approach.

A price to pay

However, there is a price to pay for the increased generality and power. In particular, the programmer who uses this approach must have a much betterunderstanding of Java object-oriented programming concepts than the programmer who uses the two approaches discussed earlier in this module.

Particularly true relative to first approach

This is particularly true relative to the first approach discussed earlier. That approach is sufficiently similar to the use of multi-dimensional arrays inother languages that a programmer with little understanding of Java object-oriented programming concepts can probably muddle through the syntaxbased on prior knowledge. However, it is unlikely that a programmer could muddle through this approach without really understanding what she is doing.

Won't illustrate true power

Although this approach is very general and very powerful, this sample program won't attempt to illustrate that power and generality. Rather, this sampleprogram will use this approach to emulate a traditional two-dimensional rectangular array just like the first two approaches discussed earlier. (Later, I will also use this approach for a couple of ragged arrays.)

Populate the root object

The two statements in Listing 16 create two array objects, each having three elements. Each element is capable of storing areference to any object that is assignment compatible with the Object type.

Questions & Answers

what is defense mechanism
Chinaza Reply
what is defense mechanisms
Chinaza
I'm interested in biological psychology and cognitive psychology
Tanya Reply
what does preconceived mean
sammie Reply
physiological Psychology
Nwosu Reply
How can I develope my cognitive domain
Amanyire Reply
why is communication effective
Dakolo Reply
Communication is effective because it allows individuals to share ideas, thoughts, and information with others.
effective communication can lead to improved outcomes in various settings, including personal relationships, business environments, and educational settings. By communicating effectively, individuals can negotiate effectively, solve problems collaboratively, and work towards common goals.
it starts up serve and return practice/assessments.it helps find voice talking therapy also assessments through relaxed conversation.
miss
Every time someone flushes a toilet in the apartment building, the person begins to jumb back automatically after hearing the flush, before the water temperature changes. Identify the types of learning, if it is classical conditioning identify the NS, UCS, CS and CR. If it is operant conditioning, identify the type of consequence positive reinforcement, negative reinforcement or punishment
Wekolamo Reply
please i need answer
Wekolamo
because it helps many people around the world to understand how to interact with other people and understand them well, for example at work (job).
Manix Reply
Agreed 👍 There are many parts of our brains and behaviors, we really need to get to know. Blessings for everyone and happy Sunday!
ARC
A child is a member of community not society elucidate ?
JESSY Reply
Isn't practices worldwide, be it psychology, be it science. isn't much just a false belief of control over something the mind cannot truly comprehend?
Simon Reply
compare and contrast skinner's perspective on personality development on freud
namakula Reply
Skinner skipped the whole unconscious phenomenon and rather emphasized on classical conditioning
war
explain how nature and nurture affect the development and later the productivity of an individual.
Amesalu Reply
nature is an hereditary factor while nurture is an environmental factor which constitute an individual personality. so if an individual's parent has a deviant behavior and was also brought up in an deviant environment, observation of the behavior and the inborn trait we make the individual deviant.
Samuel
I am taking this course because I am hoping that I could somehow learn more about my chosen field of interest and due to the fact that being a PsyD really ignites my passion as an individual the more I hope to learn about developing and literally explore the complexity of my critical thinking skills
Zyryn Reply
good👍
Jonathan
and having a good philosophy of the world is like a sandwich and a peanut butter 👍
Jonathan
generally amnesi how long yrs memory loss
Kelu Reply
interpersonal relationships
Abdulfatai Reply
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