Java Some Tips 一些小細節(jié)

更多 Java 基礎(chǔ)知識方面的文章,請參見文集《Java 基礎(chǔ)知識》


關(guān)于局部變量的默認值

局部變量沒有默認值,無論是基本數(shù)據(jù)類型,還是引用類型。
例如,以下代碼編譯不通過:

public static void main(String[] args) throws Exception {
    // 編譯不通過
    int i;
    System.out.println(i);
}

關(guān)于構(gòu)造方法

  • 沒有 return type
  • 有 return value
  • 不會被繼承
  • 不能用 final 修飾

關(guān)于 main 方法

為什么 main 方法是 static:無需創(chuàng)建對象,無需額外分配內(nèi)存
以下兩種方式等價:
public static void main(String[] args)
static public void main(String[] args)

沒有 main 方法可以執(zhí)行程序么?YES,將代碼放在 static 塊中。(僅限 Java 7 之前)

Java 中只有值傳遞

例如如下代碼:
函數(shù) f1() 傳入引用變量 s1,產(chǎn)生一個副本 temp,指向同一個對象。通過 temp 改變對象的內(nèi)容 name,會影響到變量 s1。
函數(shù) f2() 傳入引用變量 s2,產(chǎn)生一個副本 temp,指向同一個對象。通過 s = new Student("321"); 改變 temp 的指向,不會影響到變量 s2。

public class PassByValue_Test {
    public static void f1(Student s) {
        s.name = "321";
    }

    public static void f2(Student s) {
        s = new Student("321");
    }

    public static void main(String[] args) {
        Student s1 = new Student("123");
        f1(s1);
        // 輸出 321
        System.out.println(s1.name);

        Student s2 = new Student("123");
        f2(s2);
        // 輸出 123
        System.out.println(s2.name);
    }
}

class Student {
    public String name;

    public Student(String name) {
        this.name = name;
    }
}

Java Math

  • Math.cell(1.2):不小于 1.2 的最小整數(shù),結(jié)果為 2
  • Math.floor(1.2):不大于 1.2 的最大整數(shù),結(jié)果為 1

Java Array 類

提供一系列靜態(tài)方法來創(chuàng)建和操作數(shù)組。
示例:

public static void main(String[] args) {
    Object arr = Array.newInstance(String.class, 10);
    Array.set(arr, 0, "Tom");

    System.out.println(Array.get(arr, 0));
}

數(shù)據(jù)存儲:大端 Big Endian VS 小端 Small Endian

例如 char c = 'a'; 字符 a 的 ASCII 碼為97。

  • 大端 Big Endian:低地址位存放高有效字節(jié)。存儲為:00097
  • 小端 Small Endian:低地址位存放低有效字節(jié)。存儲為:97000
    Java 使用 Big Endian

示例如下:

import java.nio.ByteBuffer;
import java.nio.ByteOrder;

public class Endian_Test {
    public static void main(String[] args) {
        char c = 'a';

        ByteBuffer buffer1 = ByteBuffer.allocate(4);
        buffer1.order(ByteOrder.BIG_ENDIAN);
        buffer1.putInt(c);

        byte[] arr1 = buffer1.array();
        for (int i = 0; i < arr1.length; i++) {
            System.out.print(arr1[i]); // 輸出 00097
        }

        ByteBuffer buffer2 = ByteBuffer.allocate(4);
        buffer2.order(ByteOrder.LITTLE_ENDIAN);
        buffer2.putInt(c);

        byte[] arr2 = buffer2.array();
        for (int i = 0; i < arr2.length; i++) {
            System.out.print(arr2[i]); // 輸出 97000
        }
    }
}

數(shù)字之間可以使用下劃線

參見 Underscores in Numeric Literals
In Java SE 7 and later, any number of underscore characters (_) can appear anywhere between digits in a numerical literal. This feature enables you, for example, to separate groups of digits in numeric literals, which can improve the readability of your code. 提高代碼可讀性
For instance, if your code contains numbers with many digits, you can use an underscore character to separate digits in groups of three, similar to how you would use a punctuation mark like a comma, or a space, as a separator.

例如:

long creditCardNumber = 1234_5678_9012_3456L;
long socialSecurityNumber = 999_99_9999L;
float pi =      3.14_15F;
long hexBytes = 0xFF_EC_DE_5E;
long hexWords = 0xCAFE_BABE;
long maxLong = 0x7fff_ffff_ffff_ffffL;
byte nybbles = 0b0010_0101;
long bytes = 0b11010010_01101001_10010100_10010010;

Java 允許長度為 0 的數(shù)組

這樣可以避免了對空指針的檢測,例如:

public class ZeroLengthArrayDemo {
    public static void main(String[] args) {
        int[] is = new int[0];

        System.out.println(is.length); // 0
        
        for(int i : is) {
            System.out.println(i);
        }
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 1. Java基礎(chǔ)部分 基礎(chǔ)部分的順序:基本語法,類相關(guān)的語法,內(nèi)部類的語法,繼承相關(guān)的語法,異常的語法,線程的語...
    子非魚_t_閱讀 34,696評論 18 399
  • 多線程、特別是NSOperation 和 GCD 的內(nèi)部原理。運行時機制的原理和運用場景。SDWebImage的原...
    LZM輪回閱讀 2,120評論 0 12
  • ———————————————回答好下面的足夠了---------------------------------...
    恒愛DE問候閱讀 1,843評論 0 4
  • iOS面試小貼士 ———————————————回答好下面的足夠了------------------------...
    不言不愛閱讀 2,251評論 0 7
  • 史上最全的iOS面試題及答案 iOS面試小貼士———————————————回答好下面的足夠了----------...
    Style_偉閱讀 2,575評論 0 35

友情鏈接更多精彩內(nèi)容