Author: shumi(CIYAWASAY)
First Release: 2004-08-06
Last Updated: 2004-08-13
Version: 0.1

強力建議使用 Mozilla 系列瀏覽
若有任何錯誤,請通知我

Ch1 Language Fundamentals
Ch2 Declarations and Access Control
Ch3 Operators and assignments
Ch4 Flow Control, Exceptions, and Assertions
Ch5 Object Orientation, Overloading and Overriding, Constructors, and Return types
Ch6 java.lang-The Math Class, String, and Wrappers
Ch7 Object and Collections
Ch8 Inner Classes
Ch9 Threads

Ch1

Keywords

Ranges of Primitives Numbers

Type Bits Bytes Min Range Max Range
byte
8
1
-27
27-1
short
16
2
-215
215-1
int
32
4
-231
231-1
long
64
8
-263
263-1
float
32
4
Not needed
Not needed
doule
64
8
Not needed
Not needed
char
can be assigned to any number type to 65535

Default value for Primitive and Reference Types

Object reference
null
byte short int long
0
float double
0.0
boolean
false
char
'\u0000'
 

Literals and Ranges of All Primitive Data Types

Ch2

Class Declarations and Modifiers

Method and Variable Declarations and Modifiers

Formatting the Main() Method

Determining Access to Class Members

Visiblity
public
protected
default
private
From the same class
O
O
O
O
From any class in the same package
O
O
O
X
a subclass in the same package
O
O
O
X
a subclass outside the same package
O
O
X
X
non-subclass class outside the package
O
X
X
X

Interface Implementation

In order to be a legal implementation class, a nonabstract implementation class must do the following:

  1. Provide concrete(nonabstract) implementations for all methods from the declared interface
  2. Follow all the rules for legal overrides
  3. Declare no checked exceptions on implementation methods other than those declared by the interface method
  4. Maintain the signature of the interface method, and maintain the same return type(but does not have to declare the exceptions declared in the interface method)

Comparsion of modifiers on variables methods classes

Classes
Methods
Variables Local Variables
final
final
final
final
public
public
public
default
protected
protected
abstract
private
private
strictfp
static
static
default
transient
abstract
volatile
strictfp
synchronized
native

Ch3

instance of Comparison

Equality Operators

Arithmetic Operators

Shift Operators

Calculating Values from a Truth Table

X
Y
&
|
^
0
1
0
1
1
0
0
0
0
0
1
1
1
1
0
1
0
0
1
1

Short-Circuit Logical operators

Logical operators

example about instanceof

interface Face{}
class Bar implements Face{}
class Foo extends Bar{}
................
First Operand instanceof Operand Result
null
Any Class or Interface type
X
Foo instance
Foo,Bar,Face,Object
O
Bar instance
Bar,Face,Object
O
Bar instance
Foo
X
Foo[]
Foo,Bar,Face
X
Foo[]
Ojbect
O
Foo[1]
Foo,Bar,Face,Object
O

 

Ch4

switch Statements

for Loop

Using break and continue

Catching an Exception Using try and catch

Working with the Assertion Mechanism

example about boolean

boolean boo = false;
if(boo = true){}
.....

O.K

example about switch

final int one = 1;
int x = 1;
switch(x){
	case one : System.out.print("one");
	......
}
O.K but must have final

Cause of Early Loop Termination

Code in Loop What Happens
break Execution jumps immediately to the first statement after the for loop.
return Execution immediately jumps back to the calling method
System.exit() All program execution stops; the JVM shuts down.

compare break & continue

for(int i =0; i <= 10; i++){
	if((i % 2) == 0){
		break;
		(continue;)
		System.out.print(i);
	}
}

output:

continue: 1 3 5 7 9

break: nothing

Exception class hierarchy

Legal and Illegal aeesrt Expressions

Expressions1 Expressions2
Legal Illegal Legal Illegal
assert(x == 2)
assert(x = 2)
: "x is " + x
: void
boolean z = true;
assert(z)
int z = 0; 
assert(z)
public int go(){return 1}

: go();
public void go(){}
: go(); 
assert false
assert 1
: new Foo();
: Foo f;

Assertion Command-Line Switches

Command-Line Example What it means
java -ea
java -enableassertions
Enable assertions
java -da
java -disableassertions
Disable assertions
java -ea:com.foo.Bar 
Enable assertions in class com.foo.Bar
java -ea:com.foo..
Enable assertions in package com.foo ,and any of its subpackages
java -esa -dsa
Enable assertions in general, but disable assertions in system classes
java -ea -da:com.foo....
Enable assertions in general, but disable assertions in package com.foo and any of its subpackages

Ch5

Overriding and Overloading

Instantiation and Constructors

Return Types

Difference Between Overloaded and Overridden Methods

  Overloaded Overridden
argument list Must change Must not change

return type

Can change Must not change
exceptions Can change can reduce or eliminate. Must not throw new or broader checked exception.
access Can change Must not make more restrictive(can be less restrictive)
invocation reference type determines which overloaded version. Happens at compile time. Object type determines which method is selected. Happens at runtime.

Ch6

Using the java.lang.Math Class

Important Static Math Class Method Signatures

Static Methods examples
double ceil(double d) Math.ceil(-9.4/-9.0/-9.8) //-9.0
double floor(double d)

Math.floor(9.4/9.0/9.8) //9.0

Math.floor(-9.0/-8.8/-8.1) //-9.0

double random() (int)(Math.random()*10) //0~9
int abs(int i) Math.abs(-99) //99
float abs(float f)  
double abs(double d)  
long abs(long l)  
int max(int a, int b) Math.max(1024, -5000) //1024
float max(float a, float b)  
double max(double a, double b)  
long max(long a, long b)  
int min(int a, int b) Math.min(1024, -5000) //-5000
float min(float a, float b)  
double min(double a, double b)  
long min(long a, long b)  
double sqrt(double d) Math.sqrt(9.0) //3.0
double toDegrees(double d) Math.toDegrees(Math.PI*2.0) //360.0
double toRadians(double d) Math.toRadians(360.0) //6.283185
double sin(double d) Math.sin(Math.toRadians(90.0) ) //1.0
double cos(double d) Math.cos(Math.toRadians(0.0)) //1.0
double tan(double d) Math.tan(Math.toRadians(45.0)) //1.0
int round(float f)

Math.round(10.5) //11

Math.round(-10.5) //-10

long round(double d)  

Using Wrappers

Wrapper Classes and Their Constructor Arguments

Primitive Wrapper Class Constructor Arguments
boolean Boolean boolean or String
byte Byte byte or String
char Character char
double Double double or String
float Float float, double, String
int Integer int or String
long Long long or String
short Short short or String

Common Wrapper Conversion Methods

Method Boolean Byte Charaeter Double Float Integer Long Short
byteValue   O   O O O O O
doubleValue   O   O O O O O
floatValue   O   O O O O O
intValue   O   O O O O O
shortValue   O   O O O O O
parseXxx s n   O   O O O O O
(with radix)
parseXxx s n
  O       O O O
valueOf s n O O   O O O O O
(with radix)
valueOf s n
  O       O O O
toString O O O O O O O O
(primitive)
toString s
  O   O O O O O
(primitive, radix)
toString s
          O O  
toBinaryString s           O O  
toHexString s           O O  
toOctalString s           O O  
s = static n =NFE exception
  • primitive xxxValue() -to convert a Wrapper to a primitive
  • primitive parseXxx(String) -to convert a String to a primitive
  • Wrapper valueOf(String) -to convert a String to a Wrapper

Key facts to remember about comparing variables

  1. The rule is the same for reference variables and primitive variables : == returns true if the two bit patterns are identical.
  2. Primitive variables must use ==; they can't use the equals() method.
  3. for reference variables, == means that both reference variables are referring to the same object.

Key points about the equals() method

  1. equals() is used only to compare objects.
  2. equals() returns a boolean, true or false.
  3. The StringBuffer class has not overridden equals()
  4. The String and wrapper classes are final and have overridden equals()

Ch7

Overriding hashCode() and equals()

Garbage Collection

Methods of Class Object Covered on the Exam

Method Description
boolean equals(Object obj) Decides whether two objects are meaningfully equivalent.
void finalize() When the garbage collector sees the object cannot be referenced.
int hashCode() Returns a hashcode int value for an object, so that the object can be used in Collection classes that uses hashing, including Hashtable, HashMap, HashSet.
final void notify() Wakes up a thread that is waiting for this object's lock.
final void notifyAll() Wakes up all threads that are waiting for this object's lock.
final void wait() Causes the current thread to wait until another calls notify or notifyAll on this object.
String toString() Returns a "text representation" of the object.

The equals() Contract

finalize()

Three meanings for "collection":

Three basic flavors of collections include Lists, Sets, Maps.

Collections

Collection Interface Concrete Implemention Classes

Class Map Set List Ordered Sorted
HashMap O     No No
Hashtable O     No No
TreeMap O     Sorted By natural order or custom comparison rules
LinkedHashMap O     By insertion order or last access order No
HashSet   O   No No
TreeSet   O   Sorted By natural order or custom comparison rules
LinkedHashSet   O   By insertion order or last access order No
ArrayList     O By index No
Vector     O By index No
LinkedList     O By index No

Ch8

Inner Classes

Method-Local Inner Classes

example about four inner classes

  1. Regular Inner Class
    class MyOuter{
       private int x = 7;
       class MyInner{
          public void seeOuter(){
             System.out.print("shumi");
          }
       }
    }
  2. Method-Local Inner Class
    class MyOuter2{
       private String x = "Outer2";
       void doStuff(){
          class MyInner{
             public void seeOuter(){
                System.out.print("shumi");
             }
          }
          MyInner mi = new MyInner();//This must come after the class
          mi.seeOuter();
       }
    }
  3. Plain Old Anoymous Inner Classes, Flavor One
    class Popcorn{
       public void pop(){
          System.out.print("popcorn");
       }
    }
    
    class Food{
       Popcorn p = new Popcorn(){
          public void pop(){
             System.out.print("anonymous popcorn")
          }
       };//Don't miss it!!
    }
  4. Plain Old Anonymous Inner Classes, Flavor Two
  5. interface Cookable {
        public void cook();
    }
    class Food {
       Cookable c = new Cookable() {
          public void cook() {
             System.out.print("anonymous cookable implement");
          }
       };
    }
  6. Argument-Defoned Anonymous Inner Class
  7. class MyWonderfulClass {
       void go() {
          Bar b = new Bar();
          b.doStuff(new Foo() {
             public vid foof() {
                System.out.pront("foofy");
             }
          });
       }
    }
    interface Foo(){
       void foof();   
    }
    class Bar{
       void doStuff(Foo f){}
    }
  8. Static Nested Classes
  9. class BigOuter{
       static class Nested{}
    }
    class Broom{
       public static void main(String[] args){
          BigOuter.Nested n = new BigOuter.Nested();
       }
    }

Ch9

Thread states

Methods and Look Status

Give Up Lock Kep Locks Class Defining the method
wait() notify()(p.s.) java.lang.Object
  join() java.lang.Thread
  sleep() java.lang.Thread
  yield() java.lang.Thread
p.s.Although the thread will probably exit the synchronized code shortly after this call, and thus give up is locks.

Key Thread Methods

Class Object Class Thread Interface Runnable
wait() start() run()
notify() yield() (static)  
notifyAll() sleep() (static)  
  join()