<< Chapter < Page Chapter >> Page >
int[] A[]; A = new int[2][3];

While it doesn't matter which location you use for the square brackets in the declaration, it does matter how many pairs of square brackets you place in thetwo locations combined. The number of dimensions on the array (if you want to think of a Java array as having dimensions) will equal the total number of pairs of empty square brackets in the declaration of the reference variable. Thus, in this case, the array is a two-dimensional arraybecause there is one pair of square brackets next to the type and another pair next to the variable name.

This program goes on to use nested for loops to populate the array and then to display the contents of the elements.

I personally don't use this syntax, and I hope that you don't either. However, even if you don't use it, you need to be able to recognize it when usedby others.

Back to Question 13

Answer 12

C. 1 2

Explanation 12

The length property

This program illustrates the use of the array property named length , whose value always matches the number of elements in the array.

As a Java programmer, you will frequently call methods that will return a reference to an array object of a specified type, but of an unknown length. (See, for example, the method named getEventSetDescriptors that is declared in the interface named BeanInfo .) This program simulates that situation.

Returning a reference to an array

The method named returnArray returns a reference to an array of type int having two elements. Although I fixed the size of the array in this example, I could just as easily have used a random number to set adifferent size for the array each time the method is called. Therefore, the doArrays method making the call to the method named returnArray has no way of knowing the size of the array referred to by the reference that it receivesas a return value.

All array objects have a length property

This could be a problem, but Java provides the solution to the problem in the length property belonging to all array objects.

The for loop in the method named doArrays uses the length property of the array to determine how many elements it needs to display. This is a very commonscenario in Java.

Back to Question 12

Answer 11

A. Compiler Error

Explanation 11

You cannot cast primitive array references

You cannot cast an array reference from one primitive type to another primitive type, even if the individual elements in the array are of a type thatcan normally be converted to the new type.

This program attempts to cast a reference to an array of type int[] and assign it to a reference variable of type double [].Normally, a value of type int will be automatically converted to type double whenever there is a need for such a conversion. However, this attempted cast produces the following compiler error under JDK1.3.

Ap074.java:19: inconvertible types found : int[]required: double[] double[]B = (double[])A;

Why is this cast not allowed?

I can't give you a firm reason why such a cast is not allowed, but I believe that I have a good idea why. I speculate that this is due to the fact that theactual primitive values are physically stored in the array object, and primitive values of different types require different amounts of storage. For example, the type int requires 32 bits of storage while the type double requires 64 bits of storage.

Questions & Answers

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
What would be the best educational aid(s) for gifted kids/savants?
Heidi Reply
treat them normal, if they want help then give them. that will make everyone happy
Saurabh
What are the treatment for autism?
Magret Reply
hello. autism is a umbrella term. autistic kids have different disorder overlapping. for example. a kid may show symptoms of ADHD and also learning disabilities. before treatment please make sure the kid doesn't have physical disabilities like hearing..vision..speech problem. sometimes these
Jharna
continue.. sometimes due to these physical problems..the diagnosis may be misdiagnosed. treatment for autism. well it depends on the severity. since autistic kids have problems in communicating and adopting to the environment.. it's best to expose the child in situations where the child
Jharna
child interact with other kids under doc supervision. play therapy. speech therapy. Engaging in different activities that activate most parts of the brain.. like drawing..painting. matching color board game. string and beads game. the more you interact with the child the more effective
Jharna
results you'll get.. please consult a therapist to know what suits best on your child. and last as a parent. I know sometimes it's overwhelming to guide a special kid. but trust the process and be strong and patient as a parent.
Jharna
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