動手動腦和作業 軟王

YANGzLIN...11發表於2024-09-28

動手動腦1
1.System.out.println(s==t); 輸出 false:
說明 Size s = Size.SMALL; 和 Size t = Size.LARGE; 是不同的列舉常量。
2.System.out.println(s.getClass().isPrimitive()); 輸出 false:
說明 s 的型別不是原始型別
3.Size u = Size.valueOf("SMALL"); 然後 System.out.println(s == u); 輸出 true:
說明 Size.valueOf("SMALL") 方法返回的列舉常量 u 與 s 是同一個物件 Size.SMALL
4.for (Size value : Size.values()) { System.out.println(value); } 將輸出所有定義在 Size 列舉中的常量,依次輸出 SMALL, MEDIUM, LARGE
結論:列舉型別提供了一種簡潔、安全的方式來定義和使用一組常量
動手動腦2
原碼(Sign-Magnitude Representation):直接在最左邊一位表示符號,0 表示正數,1 表示負數,剩下位表示數的絕對值。
反碼(Ones' Complement):負數由其對應的正數所有位取反而得。
補碼(Two's Complement):負數由其對應的正數所有位取反再加 1 而得。

public class BinaryOperationsDemo {
public static void main(String[] args) {
int number = 5;
int negativeNumber = -5;

    // 輸出原數的二進位制表示
    System.out.println("Original number (5): " + Integer.toBinaryString(number));
    System.out.println("Original negative number (-5): " + Integer.toBinaryString(negativeNumber));

    // 驗證原碼、反碼和補碼
    System.out.println("Binary (5): " + String.format("%32s", Integer.toBinaryString(number)).replace(' ', '0'));
    System.out.println("Binary (-5): " + String.format("%32s", Integer.toBinaryString(negativeNumber)).replace(' ', '0'));

    // 移位操作
    System.out.println("After left shift (5 << 1): " + (number << 1));
    System.out.println("Binary: " + String.format("%32s", Integer.toBinaryString(number << 1)).replace(' ', '0'));

    System.out.println("After right shift logical (5 >> 1): " + (number >> 1));
    System.out.println("Binary: " + String.format("%32s", Integer.toBinaryString(number >> 1)).replace(' ', '0'));

    System.out.println("After right shift logical (-5 >> 1): " + (negativeNumber >> 1));
    System.out.println("Binary: " + String.format("%32s", Integer.toBinaryString(negativeNumber >> 1)).replace(' ', '0'));

    System.out.println("After right shift arithmetic (-5 >>> 1): " + (negativeNumber >>> 1));
    System.out.println("Binary: " + String.format("%32s", Integer.toBinaryString(negativeNumber >>> 1)).replace(' ', '0'));

    // 取反操作
    System.out.println("Bitwise NOT (~5): " + ~number);
    System.out.println("Binary: " + String.format("%32s", Integer.toBinaryString(~number)).replace(' ', '0'));

    System.out.println("Bitwise NOT (~-5): " + ~negativeNumber);
    System.out.println("Binary: " + String.format("%32s", Integer.toBinaryString(~negativeNumber)).replace(' ', '0'));
}

}
Java採用補碼
動手動腦3
區域性變數遮蔽成員變數:在exampleMethod方法中,區域性變數x遮蔽了成員變數x,因此在區域性範圍內使用的x是區域性變數。
方法引數遮蔽成員變數:在exampleMethod方法中,方法引數y遮蔽了成員變數y,因此在方法範圍內使用的y是方法引數。
for迴圈變數遮蔽外層變數:在for迴圈中,迴圈變數z遮蔽了外層的變數z,因此在迴圈內使用的z是迴圈變數。
靜態變數與例項變數:靜態變數與例項變數可以同名,但在方法中,如果沒有特別指明的是哪個變數,將使用例項變數。要訪問靜態變數,可以透過類名進行訪問。
動手動腦4
結論:由位數小和範圍小的的資料型別轉向大的無精度損失,若由同位數的資料型別轉換範圍大的向範圍小的轉換則有精度損失,整型向浮點型轉換總有精度損失。
動手動腦5
public class TestDouble {

public static void main(String args[]) {
    System.out.println("0.05 + 0.01 = " + (0.05 + 0.01));
    System.out.println("1.0 - 0.42 = " + (1.0 - 0.42));
    System.out.println("4.015 * 100 = " + (4.015 * 100));
    System.out.println("123.3 / 100 = " + (123.3 / 100));
}

}

使用double型別的數值進行計算, 其結果是不精確的,二進位制的計算可能會導致四捨五入

動手動腦6
以下程式碼的輸出結果是什麼?
int X=100;
int Y=200;
System.out.println("X+Y="+X+Y);
System.out.println(X+Y+"=X+Y");
為什麼會有這樣的輸出結果?
X+Y =100300
300=X+Y
因為字串與整數拼接:當字串與整數用 + 運算子拼接時,整數會自動轉換為字串並與之前的字串拼接;整數與字串拼接實是從左往右正常進行。
課後作業2
class pg{
public static void main(String[] args){
for(int i=0;i<30;i++){
int a=(int)(Math.random()100)+1;
int b=(int)(Math.random()
100)+1;
int n=(int)(Math.random()3);
char M='0';
switch(n){
case 0:M='+';break;
case 1:M='-';break;
case 2:M='
';break;
case 3:M='/';break;
}
System.out.printf("%d%c%d=\n",a,M,b);
}
}
}

相關文章