Lesson 3
September 9, 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. True or false? By default, an integral literal is a 64 bit value.
3. There are seven characters, at least one of which must be included to indicate a floating point literal. What are they?
4. True or false? The default for a floating-point literal without an F or a D is a 32-bit float.
5. Show how to represent a String literal.
6. A Java array is an ordered collection of three kinds of things. What are they?
7. True or false? All elements in a Java array must be of the same type.
8. There are two different formats that can be used to declare a reference variable capable of containing a reference to a single-dimensional array. What are they?
9. True or false? The size of a Java array can be specified using a variable or a literal.
Bonus question. The following question is more difficult than the previous nine questions, and is included here to challenge you if the previous nine questions have been too easy.
10. Consider the following simple program, which initializes the instance variable named myIntVar to a value of 10 when the instance variable is initialized.
What value will be displayed by this program?
class Q56{ int myIntVar = 10;//instance variable Q56(){//constructor myIntVar = 20; }//end constructor public static void main(String args[]){ System.out.println(new Q56().myIntVar); }//end main() }//end class Q7 |
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.
This is a very powerful capability because it means that you do not need to know the size of an array when you write and compile a program. Rather, you can defer that decision until runtime.
For example, this capability makes it possible
to write code such as the following. Although in this case, I initialized
the value stored in the variable named size when I declared it,
that value could come from anywhere (such as the length of a file) before
being used to establish the size of the array.
class Q003_09{ public static void main( String args[]){ int size = 10; int[] array = new int[size]; size = 20; System.out.println( size + " " + array.length); }//end main() }//end class definition |
It is very important to understand, however, that once the value stored in a variable is used to establish the size of the array, changing the value stored in the variable does not cause the size of the array to change.
It is also true that all of the elements in an array that contains primitives must be of the same primitive type. (However, under certain circumstances, primitives stored in an array will be automatically converted to the type of elements declared for the array if they are not already of that type.)
However, through the use of polymorphism, it is possible to store references to many different true types of objects in a Java array. When you store an object reference in an array, that reference can be stored in a reference that has been declared to be any of the following types:
. E e F f D d |
28, 034, 0x1c, 0x1C, 0X1c, 0X1C |
Octal representation was widely used in the earlier days when the number of bits in each word of computer memory was often a multiple of three, such as 12, 24, 36, etc. However, with the advent of memory word sizes that are not multiples of three, the usefulness of octal representation has diminished.
In case you are interested, octal numbers consist of the following digits: 0, 1, 2, 3, 4, 5. 6. and 7. Note that there are eight unique digits in the octal number system.
If you preface the literal value with either 0x or 0X, that indicates that you are specifying the value as a hexadecimal value. A hexadecimal number is a number that is represented according to the base sixteen.
Hexadecimal representation came into wide use when the defacto standard for the number of bits in each byte of computer memory became eight which is a multiple of four. Hexadecimal numbers are commonly represented using the following digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, and F. Note that there are sixteen unique digits in the hexadecimal number system. In Java, the individual letters that are used as digits can be either upper case or lower case.
Because the X used to preface the literal value and the letter used in the literal vale can be either upper case or lower case, there are four different ways to express the decimal value 28 in hexadecimal notation.
The following list shows six ways to express the decimal value 28 as a literal integer. Four of these use hexadecimal notation, one is octal, and the other is decimal. The second column identifies the notation being used.
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-