7.3 ARE THE DEFINED VARIABLES IN JAVA INITIALIZED BY DEFAULT?


7.3 ARE THE DEFINED VARIABLES IN JAVA INITIALIZED BY DEFAULT?

The basic rule to remember for Java is that whenever a variable is defined, as opposed to just being declared but not defined, it is always default-initialized to a zero of the appropriate kind according to the entries in Table 7.2.

Table 7.2

Type

Initial Value

boolean

false

char

/u0000

all integer types

0

float

0.0f

double

0.0

object reference

null

Based on what we said in Section 7.1, the only time a variable is only declared but not defined in Java is when it makes a first-time appearance in a function without an initializer. For example, in the following program, the variable u1 is local to main; it has been declared without initialization. In Java, no memory will be allocated to such a variable until it is subsequently given a value somewhere in a program. Until that time, the variable remains undefined. It's for this reason that the commented out statement in line (B) after the declaration in line (A) elicits an error message from the compiler. By contrast, the variable u2 in line (C) is properly defined, resulting in the output shown in line (D).

 
//DefaultInit.java class User { private String name; private int age; public User() { name = "John Doe"; age = 25; } public String toString() { return name + " " + age; } } class Test { public static void main( String[] args ) { //u1 declared but not defined: User u1; //(A) // System.out.println( u1 ); // ERROR //(B) //u2 defined and initialized: User u2 = new User(); //(C) System.out.println( u2 ); // John Doe 25 //(D) } }

To show a more elaborate example that parallels the C++ example code in DefaultInit3.cc of Section 7.2.1, the following program includes a programmer-defined no-arg constructor for User in line (A), but such a constructor is not provided for the class UserGroup. Nonetheless, the object ug of type UserGroup is constructed in line (D) by invoking its system-supplied default no-arg constructor (which exists because we have not supplied the UserGroup class with any constructors at all). According to Table 7.2, in the UserGroup object constructed the data member groupName is default-initialized to null, the data member chief also to null, the data member priority to 0, and the data member members also to null. (Why the last data member, members, is set to null will be clear from our discussion on arrays in Java at the end of this chapter.) That explains the output produced by the print statement in line (E). Recall from Chapter 3 that the output shown in line (E) is produced by the override definition of the toString method for the UserGroup class in line (C).

 
//DefaultInit2.java class User { public String name; public int age; public User() { name = "John Doe"; age = 25; } //(A) public String toString() { return name + " " + age; } //(B) } class UserGroup { public String groupName; public User chief; public int priority public User[] members; public String toString() { //(C) return groupName + " " + chief + " " + priority + " " + members; } } class Test { public static void main( String[] args ) { UserGroup ug = new UserGroup(); //(D) System.out.println( ug ); // null null 0 null //(E) } }

Contrast this behavior with that of C++ where the no-arg constructor for a class is happy to invoke on its own the no-arg constructors for class-type data members if they need default-initialization. In Java, the default initialization for a class type data member (and also for the primitive type data members) is strictly according to Table 7.2.

7.3.1 Is Default Initialization Affected by Default Values for Class Members?

The following program does not provide a constructor for the User class. So the system supplies its own no-arg constructor for the object u in line (C) of main. Ordinarily, this constructor would initialize each data member of the object according to Table 7.2. But if the programmer has supplied default values for the data members, as in the program below in lines (A) and (B), those are used in lieu of the entries in the table. This explains the output of the print statement in line (D).

 
//DefaultInit3.java class User { public String name = "John Doe"; //(A) public int age = 25; //(B) public String toString() { return name + " " + age; } } class Test { public static void main( String[] args ) { User u = new User(); //(C) System.out.println( u ); // John Doe 25 //(D) } }

We mentioned earlier that C++ does not allow us to give default values to class members at class definition time (except for the special case of static const data members). On the other hand, such class definitions, as shown above, are not only legal in Java but also used frequently.

7.3.2 Is the Default Value of a Class Member Ignored If a Constructor Specifies a Value for the Member?

Our previous discussion makes it clear that a class data member possesses a default initialization according to the entries in Table 7.2, unless that value is overridden by a programmer-supplied default value as in the program DefaultInit3.java. The question now is whether this default value, either system-supplied according to Table 7.2, or programmer-supplied as in DefaultInit3.java, is ever used if a class has a regular constructor that assigns a value to a data member.

The answer to the question is, Yes. When a new object is constructed in Java with the new operator, the data members are set either to the values according to Table 7.2, or to the default values specified explicitly for the data members. It is only after the object is created in this fashion that the values of the data members are reset according to the code in the constructor.

Finally, since Java does not permit default values for the parameters of a function, the C++ method of defining a no-arg constructor by giving default values to the parameters of a regular constructor cannot be used in Java.




Programming With Objects[c] A Comparative Presentation of Object-Oriented Programming With C++ and Java
Programming with Objects: A Comparative Presentation of Object Oriented Programming with C++ and Java
ISBN: 0471268526
EAN: 2147483647
Year: 2005
Pages: 273
Authors: Avinash Kak

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net