Lesson 2
Rev, February 20, 2001
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.
Type | Size in Bits | |
A | boolean | 1 |
B | char | 8 |
C | byte | 8 |
D | short | 16 |
E | int | 32 |
F | long | 64 |
G | float | 32 |
H | double | 64 |
2. boolean states: Which of the following are the possible states of a boolean variable?
3. Range of integer types: The following
table purports to show the numeric range of the integer types in terms
of a power of two. Identify which lines, if any, contain errors.
(The syntax 2eX means 2 raised to the X power.)
Type | Minimum | Maximum | |
A | byte | -2e7 | (2e7)-1 |
B | short | -2e15 | (2e15)-1 |
C | int | -2e31 | (2e31)-1 |
E | long | -2e63 | (2e63)-1 |
4. Signed versus unsigned True or false? All of the integral types, including char are signed.
5. Character representation: True or false? Characters in Java are represented by the 8-bit Extended ASCII character set.
6. Unicode to ASCII relationship: What is the relationship between the 16-bit Unicode character set and the 7-bit ASCII character set.
7. Invalid numeric values: As a result of certain arithmetic operations (such as division by zero), certain primitives can take on bit patterns that do not represent valid numeric values. These possibilities are represented by symbolic constants in the various classes. Which of the following are not valid symbolic constants?
8. Literal values: Which, if any, of the following are not valid literal values for type char?
9. Escape characters: Which of the following are valid escape characters in Java?
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. Bit manipulations: What output is produced by the following program?
class Q002_10{ public static void main(String args[]){ int x = 1; int y = ~x + 1; System.out.println(y); }//end main() }//end class definition |
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.
In fact, this is how you convert a positive twos complement binary value to a negative twos complement binary value (or vice versa). First you invert all of the bits. Then you add 1.
B. '\t' is also a valid literal value of type char. This is the so-called escape character that represents a Tab. There are about eight such escape characters supported by Java that represent various things like Tab, Newline, Return, Backspace, etc.
C. "a" is not a valid literal of type char. Rather, this is a String literal.
In Java, unlike some other languages, quotation marks (") and apostrophes (') have entirely different meanings.
Zero or more characters surrounded by quotation marks are interpreted by the compiler to constitute a String literal.
Any single character surrounded by apostrophes is interpreted by the compiler to be a char literal. When confronted with such a literal, the compiler produces the numeric value that represents that character. For example, the following code fragment will cause the value 65 to be displayed because the numeric value that represents the upper-case A in the Unicode character set is 65.
int data = 'A';
System.out.println(data);
D. 'end' has no meaning in Java. Unlike some other languages, such as Python, you cannot interchange quotation marks and apostrophes when creating strings. For example, the following statement will produce the compiler error "Invalid character constant"
System.out.println('end');
An object of the Integer class is intended to encapsulate an int value. There are no operations that you can perform with an int that results in a bit pattern that does not represent a valid numeric value.
While it is possible to end up with erroneous int values as a result of some arithmetic operations, they are still valid numeric values and so there is no need for the kind of symbolic constants described for float and double in this question.
As it turns out, for those of us who program using the U.S./English alphabet, the change from Extended ASCII to Unicode isn't very significant. We can pretty much continue doing what we have been doing for years.
However, those who program using alphabets from other spoken languages now have the ability to reflect those languages in the character representations contained in their programs.
However, Java does not use the ASCII character
set. Java characters are expressed as 16-bit Unicode characters.
(To learn more about Unicode characters, visit the following URL.)
In summary, this means that a variable containing n bits has the following numeric range (where 2eX means 2 raised to the X power):
Minimum = -2e(n-1)
Maximum = (2e(n-1))-1
Consider the following simple example. Assume that n is
equal to 3. Then:
Minimum = -2e(3-1) = -4
Maximum = (2e(3-1))-1 = 4-1 = 3
A three-bit variable can represent eight different combinations of bits.
In two's complement notation, it can represent the range of integer values
from -4 to +3. Here is how the bits would be organized to represent
those values in two's complement binary notation.
Bits | Value |
000 | 0 |
001 | 1 |
010 | 2 |
011 | 3 |
100 | -4 |
101 | -3 |
110 | -2 |
111 | -1 |
The boolean type is often used in conditional expressions to make a choice between two possibilities. For example, the following construct would not work properly if the boolean were allowed to have the third value uncertain. In that case, you might find yourself stranded on the roadside with an empty gas tank.
if(gasInTank < 1)
buyGas(); //need to buy gas
else
keepDriving();//don't need to buy gas
You may wonder why this is the case. One of the primary reasons is the need to internationalize computer technology. An 8-bit character set can represent only 255 different characters. During that part of history when most computer technology was based on the U.S./English alphabet, this was sufficient. However, computer technology is no longer so confined, and there is a need to be able to represent more than 255 characters.
The 16-bit Unicode set makes it possible to represent more than 65,000 different characters. Hopefully this will be sufficient to represent the alphabets of all countries in the international computer technology arena.
You may also wonder about the correctness of line A which indicates that the boolean type is 1 bit in size. I personally don't know exactly how a boolean variable is represented in memory, but I would have guessed that it is stored in an 8-bit byte. I also don't know how to write any code that will expose the manner in which a boolean variable is stored in memory.
The size of one bit for a boolean is based on information from the book entitled The Complete Java 2 Certification Study Guide by Roberts, Heller, and Ernest. A boolean certainly could be stored in a single bit since only two different values must be represented. This assumes, of course, that the processor has the ability to address memory down to the single-bit level.
In the final analysis, it probably doesn't matter. Unlike C and C++, you cannot do arithmetic with a boolean, and I can't think of any other operation that you can perform on a boolean where the manner in which it is stored in memory makes any difference (except possibly for speed).
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-