Lesson 4
September 23, 2000
I recommend that you also make use of my online Java tutorial lessons which are designed from a more conventional textbook approach. Those tutorial lessons are published at Gamelan.com.
For your convenience, I also maintain a consolidated Table of Contents on my personal web site that links to the individual lessons on the Gamelan site.
Insofar as possible, I will make use of Sun Java in these lessons. However, it will not be possible for me to go back and do a full update each time Sun releases a new version, so over the course of time, I expect to use different versions of Sun Java.
Just in case you would like to sneak a peek, the answers to the questions, and the explanations of those answers are located (in reverse order) at the end of this file.
The questions and the answers are connected by hyperlinks to make it
easy for you to navigate from the question to the answer and back.
It is recommended that you make your first pass through the questions in
the order that they appear so as to avoid inadvertently seeing the answer
to a question before you provide your own answer.
2. What output is produced by the following program?
class Q004_02{ public static void main( String args[]){ long size = 10; int[] array = new int[size]; size = 20; System.out.println(array.length); }//end main() }//end class definition |
3. What output is produced by the following program?
class Q004_03{ public static void main( String args[]){ byte size = 10; int[] array = new int[size]; size = 20; System.out.println(array.length); }//end main() }//end class definition |
4. True or false? Element values in Java arrays are automatically initialized when the array is constructed using the new operator.
5. When an array of char elements is constructed, the elements are automatically initialized to which of the following values:
6. What output is produced by the following program?
class Q004_06{ public static void main( String args[]){ char[] a1 = {'\u0030','\u0031'}; System.out.println(a1[1]); }//end main() }//end class definition |
7. What output is produced by the following program? (Note the placeholder comma in the boldface portion.)
class Q004_07{ public static void main( String args[]){ char[] a1 = {'\u0030',,'\u0031'}; System.out.println(a1[0]); }//end main() }//end class definition |
8. What is the name of the property of an array object that always contains the size of the array and how may it be used?
9. True or false? All Java applications and applets require a main() method.
Bonus question. The following question is considerably more difficult than the previous nine questions, and is included here to challenge you if the previous nine questions have been too easy.
10. What output is produced by the following program?
class Q004_10{ public static void main( String args[]){ SubClass ref1 = new SubClass(5); SubClass ref2 = new SubClass(10); System.out.println( ref1.add(ref2)); }//end main() }//end class definition class AClass{ private int x; AClass(int x){//constructor this.x = x; }// end constructor }//end class AClass class SubClass extends AClass{ SubClass(int x){ super(x); }//end constructor int add(AClass ref){ return x + ref.x; }//end add() }//end class SubClass |
Richard has participated in numerous consulting projects involving Java, XML, or a combination of the two. He frequently provides onsite Java and/or XML training at the high-tech companies located in and around Austin, Texas. He is the author of Baldwin's Java Programming Tutorials, which has gained a worldwide following among experienced and aspiring Java programmers. He has also published articles on Java Programming in Java Pro magazine.
Richard holds an MSEE degree from Southern Methodist University and has many years of experience in the application of computer technology to real-world problems.
Code in a method belonging to an object of a subclass
does not have direct access to private instance variables declared
in the superclass, even though those variable are inherited into and belong
to the object of the subclass. Private members can only be accessed
directly by an instance of the class in which they are declared.
class Q004_08{ public static void main( String args[]){ char[] a1 = {'a','b','c','d'}; System.out.println(a1[3] + " " + a1.length); }//end main() }//end class definition |
This program produces the following output, showing that the array contains four elements.
d 4
Note that although length is commonly referred to as a property of the array object, it is not a property in the sense of a JavaBean design-pattern property. If it were, it would not be directly accessible as shown in the above program. Rather, it would be accessible by invoking a method named getLength(). (I will have a lot more to say about JavaBean design patterns in a subsequent lesson.)
char[] a1 = {'\u0030','\u0031'};
This syntax
This is easily demonstrated by the following simple
program.
class Q004_05{ public static void main( String args[]){ char[] a1 = new char[1]; System.out.println((int)a1[0] + " " + (int)'0' + " " + (int)'1'); }//end main() }//end class definition |
This program produced the following output:
0 48 49
This output resulted from casting three values to type int and displaying the three values.
The first value displayed was the value that was placed in the array element of type char through array initialization. As you can see, the value was 0. It was necessary to cast this to type int in order to display it because there is no displayable character associated with the unicode character with a value of 0.
The next two values displayed were the unicode values for the characters '0' and '1'. These values were displayed simply to demonstrate that the unicode value for the character '0' is not numeric zero, but instead is numeric 48.
The default values are:
class Q004_04{ public static void main( String args[]){ int[] a1 = new int[1]; float[] a2 = new float[1]; boolean[] a3 = new boolean[1]; String[] a4 = new String[1]; System.out.print(a1[0] + " " + a2[0] + " " + a3[0] + " " + a4[0] + '\n'); }//end main() }//end class definition |
This program displays the default value for one element from each of the four arrays and produces the following output:
0 0.0 false null
Incompatible type for new. Explicit cast needed to convert long to int.
While it is allowable to use a variable of type byte, short, or int to specify the size of an array, you cannot specify the size of an array using a variable of type long.
Note that an array is a special kind of object in Java, and instantiation of an array object is similar to, but different from the instantiation of an ordinary object. For example, the code to instantiate an ordinary String object might be as follows:
String myVar = new String("Data");
Note the differences:
Richard has participated in numerous consulting projects involving Java, XML, or a combination of the two. He frequently provides onsite Java and/or XML training at the high-tech companies located in and around Austin, Texas. He is the author of Baldwin's Java Programming Tutorials, which has gained a worldwide following among experienced and aspiring Java programmers. He has also published articles on Java Programming in Java Pro magazine.
Richard holds an MSEE degree from Southern Methodist University and has many years of experience in the application of computer technology to real-world problems.
-end-