Thinking in java 第3章 操作符 筆記+習題

Thinking in java 第3章 操作符

學習目錄


3.4 賦值

1. 基本類型存儲了實際的數值,而非指向一個對象的引用,所以在賦值時,是直接將一個地方的內容複製到了另一個地方;

    對象在賦值時實際是將引用從一個地方複製到另一個地方,修改一處會同時變兩處。

2. 因對象之間的賦值引發的問題稱爲別名現象。在方法調用中傳遞進方法的對象也爲引用。

 

3.7 關係操作符

1. 在對象中"=="比較的是地址值,對於自己定義的類來說,需要重寫equals方法來比較兩個對象的內容。

 

3.8 邏輯操作符

1. 在java中不能把非布爾值當作布爾值使用,比如把1當作true或把0當作false。

2. 短路是指一旦能明確無誤地確定整個表達式的值,就不再計算表達式剩餘的部分了。

 

3.9 直接常量

L long
D double
F float
0 八進制
0x 十六進制

1. Integer和Long中有toBinaryString()方法轉化爲二進制。

 


習題

練習1:使用“簡短的”和正常的打印語句來編寫一個程序。

略。

練習2:創建一個包含一個float域的類,並用這個類來展示別名機制。

public class E3_2 {
    private float f;

    public E3_2(float f) {
        this.f = f;
    }

    public float getF() {
        return f;
    }

    public void setF(float f) {
        this.f = f;
    }

    public E3_2() {
    }
}

public class Main {
    public static void main(String[] args) {
        E3_2 e = new E3_2(1.1f);
        E3_2 e2 = e;
        e2.setF(1.3f);
        System.out.println(e.getF());
    }
}

/*
1.3
*/

練習3:創建一個包含一個float域的類,並用這個類來展示方法調用時的別名機制。

public class E3_2 {
    private float f;

    public E3_2(float f) {
        this.f = f;
    }

    public float getF() {
        return f;
    }

    public void setF(float f) {
        this.f = f;
    }

    public E3_2() {
    }
}

public class Main {
    public static void main(String[] args) {
        E3_2 e = new E3_2(1.1f);
        change(e);
        System.out.println(e.getF());
    }

    public static void change(E3_2 e) {
        e.setF(5f);
    }
}

/*
5.0
*/

練習4:編寫一個計算速度的程序,它所使用的距離和時間都是常量。

public class E3_4 {
    public static void main(String[] args) {
        Random r = new Random();
        double s = r.nextDouble();
        double t = r.nextDouble();
        System.out.println("s = " + s + ", t = " + t + ", v = " + (s/t));
    }
}

/*
s = 0.33294763377119274, t = 0.3875726368796141, v = 0.8590586694968647
*/

練習4:創建一個名爲Dog的類,它包含兩個String域:name和says。在main()方法中,創建兩個Dog對象,一個名爲spot(它的叫聲爲“Ruff!”),另一個名爲scruffy(它的叫聲爲“Wurf!”)。然後顯示它們的名字和叫聲。

練習5:在練習5的基礎上,創建一個新的Dog索引,並對其賦值爲spot對象。

public class Dog {
    private String name;
    private String says;

    public Dog(String name, String says) {
        this.name = name;
        this.says = says;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSays() {
        return says;
    }

    public void setSays(String says) {
        this.says = says;
    }

    public Dog() {
    }

    @Override
    public String toString() {
        return "Dog{" +
                "name='" + name + '\'' +
                ", says='" + says + '\'' +
                '}';
    }
}

public class Main {
    public static void main(String[] args) {
        Dog d1 = new Dog("spot","Ruff!");
        Dog d2 = new Dog("scruffy","Wurf!");
        System.out.println(d1);
        System.out.println(d2);
        Dog d3 = d1;
        System.out.println(d3 == d1);
        System.out.println(d3.equals(d1));
    }

}

/*
Dog{name='spot', says='Ruff!'}
Dog{name='scruffy', says='Wurf!'}
true
true
*/

練習7:編寫一個程序,模擬扔硬幣的結果。

import java.util.Random;

public class E3_7 {
    public static void main(String[] args) {
        Random r = new Random();
        int i = r.nextInt(1);
        int j = r.nextInt(1);
        System.out.println("第一次爲"+func(i));
        System.out.println("第二次爲"+func(j));
        System.out.println("兩次"+(i == j?"相等":"不同"));
    }

    public static String func(int a) {
        return a == 0?"反":"正";
    }
}

/*
第一次爲反
第二次爲反
兩次相等
*/

練習8:展示用十六進制和八進制計數法來操作long值,用Long.toBinaryString()來顯示結果。

public class E3_8 {
    public static void main(String[] args) {
        long l1 = 011;
        System.out.println(""+l1+": "+Long.toBinaryString(l1));
        long l2 = 0x11;
        System.out.println(""+l2+": "+Long.toBinaryString(l2));
    }
}

/*
9: 1001
17: 10001
*/

練習9:分別顯示用float和double指數記數法所能表示的最大和最小的數字。

public class E3_9 {
    public static void main(String[] args) {
        System.out.println("float max = " + Float.MAX_VALUE);
        System.out.println("float min = " + Float.MIN_VALUE);
        System.out.println("double max = " + Double.MAX_VALUE);
        System.out.println("double min = " + Double.MIN_VALUE);
    }
}

/*
float max = 3.4028235E38
float min = 1.4E-45
double max = 1.7976931348623157E308
double min = 4.9E-324
*/

練習10-13  位運算

略。

練習14:編寫一個接收兩個字符串參數的方法,用各種布爾值的比較關係來比較這兩個字符串,然後把結果打印出來。做==和!=比較的同時,用equals()做測試。在main()裏面用幾個不同的字符串對象調用這個方法。

public class E3_14 {
    public static void main(String[] args) {
        String s1 = "abc";
        String s2 = "abc";
        func(s1,s2);
    }

    public static void func(String a, String b) {
        System.out.println(a == b);
        System.out.println(a != b);
        System.out.println(a.equals(b));
    }
}

/*
true
false
true
*/

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章