==========================
create by wythe 2016/2/15 21:49
在看TIJ之前,已經(jīng)有了C/C++的一點(diǎn)基礎(chǔ)。最近因?yàn)閰⒓訉W(xué)校的軟件編程比賽置吓,做移動端的app犁嗅,馬馬虎虎算是弄完了边涕。在做得過程發(fā)現(xiàn)自己的java不熟悉(菜的一筆),所以閑時就看看這本經(jīng)典褂微,做些筆記
一 功蜓、 Introduction to Object
- 主數(shù)據(jù)類型(Primitive Type)
類型 | 大小 | 默認(rèn)值 |
---|---|---|
byte | 8 bits | (byte)0 |
char | 16 bits | '\u0000'(null) |
boolean | 1 bit | false |
short | 16 bits | (short)0 |
int | 32 bits | 0 |
long | 64 bits | 0L |
float | 64 bits | 0.0f |
double | 64 bits | 0.0d |
若主數(shù)據(jù)類型屬于一個類成員,則會以上述的默認(rèn)值初始化宠蚂。但是局部變量會得到隨機(jī)值
99
二 式撼、 Everything is Object
-
String str = new String("hello world")
str
為句柄Java通過句柄(str)來操作對象;
句柄(str)在它的作用域(Scope)終點(diǎn)自動銷毀,然而句柄指向的對象還占據(jù)內(nèi)存空間求厕; - 內(nèi)存
類型 | 作用 |
---|---|
寄存器(Registers) | 我們沒有直接控制權(quán)著隆,只能由編譯器分配 |
堆棧(The stack) | 對象句柄的儲存的地方 |
堆(The heap) | 對象的儲存地方 |
- 注意點(diǎn)
如果您的main()用package語句封裝到一個文件里,那么必須在程序名前面指定完整的包裹名稱呀癣,否則不能運(yùn)行程序美浦。
三 、 Operator
- ==和项栏!=運(yùn)算符
關(guān)系運(yùn)算符==和!=也適用于所有對象浦辨。下面是一個例子:
//:
Equivalence.java public class Equivalence {
public static void main(String[] args) {
Integer n1 = new Integer(47);
Integer n2 = new Integer(47);
System.out.println(n1 == n2);
System.out.println(n1 != n2);
}
} ///:~
其中,表達(dá)式System.out.println(n1 == n2)
可打印出內(nèi)部的布爾比較結(jié)果沼沈。一般人都會認(rèn)為輸出結(jié)果肯定先是true流酬,再是false,因?yàn)閮蓚€Integer對象都是相同的列另。但盡管對象的內(nèi)容相同芽腾,句柄卻是不同的,而==和!=比較的正好就是對象句柄访递。所以輸出結(jié)果實(shí)際上先是false晦嵌,再是true。這自然會使第一次接觸的人感到驚奇。 若想對比兩個對象的實(shí)際內(nèi)容是否相同惭载,又該如何操作呢旱函?此時,必須使用所有對象都適用的特殊方法equals()描滔。但這個方法不適用于“主類型”棒妨,那些類型直接使用==和!=即可。由于equals()的默認(rèn)行為是比較句柄含长。所以除非在自己的新類中改變了equals()券腔,否則不可能表現(xiàn)出我們希望的行為
- 移位運(yùn)算符
1、左移位運(yùn)算符(<<)能將運(yùn)算符左邊的運(yùn)算對象向左移動運(yùn)算符右側(cè)指定的位數(shù)
(在低位補(bǔ)0)拘泞;
2纷纫、“有符號”右移位運(yùn)算符(>>)則將運(yùn)算符左邊的運(yùn)算對象向右移動運(yùn)算符右側(cè)指定
的位數(shù)∨汶纾“有符號”右移位運(yùn)算符使用了“符號擴(kuò)展”:若值為正辱魁,則在高位插入0;若值為負(fù)诗鸭,則在高位插入1染簇;
3、Java也添加了一種“無符號”右移位運(yùn)算符(>>>)强岸,它使用了“零擴(kuò)展”:無論正負(fù)锻弓,都在高位插入0;
- 主數(shù)據(jù)類型(int蝌箍、long etc.)的二進(jìn)制為補(bǔ)碼形式青灼,用以下代碼驗(yàn)證
//:Main.java
//Test for Learning Java
package com.company;
import java.util.*;
public class Main {
public static void main(String[] args) {
// write your code here
int i = -1;
printIntBinary(i);
printIntBinary(i>>3);
printIntBinary(i>>>3);
}
static void printIntBinary(int a){
for(int j = 31; j >= 0; --j) {
if((( 1 << j ) & a ) != 0)
System.out.print("1");
else
System.out.print("0");
}
System.out.println();
}
}
///:The end~
結(jié)果:
- javadoc sample.class
通過代碼的注釋,自動生成api文檔 教程
四 十绑、 Initialization & Clean up
- 對象初始化
static變量在非static變量前初始化
示例:
class Man{
static int cnt;
String name;
//static區(qū)塊用于初始化static變量
static{
cnt=1;
}
//非靜態(tài)變量的初始化區(qū)塊,支持"匿名內(nèi)部類"的初始化
{
name = "Mike";
}
Man(){
cnt++;
}
}
- 數(shù)組初始化
1聚至、主數(shù)據(jù)類型(int、char etc.):表達(dá)式
int[] a = new int[size]
產(chǎn)生長度為size
的int
數(shù)組本橙,初始化值為默認(rèn)值0
.
2扳躬、對象:表達(dá)式Interger[] a = new Interger[size]
產(chǎn)生一個句柄數(shù)組,具體的對象空間未分配甚亭。此時贷币,數(shù)組元素的值為null
。通過a[i] = new Interger(2)
來關(guān)聯(lián)對象.
- 多維數(shù)組
多維數(shù)組可以任意指定各維的大小
示例:
int [][][] a = new int[randInt()][][];
for(int i = 0; i < a.length; ++i){
a[i] = new int[randInt()][];
for(int j = 0; j < a[i].length; ++j){
a[i][j] = new int[randInt()];
for(int k = 0; k < a[i][j].length; ++k)
a[i][j][k] = i*j*k;
}
}
- finalize()方法
Java提供了一個名為finalize()的方法亏狰,我們可以在自己類中定義它役纹。它的工作原理:當(dāng)垃圾收集器準(zhǔn)備好釋放對象占用的儲存空間,它會首先調(diào)用finalize()暇唾,而且只有在下一次垃圾收集過程中促脉,才會真正回收對象內(nèi)存辰斋。因此,可以用finalize()在垃圾收集期間進(jìn)行一些重要的清除工作
示例:
//: initialization/TerminationCondition.java
// Using finalize() to detect an object that
// hasn’t been properly cleaned up.
class Book {
boolean checkedOut = false;
Book(boolean checkOut) {
checkedOut = checkOut;
}
void checkIn() {
checkedOut = false;
}
protected void finalize() {
if(checkedOut)
System.out.println("Error: checked out");
// Normally, you’ll also do this:
// super.finalize();
// Call the base-class version
}
}
public class TerminationCondition {
public static void main(String[] args) {
Book novel = new Book(true);
// Proper cleanup:
novel.checkIn();
// Drop the reference, forget to clean up:
new Book(true);
// Force garbage collection & finalization:
System.gc();
}
}
/* Output: Error: checked out *///:~
五 瘸味、Access Control
- 修飾符
1宫仗、
friendly
默認(rèn)的訪問修飾符,它不是顯式的旁仿,不用特意來使用它藕夫。它指明可在包內(nèi)訪問,包外不可訪問枯冈。
2毅贮、protected
在包內(nèi)訪問上與friendly
一致。但protected
可訪問性比friendly
強(qiáng)尘奏。
可看以下圖片滩褥,紅色為語法錯誤
六 、Reusing Classes
- final關(guān)鍵字
1罪既、自變量final:例如铸题,
void sampleMethod(final sampleClass s)
中,無法改變自變量s
句柄的指向(對象)琢感;
2、空白final:例如探熔,
class blankFinal{
final int i;
blankFinal(){
i = 1;
}
}
在對象初始化時驹针,對final進(jìn)行正確的賦值;
七诀艰、 Polymorphism
- 多形性實(shí)例
//:Main.java
//Test for Learning Java
package com.company;
import com.company.tools.*;
class Instrument{
public void play(){
CustomPrint.print("Instrument~");
}
}
class Wind extends Instrument{
public void play(){
CustomPrint.print("Wind~");
}
}
class Brass extends Instrument{
public void play(){
CustomPrint.print("Brass~");
}
}
class Brass2 extends Brass{
public void play(){
CustomPrint.print("Brass2~");
}
}
public class Main {
public static void tune(Instrument isn){
isn.play();
}
public static void main(String[] args) {
Brass brass = new Brass();
Brass2 brass2 = new Brass2();
tune(brass);
tune(brass2);
}
}
///:The end~

如果柬甥,
將上述的`play()`方法,加上static關(guān)鍵字`public static void play()`,則結(jié)果是:

- 接口
>***1其垄、接口介紹***
1)接口不規(guī)定方法主體苛蒲;
2)接口可以聲明**基本數(shù)據(jù)類型**的數(shù)據(jù)成員,它們都***默認(rèn)***為static 和final绿满;
3)與類相似臂外,我們可在interface關(guān)鍵字的前面添加一個 public關(guān)鍵字(但只有接口定義于同名的一個文件內(nèi));或者將其省略喇颁,營造一種“friendly”狀態(tài);
4)接口中的方法聲明默認(rèn)為 public漏健。所以在實(shí)現(xiàn)一個接口的時候,來自接口的方法***必須***定義成public橘霎。不然它們會默認(rèn)為“friendly"
就像這樣蔫浆,
interface Instrument5 {
// Compile-time constant:
int i = 5; // static & final
// Cannot have method definitions:
void play(); // Automatically public
String what();
void adjust();
}
接口也具有多形性。下面這段代碼的結(jié)果和`多形性實(shí)例`的結(jié)果一致:
//:Main.java
//Test for Learning Java
package com.company;
import com.company.tools.*;
interface Instrument{
void play();
}
class Wind implements Instrument {
public void play(){
CustomPrint.print("Wind~");
}
}
class Brass implements Instrument {
public void play(){
CustomPrint.print("Brass~");
}
}
class Brass2 extends Brass{
public void play(){
CustomPrint.print("Brass2~");
}
}
public class Main {
public static void tune(Instrument isn){
isn.play();
}
public static void main(String[] args) {
Brass brass = new Brass();
Brass2 brass2 = new Brass2();
tune(brass);
tune(brass2);
}
}
///:The end~
**2姐叁、接口實(shí)現(xiàn)“多重繼承”**
>就像這樣瓦盛,`class samlpe implements interface1,interface2,interface3`洗显。sample可以將類型上溯至interface1,interface2原环,interface3中的任意一種墙懂。下面是一個實(shí)例:
//: Adventure.java
// Multiple interfaces import java.util.*;
interface CanFight { void fight(); }
interface CanSwim { void swim(); }
interface CanFly { void fly(); }
class ActionCharacter { public void fight() {} }
class Hero extends ActionCharacter
implements CanFight, CanSwim, CanFly {
public void swim() {}
public void fly() {}
}
class Adventure {
static void t(CanFight x) {
x.fight();
}
static void u(CanSwim x) {
x.swim();
}
static void v(CanFly x) {
x.fly();
}
static void w(ActionCharacter x) {
x.fight();
}
public static void main(String[] args) {
Hero i = new Hero();
t(i); // Treat it as a CanFight
t(i); // Treat it as a CanSwim
v(i); // Treat it as a CanFly
w(i); // Treat it as an ActionCharacter
}
} ///:~
**3、接口能夠繼承接口**
>像這樣`interface sample extends interface1,interface2,interface3`扮念,形成更豐富的新接口
**4损搬、接口產(chǎn)生枚舉數(shù)組**
>就像這樣,
public interface Months{
int
JANUARY = 1, FEBRUARY = 2, MARCH = 3,
APRIL = 4, MAY = 5, JUNE = 6, JULY = 7,
AUGUST = 8, SEPTEMBER = 9, OCTOBER = 10,
NOVEMBER = 11, DESEMBER = 12;
}
接口內(nèi)的基本數(shù)據(jù)類型默認(rèn)為***final*** **&** ***static***,可以***Months.JANUARY***的方式訪問柜与。
- 內(nèi)部類
>**1巧勤、內(nèi)部類,一個被定義在現(xiàn)有類內(nèi)部的新類弄匕。**
像這樣颅悉,
public class Parcel1 {
class Contents {
private int i = 11;
public int value() { return i; }
}
class Destination {
private String label;
Destination(String whereTo) {
label = whereTo;
}
String readLabel() { return label; }
}
public Contents cont(){
return new Contens();
}
public Destination to(){
return new Destination();
}
// Using inner classes looks just like
// using any other class, within Parcel1:
public void ship(String dest) {
Contents c = new Contents();
Destination d = new Destination(dest);
}
public void ship2(String dest){
Contents c = cont();
Destination d = to(dest);
}
public static void main(String[] args) {
Parcel1 p = new Parcel1();
p.ship("Tanzania");
Parcel.Contents c = p.cont();
}
}
**2、可在方法或者if語句等的作用域內(nèi)內(nèi)嵌一個內(nèi)部類迁匠,但是這個內(nèi)部類僅限在這個作用域內(nèi)使用**
**3剩瓶、匿名類,方法尾部return一個內(nèi)部類**
像這樣城丧,
public Content cont(){
return new Content(){
private int i = 0;
public int value(){return i;}延曙;
}
}
new 表達(dá)式返回的句柄會自動上溯到Content,由此亡哄,可以重寫Content的方法來隱藏實(shí)現(xiàn)細(xì)節(jié)1-·
**4枝缔、當(dāng)一個新類繼承一個外部類時,外部類內(nèi)嵌的內(nèi)部類不會被自動繼承蚊惯。因此愿卸,無法再新類中簡單地覆蓋內(nèi)部類**
實(shí)例:
//: BigEgg.java
// An inner class cannot be overriden
// like a method
class Egg {
protected class Yolk {
public Yolk() {
System.out.println("Egg.Yolk()");
}
}
private Yolk y;
public Egg() {
System.out.println("New Egg()");
y = new Yolk();
}
}
public class BigEgg extends Egg {
public class Yolk {
public Yolk() {
System.out.println("BigEgg.Yolk()");
}
}
public static void main(String[] args) {
new BigEgg();
}
} ///:~
輸出:New Egg( )
Egg.Yolk( )
**5、可以用“外部類.內(nèi)部類”的方式來繼承內(nèi)部類截型,并可用這種方法來覆蓋父類內(nèi)部類的方法**
下面是例子:
//: BigEgg2.java
// Proper inheritance of an inner class
class Egg2 {
protected class Yolk {
public Yolk() {
System.out.println("Egg2.Yolk()");
}
public void f() {
System.out.println("Egg2.Yolk.f()");
}
}
private Yolk y = new Yolk();
public Egg2() {
System.out.println("New Egg2()");
}
public void insertYolk(Yolk yy) { y = yy; }
public void g() { y.f(); }
}
public class BigEgg2 extends Egg2 {
public class Yolk extends Egg2.Yolk {
public Yolk() {
System.out.println("BigEgg2.Yolk()");
}
public void f() {
System.out.println("BigEgg2.Yolk.f()");
}
}
public BigEgg2() { insertYolk(new Yolk()); }
public static void main(String[] args) {
Egg2 e2 = new BigEgg2();
e2.g();
}
} ///:~
輸出: Egg2.Yolk()
New Egg2()
Egg2.Yolk()
BigEgg2.Yolk()
BigEgg2.Yolk.f()
#####八趴荸、Honding Your Objects
- continue配合標(biāo)簽使用,可以連跳多級循環(huán)
>```
retry:
while(true){
int t=
(int)(Math.random()*flav.length);
for(int j=0;j<i;j++)
if(picks[j]==t)continue retry;
}
- 命令行下編譯運(yùn)行sample.java
//cd 到com文件夾的上一級(如果是在IED(IJ)中生成的)
javac sample.java
//文件不含包名
java sample
//如果文件包含“package com.package”,則
java com.package.sample
還需要注意,“CLASSPATH=.;%JAVA_HOME%/lib/dt.jar;%JAVA_HOME%/lib/tools.jar”宦焦。其中发钝,".;"表示當(dāng)前路徑
九、Strings
- StringBuilder
- 正則表達(dá)式
String mp = "\w+@(\w+\.)+[A-Za-z]{2,14}";
String mail = "vicent@fzu.edu.cn";
Pattern p = Pattern.compile(mp);
Matcher m = p.matcher(mail);