<< Chapter < Page Chapter >> Page >

This program illustrates default initialization of int , double , and boolean arrays.

The default values are as follows:

  • zero for all numeric values
  • false for all boolean values
  • all zero bits for char values
  • null for object references

An array with no elements ...

This program also illustrates that it is possible to have an array object in Java that has no elements. In this case, the value of the length property for the array object is 0.

Give me an example

For example, when the user doesn't enter any arguments on the command line for a Java application, the incoming String array parameter to the main method has a length value of 0.

Another example

It is also possible that methods that return a reference to an array object may sometimes return a reference to an array whose length is 0. The method mustsatisfy the return type requirement by returning a reference to an array object. Sometimes, there is no data to be used to populate the array, so the method willsimply return a reference to an array object with a length property value of 0.

Back to Question 15

Answer 14

A. Compiler Error

Explanation 14

Assigning array reference to type Object

As you learned in an earlier module, you can assign an array object's reference to an ordinary reference variable of the type Object . It is not necessary to indicate that the reference variable is a reference to anarray by appending square brackets to the type name or the variable name.

Only works with type Object

However, you cannot assign an array object's reference to an ordinary reference variable of any other type. For any type other than Object , the reference variable must be declared to hold a reference to an array objectby appending empty square brackets onto the type name or the variable name.

The first statement in the following fragment compiles successfully.

Object X = A; Superclass B = A;

However, the second statement in the above fragment produces a compiler error under JDK 1.3, which is partially reproduced below.

Ap077.java:22: incompatible types found : Subclass[]required: Superclass Superclass B = A;

Both Superclass and Object are superclasses of the array type referred to by the reference variable named A . However, because of theabove rule, in order to cause this program to compile successfully, you would need to modify it as shown below by adding the requisite empty square bracketsto the Superclass type name.

Object X = A; Superclass[]B = A;

Back to Question 14

Answer 13

C. 0 0 0 0 1 2

Explanation 13

Syntactical ugliness

As I indicated in an earlier module, when declaring a reference variable that will refer to an array object, you can place the empty square brackets next tothe name of the type or next to the name of the reference variable. In other words, either of the following formats will work.

int[][]A; int B[][];

What I may not have told you at that time is that you can place some of the empty square brackets in one location and the remainder in the other location.

Really ugly syntax

This is indicated by the following fragment, which declares a reference variable for a two-dimensional array of type int . Then it creates the two-dimensional array object and assigns the array object'sreference to the reference variable.

Questions & Answers

find the equation of the tangent to the curve y=2x³-x²+3x+1 at the points x=1 and x=3
Esther Reply
derivative of logarithms function
Iqra Reply
how to solve this question
sidra
ex 2.1 question no 11
khansa
anyone can help me
khansa
question please
Rasul
ex 2.1 question no. 11
khansa
i cant type here
khansa
Find the derivative of g(x)=−3.
Abdullah Reply
any genius online ? I need help!!
Guzorochi Reply
how can i help you?
Pina
need to learn polynomial
Zakariya
i will teach...
nandu
I'm waiting
Zakariya
plz help me in question
Abish
How can I help you?
Tlou
evaluate the following computation (x³-8/x-2)
Murtala Reply
teach me how to solve the first law of calculus.
Uncle Reply
teach me also how to solve the first law of calculus
Bilson
what is differentiation
Ibrahim Reply
only god knows😂
abdulkadir
f(x) = x-2 g(x) = 3x + 5 fog(x)? f(x)/g(x)
Naufal Reply
fog(x)= f(g(x)) = x-2 = 3x+5-2 = 3x+3 f(x)/g(x)= x-2/3x+5
diron
pweding paturo nsa calculus?
jimmy
how to use fundamental theorem to solve exponential
JULIA Reply
find the bounded area of the parabola y^2=4x and y=16x
Omar Reply
what is absolute value means?
Geo Reply
Chicken nuggets
Hugh
🐔
MM
🐔🦃 nuggets
MM
(mathematics) For a complex number a+bi, the principal square root of the sum of the squares of its real and imaginary parts, √a2+b2 . Denoted by | |. The absolute value |x| of a real number x is √x2 , which is equal to x if x is non-negative, and −x if x is negative.
Ismael
find integration of loge x
Game Reply
find the volume of a solid about the y-axis, x=0, x=1, y=0, y=7+x^3
Godwin Reply
how does this work
Brad Reply
Can calculus give the answers as same as other methods give in basic classes while solving the numericals?
Cosmos Reply
log tan (x/4+x/2)
Rohan
please answer
Rohan
y=(x^2 + 3x).(eipix)
Claudia
is this a answer
Ismael
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