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
privateprotectedpublicabstractclassextendsfinalimplementsinterfacenativenewstaticstrictfpsynchronizedtransientvolatilebreakcasecontinuedefaultdoelseforifinstanceofreturnswitchwhilecatchfinallythrowthrowstryassertimportpackagebooleanbytechardoublefloatintlongshortsuperthisvoidconstgoto| 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 |
|||
| Object reference | null |
byte short int long |
0 |
float double |
0.0 |
boolean |
false |
char |
'\u0000' |
||
public Access can be seen by all classes from all packages.stricfp + final / abstract O.K.(class)final + abstract Wrongstricfp can modify class, method but not variable.final Classes the class can't be subclassedabstract Classes can never be instantiated(if a single method is abstract,
the whole class must be declared abstract)public Members-all other classes, regardless of the package they belong
toprivate Members-only the class where the private member was declared(When
a member is declared private, a subclass can't inherit it)(a method marked
private cannot be overridden)protected and default Members-a default member may be accessed only if
the class accessing the member belongs to the same package, whereas a protected member can be accessed by a subclass even if the subclass is in a different
package.final can ever be applied to local variables.final Methods:prevents a method from being overridden in a subclassfinal Arguments:must keep the same valueabstract Methods:contailns no functional codeabstract + private (Method) Wrongabstract methods also cannot be marked as synchronized, strictfp, native,
staticsynchronized Methods:The synchronized keyword indicates that a method can
be accessed by only one thread at a time.(synchronized can be applied only
to method)native Methods:can't with abstract and only to methodsstrictfp Methods:modify a class or nonabstract methodpublic private protected final transienttransient Variables:tell JVM to skip this variable when you attempt to
serialize the object declaring it(can be applied only to instance variables)volatile Variables:can be applied only to instance variablesstatic--Methods Variables Top-level nested classes??????static methods can't be overridenpublic static or static public)abstract class can define both an insterface can have only abstract methodsabstract!| 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 |
public, must not be staticpublic, static and finalabstract, they can't be marked final, native, strictfp,
synchronizedabstract, the implementations for all
methods defined in the interface| 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 |
|
|
== : numbers, characters, booleans,
reference variables.X |
Y |
& |
| |
^ |
|---|---|---|---|---|
0 |
1 |
0 |
1 |
1 |
0 |
0 |
0 |
0 |
0 |
1 |
1 |
1 |
1 |
0 |
1 |
0 |
0 |
1 |
1 |
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 |
switch Statementsdefault case doesn't have to come at the end of the switch.switch statements can evalutae only the byte, short, int, charfor Loopfor statement lets you declare and initialize zero, one, or multiple
variables of the same type. But only one test expression.break and continuecontinue statements must be inside a loop;break statements must be used
inside either a loop or switch statementbreak : causes the program to stop execution of the innermost looping and
strat processing the next line of code after the block.continue : causes only the current iteration of the innermost loop to cease
and the next iteration of the same loop to start if the condition of the
loop is met.try and catchcatch blocks must all follow each other, without any other statements
or blocks in between.catch block, it never returns to complete the
balance of the try block. finally block encloses code that is always executed at some point after
the try block, whether an exception was thrown or not.assert expressions that cause side effects. Assertions aren't
guaranted to always run, so you don't want behavior that changes depending
on whether assertions are enabled.private method.public methods, to check for cases that you
know are never supposed to happen.booleanboolean boo = false;
if(boo = true){}
.....
O.K
switchfinal int one = 1;
int x = 1;
switch(x){
case one : System.out.print("one");
......
}
O.K but must have final
| 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. |
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
| 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; |
| 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 |
abstract methods are methods you're forced to override.abstract classes, must have a constructor.private!).| 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. |
| 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) |
Integer i1 = new Integer(42);
Integer i1 = new Integer("42");
Character c1 = new Character('c');
Integer i2 = Integer.valueOf("101011", 2);
Integer i2 = new Integer(42); byte b = i2.byteValue();
| 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 |
| 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 | ||||||||
|
||||||||
== returns
true if the two bit patterns are identical.==; they can't use the equals()
method.== means that both reference variables
are referring to the same object.boolean, true or false.final and have overridden equals()public (finalize()
is protected).transient variables aren't appropriate for equals() and hashcode()| 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. |
static collection utility. | 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 |
MyOuter mo = new Myouter(); Myouter.MyInner inner = mo.new MyInner();
MyOuter.MyInner inner = new MyOuter().new MyInner();Member Modifiers Applied to Inner Classes : final, abstract, public,
private, protected, static, strictfpabstract and final.final. class MyOuter{
private int x = 7;
class MyInner{
public void seeOuter(){
System.out.print("shumi");
}
}
}
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();
}
}
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!!
}
interface Cookable {
public void cook();
}
class Food {
Cookable c = new Cookable() {
public void cook() {
System.out.print("anonymous cookable implement");
}
};
}
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){}
}
class BigOuter{
static class Nested{}
}
class Broom{
public static void main(String[] args){
BigOuter.Nested n = new BigOuter.Nested();
}
}
| 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. |
||
| Class Object | Class Thread | Interface Runnable |
|---|---|---|
| wait() | start() | run() |
| notify() | yield() (static) | |
| notifyAll() | sleep() (static) | |
| join() |