更多 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);
}
}
}