Wednesday, October 15, 2008

Language Fundamental, Prepare for SCJP

A friend of mine ask me to get SCJP (Sun Certified Java Programmer) with him, but in his office, he will get change of the voucher if he pass the test, unfortunately not in mine. But in case I have nothing to do right now, so Im reading ebook "Java 2 : Sun Certified Java Programmer & Developer" by Kathy Sierra & Bert Bates. Both of them are SCJP team. I download it from esnips, if the link is broken, just try to google it,
I got the link also from googling first.

This post, and perhaps my next post are just as my summary of my reading, the first part of the book is Language Fundamental.

Java Programming Language Keywords


Keywords are special reserved words in Java that you cannot use as identifiers (names) for classes, methods, or variables. They have meaning to the compiler. None of them have capital letters,

Access Modifiers
- private Makes a method or a variable accessible only from within its own class.
- protected Makes a method or a variable accessible only to classes in the same package or subclasses of the class.
- public Makes a class, method, or variable accessible from any other class.

Class, Method, and Variable Modifiers
- abstract Used to declare a class that cannot be instantiated, or a method that must be implemented by a nonabstract subclass.
- class Keyword used to specify a class.
- extends Used to indicate the superclass that a subclass is extending.
- final Makes it impossible to extend a class, override a method, or reinitialize a variable.
- implements Used to indicate the interfaces that a class will implement.
- interface Keyword used to specify an interface.
- native Indicates a method is written in a platform-dependent language, such as C.
- new Used to instantiate an object by invoking the constructor.
- static Makes a method or a variable belong to a class as opposed to an instance.
- strictfp Used in front of a method or class to indicate that floating-point numbers will follow FP-strict rules in all expressions.
- synchronized Indicates that a method can be accessed by only one thread at a time.
- transient Prevents fields from ever being serialized. Transient fields are always skipped when objects are serialized.
- volatile Indicates a variable may change out of sync because it is used in threads.

Flow Control
- break Exits from the block of code in which it resides.
- case Executes a block of code, dependent on what the switch tests for.
- continue Stops the rest of the code following this statement from executing in a loop and then begins the next iteration of the loop.
- default Executes this block of code if none of the switch-case statements match.
- do Executes a block of code one time, then, in conjunction with the while statement, it performs a test to determine whether the block should be executed again.
- else Executes an alternate block of code if an if test is false.
- for Used to perform a conditional loop for a block of code.
- if Used to perform a logical test for true or false.
- instanceof Determines whether an object is an instance of a class, superclass, or interface.
- return Returns from a method without executing any code that follows the statement (can optionally return a variable).
- switch Indicates the variable to be compared with the case statements.
- while Executes a block of code repeatedly while a certain condition is true.

Error Handling
- catch Declares the block of code used to handle an exception.
- finally Block of code, usually following a try-catch statement, which is executed no matter what program flow occurs when dealing with an exception.
- throw Used to pass an exception up to the method that called this method.
- throws Indicates the method will pass an exception to the method that called it.
- try Block of code that will be tried, but which may cause an exception.
- assert Evaluates a conditional expression to verify the programmer’s assumption.

Package Control
- import Statement to import packages or classes into code.
- package Specifies to which package all classes in a source file belong.


- boolean A value indicating true or false.
- byte An 8-bit integer (signed).
- char A single Unicode character (16-bit unsigned)
- double A 64-bit floating-point number (signed).
- float A 32-bit floating-point number (signed).
- int A 32-bit integer (signed).
- long A 64-bit integer (signed).
- short A 16-bit integer (signed).

Variable Keywords
- super Reference variable referring to the immediate superclass.
- this Reference variable referring to the current instance of an object.

Void Return Type Keyword
- void Indicates no return type for a method.

Unused Reserved Words

- const Do not use to declare a constant; use public static final.
- goto Not implemented in the Java language. It’s considered harmful.

The question might appear to be asking about, say, a runtime logic problem, but the real problem will be that the code won’t even compile because of the illegal use of a keyword. For example, the following code will not compile:

class Foo {
public void go() {
// complex code here
public int break(int b) {
// code that appears to break something

Meanwhile, you’re trying to figure out the complex code within the methods, when you needn’t look beyond the illegal method name and choose the “Code does not compile” answer.

According to the Java Language Specification, null, true, and false are technically literal values (sometimes referred to as manifest constants) and not keywords. Just as with the other keywords, if you try to create an identifier with one of these literal values, you’ll get a compiler error. For the purposes of the exam, treat them just as you would the other reserved words. You will not be asked to differentiate
between reserved words and these reserved literals. “Note: There will not be any questions regarding esoteric distinctions between keywords and manifest constants.”

Literals and Ranges of All Primitive Data Types
All six number types in Java are signed, meaning they can be negative or positive. The leftmost bit (the most significant digit) is used to represent the sign, where a 1 means negative (glass half empty) and 0 means positive (glass half full).

The positive range is one less than the negative range because the number zero is stored as a positive binary number. We use the formula -2^(bits - 1) to calculate the negative range, and we use 2^
(bits - 1)–1 for the positive range.

For boolean types there is not a range; a boolean can be only true or false. If someone asks you for the bit depth of a boolean, look them straight in the eye and say, “That’s virtual-machine dependent.” They’ll be impressed. :D

The char type (a character) contains a single, 16-bit Unicode character. Char is really an integer type, it can be assigned to any number type large enough to hold 65535.

Type Bits Bytes Minimum Range Maximum Range
byte 8 1 -2^7 2^7–1
short 16 2 -2^15 2^15–1
int 32 4 -2^31 2^31–1
long 64 8 -2^63 2^63–1
float 32 4 Not needed Not needed
double 64 8 Not needed Not needed

Literal Values for All Primitive Types
Literal = source code representation

Integer Literals
three ways to represent integer numbers in the Java language:
- decimal (base 10), int length = 343;
- octal (base 8), by placing a zero in front of the number
class Octal {
public static void main(String [] args) {
int five = 06; // Equal to decimal 6
int seven = 07; // Equal to decimal 7
int eight = 010; // Equal to decimal 8
int nine = 011; // Equal to decimal 9
System.out.println("Octal 010 = " + eight);
- hexadecimal (base 16), including the prefix 0x or the optional suffix extension L
class HexTest {
public static void main (String [] args) {
int x = 0X0001;
int y = 0x7fffffff;
int z = 0xDeadCafe;
System.out.println("x = " + x + " y = " + y + " z = " + z);

All three integer literals (octal, decimal, and hexadecimal) are defined as int by default, but they may also be specified as long by placing a suffix of L or l after the number:
long jo = 110599L;
long so = 0xFFFFl; // Note the lowercase 'l'

Floating-Point Literals
Floating-point literals are defined as double (64 bits) by default, so if you want to assign a floating-point literal to a variable of type float (32 bits), you must attach the suffix F or f to the number.

float f = 23.467890; // Compiler error, possible loss of precision
float g = 49837849.029847F; // OK; has the suffix "F"

Boolean Literals
Boolean literals are the source code representation for boolean values. A boolean
value can only be defined as true or false. Although in C (and some other
languages) it is common to use numbers to represent true or false, this will
not work in Java. Again, repeat after me, “Java is not C++.”
boolean t = true; // Legal
boolean f = 0; // Compiler error!

Character Literals

A char literal is represented by a single character in single quotes.
char a = 'a';
char b = '@';
You can also type in the Unicode value of the character, using the Unicode notation of prefixing the value with \u as follows:
char letterN = '\u004E'; // The letter 'N'

Remember, characters are just 16-bit unsigned integers under the hood. That means you can assign a number literal, assuming it will fit into the unsigned 16-bit range (65535 or less). For example, the following are all legal:
char a = 0x892; // octal literal
char b = 982; // int literal
char c = (char) 70000; // The cast is required; 70000 is out of char range
char d = (char) -98; // Ridiculous, but legal
And the following are not legal and produce compiler errors:
char e = -29; // Possible loss of precision; needs a cast
char f = 70000 // Possible loss of precision; needs a cast

You can also use an escape code if you want to represent a character that can’t be typed in as a literal, including the characters for linefeed, newline, horizontal tab, backspace, and double and single quotes.
char c = '\"'; // A double quote
char d = '\n'; // A newline

Literal Values for Strings
A string literal is a source code representation of a value of a String object. For
example, the following is an example of two ways to represent a string literal:
String s = "Bill Joy";
System.out.println("Bill" + " Joy");

Array Declaration, Construction, and Initialization
Arrays are objects in Java that store multiple variables of the same type.

For this objective, you need to know three things:
■ How to make an array reference variable (declare)
■ How to make an array object (construct)
■ How to populate the array with elements (initialize)

Declaring an Array
Arrays are declared by stating the type of element the array will hold, which can be an object or a primitive, followed by square brackets to the left or right of the identifier.

Declaring an Array of Primitives
int[] key; // Square brackets before name (recommended)
int key []; // Square brackets after name (legal but less readable)
Declaring an Array of Object References
Thread[] threads; // Recommended
Thread threads []; // Legal but less readable

It is never legal to include the size of the array in your declaration. You might see a question or two that include code similar to the following:
int[5] scores;
The preceding code won’t make it past the compiler. Remember, the JVM doesn’t allocate space until you actually instantiate the array object.

Constructing an Array
Constructing an array means creating the array object on the heap—in other words, doing a new on the array type. To create an array object, Java needs to know how much space to allocate on the heap, so you must specify the size of the array at construction time. The size of the array is the number of elements the array will hold.

Constructing One-Dimensional Arrays
The most straightforward way to construct an array is to use the keyword new followed by the array type, with a bracket specifying how many elements of that type the array will hold. The following is an example of constructing an array of type int:
int[] testScores; // Declares the array of ints
testScores = new int[4]; //constructs an array and assigns it
//the testScores variable

You can also declare and construct an array in one statement as follows:
int[] testScores = new int[14];
This single statement produces the same result as the two previous statements.
Arrays of object types can be constructed in the same way:
Thread[] threads = new Thread[5];

The exam will expect you to know, for example, that the preceding code produces just one object (the array assigned to the reference variable named threads). The single object referenced by threads holds five Thread reference variables, but no Thread objects have been created or assigned to those references.

Remember, arrays must always be given a size at the time they are constructed. The JVM needs the size to allocate the appropriate space on the heap for the new array object. It is never legal, for example, to do the following:
int[] carList = new int[]; // Will not compile; needs a size

Multidimensional arrays, remember, are simply arrays of arrays. So a two-dimensional array of type int is really an object of type int array (int []), with each element in that array holding a reference to another int array. The second dimension holds the actual int primitives.
The following code declares and constructs a two-dimensional array of type int:
int[][] ratings = new int[3][];
Notice that only the first brackets are given a size. That’s acceptable in Java, since the JVM needs to know only the size of the object assigned to the variable ratings.

Initializing an Array
Initializing an array means putting things into it.

Look for code that tries to access an out of range array index. For example, if an array has three elements, trying to access the [3] element will raise an ArrayIndexOutOfBoundsException, because in an array of three elements, the legal index values are 0, 1, and 2.

Be sure to recognize that these cause runtime exceptions and not compiler errors! Nearly all of the exam questions list both runtime exception and compiler error as possible answers.
int[] x = new int[5];
x[4] = 2; // OK, the last element is at index 4
x[5] = 3; // Runtime exception. There is no element at index 5!

int [] z = new int[2];
int y = -3;
z[y] = 4; // Runtime exception.; y is a negative number

A two-dimensional array (an array of arrays) can be initialized as follows:
int[][] scores = new int[3][];
// Declare and create an array holding three references to int arrays
scores[0] = new int[4];
// the first element in the scores array is an int array of four int element
scores[1] = new int[6];
// the second element in the scores array is an int array of six int elements
scores[2] = new int[1];
// the third element in the scores array is an int array of one int element

Initializing Elements in a Loop
Array objects have a single public variable length that gives you the number of elements in the array. The last index value, then, is always one less than the length. For example, if the length of an array is 4, the index values are from 0 through 3. Often, you’ll see array elements initialized in a loop as follows:
Dog[] myDogs = new Dog[6]; // creates an array of 6 Dog references
for (int x = 0; x < style="font-weight: bold;">Declaring, Constructing, and Initializing on One Line

int[] dots = {3,6,9,8};
The size (length of the array) is determined by the number of items between the comma-separated curly braces.

Dog puppy = new Dog("Frodo");
Dog[] myDogs = {puppy, new Dog("Clover"), new Dog("Aiko")};
Four objects are created:
- 1 Dog object referenced by puppy
- 1 Dog [ ] object referenced by myDogs
- 2 Dog objects referenced by myDogs[1] and myDogs[2]

Cat[][] myCats = {{new Cat("Fluffy"), new Cat("Zeus")},{new Cat("Belbo"), new Cat("Legolas"), new Cat("Bert")}}
Eight objects are created:
- 1 2-D Cat[ ][ ] object referenced by myCat
- 2 Cat[ ] array objects
- 5 Cat objects

Constructing and Initializing an Anonymous Array
The second shortcut is called anonymous array creation and can be used to construct and initialize an array, and then assign the array to a previously declared array reference variable:
int[] testScores;
testScores = new int[] {4,7,2};
The use is for example, as an argument to a method that takes an array parameter. The following code demonstrates a just-in-time array argument:
public class Foof {
void takesAnArray(int [] someArray) {
// use the array parameter
public static void main (String [] args) {
Foof f = new Foof();
f.takesAnArray(new int[] {7,7,8,2,5}); //we need an array argument

Remember that you do not specify a size when using anonymous array creation syntax. The size is derived from the number of items (comma-separated) between the curly braces. Pay very close attention to the array syntax used in exam questions (and there will be a lot of them). You might see syntax such as
new Object[3] {null, new Object(), new Object()};
// not legal;size must not be specified

Arrays of Primitives
Primitive arrays can accept any value that can be promoted implicitly to the declared type of the array. Chapter 3 covers the rules for promotion in more detail, but for an example, an int array can hold any value that can fit into a 32-bit int variable. Thus, the following code is legal:
int[] weightList = new int[5];
byte b = 4;
char c = 'c';
short s = 7;
weightList[0] = b; // OK, byte is smaller than int
weightlist[1] = c; // OK, char is smaller than int
weightList[2] = s; // OK, short is smaller than int

Arrays of Object References
If the declared array type is a class, you can put objects of any subclass of the declared type into the array. For example, if Dog is a subclass of Animal, you can put both Dog objects and Animal objects into the array as follows:
class Car {}
class Subaru extends Car {}
class Honda extends Car {}
class Ferrari extends Car {}
Car [] myCars = {new Subaru(), new Honda(), new Ferrari()};

If the array is declared as an interface type, the array elements can refer to any instance of any class that implements the declared interface.

Array Reference Assignments for One-Dimensional Arrays
It's not talking about references in the array (in other words, array elements), but rather references to the array object. For example, if you declare an int array, the reference variable you declared can be reassigned to any int array (of any size), but cannot be reassigned to anything that is not an int array, including an int value. Remember, all arrays are objects, so an int array reference cannot refer to an int primitive. The following code demonstrates legal and illegal assignments for primitive arrays:
int[] splats;
int[] dats = new int[4];
char[] letters = new char[5];
splats = dats; // OK, dats refers to an int array
splats = letters; // NOT OK, letters refers to a char array

Arrays that hold object references, as opposed to primitives, aren’t as restrictive. Just as you can put a Honda object in a Car array (because Honda extends Car), you can assign an array of type Honda to a Car array reference variable as follows:
Car[] cars;
Honda[] cuteCars = new Honda[5];
cars = cuteCars; // OK because Honda is a type of Car
Beer[] beers = new Beer [99];
cars = beers; // NOT OK, Beer is not a type of Car

Array Reference Assignments for Multidimensional Arrays
- the array you’re assigning must be the same dimension as the reference you’re assigning it to.
int[] blots;
int[][] squeegees = new int[3][];
blots = squeegees; // NOT OK, squeegees is a two-d array of int arrays
int[] blocks = new int[6];
blots = blocks; // OK, blocks is an int array
- It must assigning array, not a primitif type
int[][] books = new int[3][];
int[] numbers = new int[6];
int aNumber = 7;
books[0] = aNumber; //NOT OK, expecting an int array instead of an int
books[0] = numbers; //OK, numbers is an int array

Using a Variable or Array Element That Is Uninitialized and Unassigned
- instance variable is declared within the class but outside any method or constructor
- local variable is declared within a method (or in the argument list of the method)
the compiler complains if you try to use a local variable before initializing it.

Primitive Instance Variables
public class BirthDate {
int year; // Instance variable
public static void main(String [] args) {
BirthDate bd = new BirthDate();
public void showYear() {
System.out.println("The year is " + year);
When the program is started, it gives the variable year a value of zero, the default value for primitive number instance variables.

Variable Type Default Value
Object reference null (not referencing any object)
byte, short, int, long 0
float, double 0.0
boolean false
char ‘\u0000’

Object Reference Instance Variables
The following modification to the Book code runs into trouble:
public class Book {
private String title;
public String getTitle() {
return title;
public static void main(String [] args) {
Book b = new Book();
String s = b.getTitle(); // Compiles and runs
String t = s.toLowerCase(); // Runtime Exception!
When we try to run the Book class, the JVM will produce the following error:
%java Book
Exception in thread "main" java.lang.NullPointerException
at Book.main(

Array Instance Variables
An array is an object; thus, an array instance variable that’s declared but not explicitly initialized will have a value of null, just as any other object reference instance variable.
But...if the array is initialized, what happens to the elements contained in the array? All array elements are given their default values—the same default values that elements of that type get when they’re instance variables. The bottom line: Array elements are always always always given default values, regardless of where the array itself is declared or instantiated.

If we initialize an array, object reference elements will equal null if they are not initialized individually with values. If primitives are contained in an array, they will be given their respective default values. For example, in the following code, the array year will contain 100 integers that all equal zero by default:
public class BirthDays {
static int [] year = new int[100];
public static void main(String [] args) {
for(int i=0;i<100;i++)
System.out.println("year[" + i + "] = " + year[i]);
When the preceding code runs, the output indicates that all 100 integers in the array equal zero.

Local (Stack, Automatic) Primitives and Objects
Local Primitives
In the following time travel simulator, the integer year is defined as an automatic variable because it is within the curly braces of a method.
public class TimeTravel {
public static void main(String [] args) {
int year = 2050;
System.out.println("The year is " + year);
Okay, so we’ve still got work to do on the physics. Local variables, including primitives, always always always must be initialized before you attempt to use them (though not necessarily on the same line of code). Java does not give local variables a default value; you must explicitly initialize them with a value, as in the preceding example. If you try to use an uninitialized primitive in your code, you’ll get a compiler error:
public class TimeTravel {
public static void main(String [] args) {
int year; // Local variable (declared but not initialized)
System.out.println("The year is " + year); // Compiler error
Compiling produces the following output:
%javac Variable year may not have been initialized.
System.out.println("The year is " + year);
1 error

Local Objects
Objects, too, behave differently when declared within a method rather than as instance variables. With instance variable object references, you can get away with leaving an object reference uninitialized, as long as the code checks to make sure the reference isn’t null before using it. Remember, to the compiler, null is a value. You can’t use the dot operator on a null reference, because there is no object at the other end of it, but a null reference is not the same as an uninitialized reference. Locally declared references can’t get away with checking for null before use, unless you explicitly initialize the local variable to null. The compiler will complain about the following code:
import java.util.Date;
public class TimeTravel {
public static void main(String [] args) {
Date date;
if (date == null)
System.out.println("date is null");
Compiling the code results in the following error:
%javac Variable date may not have been initialized.
If (date == null)
1 error

Instance variable references are always given a default value of null, until explicitly initialized to something else. But local references are not given a default value; in other words, they aren’t null. If you don’t initialize a local reference variable, then by default, its value is...well that’s the whole point—it doesn’t have any value at all! So we’ll make this simple: Just set the darn thing to null explicitly, until you’re ready to initialize it to something else. The following local variable will compile
Date date = null; // Explicitly set the local reference variable to null

Local Arrays
Just like any other object reference, array references declared within a method must be assigned a value before use. That just means you must declare and construct the array. You do not, however, need to explicitly initialize the elements of an array. We’ve said it before, but it’s important enough to repeat: array elements are given their default values (0, false, null, ‘\u0000’, etc.) regardless of whether the array is declared as an instance or local variable. The array object itself, however, will not be initialized if it’s declared locally. In other words, you must explicitly initialize an array reference if it’s declared and used within a method, but at the moment you construct an array object, all of its elements are assigned their default values.

Command-Line Arguments to Main
Now that you know all about arrays, command-line arguments will be a piece of cake. Remember that the main method—the one the JVM invokes—must take a String array parameter. That String array holds the arguments you send along with the command to run your Java program, as follows:
class TestMain {
public static void main (String [] args) {
System.out.println("First arg is " + args[0]);
When invoked at the command line as follows,
%java TestMain Hello
the output is
First arg is Hello

No comments: