一、Java基礎
1、JDK 和 JRE 有什麼區別?
JDK是面向開發人員,是java的開發工具包,包含各種類庫和工具。提供了Java的開發環境和運行環境。
JRE是面向程序員使用,核心內容就是JVM及核心類庫。
參考連接:https://blog.csdn.net/qq_39975542/article/details/81415225
2、== 和 equals 的區別是什麼?
equals()是Object中的方法。== : 是操作符。
equals() 用來檢測兩個對象是否相等 ;== 用於比較引用和基本數據類型具有不同的功能.
運行角度上,equals() 運行速度比 == 慢,因爲 == 只是比較引用。
equals():
自反性:對任意引用值X,x.equals(x)的返回值一定爲true
對稱性:對於任何引用值x,y,當且僅當y.equals(x)返回值爲true時,x.equals(y)的返回值一定爲true
傳遞性:如果x.equals(y)=true, y.equals(z)=true,則x.equals(z)=true
一致性:如果參與比較的對象沒任何改變,則對象比較的結果也不應該有任何改變
非空性:任何非空的引用值X,x.equals(null)的返回值一定爲false
== :
比較的是變量(棧)內存中存放的對象的(堆)內存地址,用來判斷兩個對象的地址是否相同,即是否是指相同一個對象。比較的是真正意義上的指針操作。
比較的是操作符兩端的操作數是否是同一個對象。
源碼展示:
//Object:
public boolean equals(Object obj) {
return (this == obj);
}
//Integer:
public boolean equals(Object obj) {
if (obj instanceof Integer) {
return value == ((Integer)obj).intValue();
}
return false;
}
//Long
public boolean equals(Object obj) {
if (obj instanceof Long) {
return value == ((Long)obj).longValue();
}
return false;
}
//String:
public boolean equals(Object anObject) {
if (this == anObject) {
return true;
}
if (anObject instanceof String) {
String anotherString = (String)anObject;
int n = value.length;
if (n == anotherString.value.length) {
char v1[] = value;
char v2[] = anotherString.value;
int i = 0;
while (n-- != 0) {
if (v1[i] != v2[i])
return false;
i++;
}
return true;
}
}
return false;
}
關於基本類型 == 的比較:
沒有指明變量的類型,默認是double型,一些運算會自動轉型,這是面試常考的。
public static void main(String[] args) {
System.out.println(1.0 == 1); //true
System.out.println(0.1 == 0.1); //true
System.out.println(1/10 == 0.1); //false
System.out.println(1/10 == 0); //true
System.out.println(2/10 == 0.2); //false
System.out.println(3/10 == 0.3); //false
System.out.println(0.1 + 0.2 == 0.3); //false
System.out.println(1 + 2 == 3); //true
System.out.println(2 - 1.1f == 0.9); //false
System.out.println(0.3f == 0.3); //false
System.out.println(getType(0.3f)); //class java.lang.Float
System.out.println(getType(0.3)); //class java.lang.Double
System.out.println(0.3f == 0.3F); //true
System.out.println(1 * 0.2 == 0.2); //true
System.out.println(1 + 0.2 == 1.2); //true
System.out.println(0.1 * 2 == 0.2); //true
System.out.println(0.1 / 10 == 0.01); //true
}
public static String getType(Object o) {
return o.getClass().toString();
}
3、兩個對象的 hashCode()相同,則 equals()也一定爲 true嗎?
不一定.
類hashCode(), equals()都可以重寫,返回值完全在於自己定義。hashCode()返回該對象的哈希碼值;equals()返回兩個對象是否相等。
一般規則:
a、兩個對象用equals()比較返回true,那麼兩個對象的hashCode()方法必須返回相同的結果。
b、兩個對象用equals()比較返回false,不要求hashCode()方法也一定返回不同的值,但是最好返回不同值,以提高哈希表性能。
c、重寫equals()方法,必須重寫hashCode()方法,以保證equals方法相等時兩個對象hashcode返回相同的值。
4、Math庫中方法
System.out.println(Math.round(-11.5)); //-11
package JavaBase;
public class MathTest {
public static void main(String[] args) {
//round: 四捨五入,float 返回int, double返回long
System.out.println(Math.round(11.2)); //11
System.out.println(Math.round(11.6)); //12
System.out.println(Math.round(-11.2)); //-11
System.out.println(Math.round(-11.6d)); //-12
System.out.println();
//rint: 四捨五入,返回double值 注意:5的時候取偶數
System.out.println(Math.rint(10.1)); //10.0
System.out.println(Math.rint(-10.1)); //-10.0
System.out.println(Math.rint(-10.95)); //-11.0
System.out.println(Math.rint(11.5)); //12.0
System.out.println(Math.rint(-11.5)); //-12.0
System.out.println(Math.rint(10.5)); //10.0
System.out.println(Math.rint(-10.5)); //-10.0
System.out.println();
//floor: 返回小的值
System.out.println(Math.floor(12.5)); //12.0
System.out.println(Math.floor(11.5)); //11.0
System.out.println(Math.floor(-12.5)); //-13.0
System.out.println(Math.floor(-11.5)); //-12.0
System.out.println(Math.floor(12.01)); //12.0
System.out.println(Math.floor(-11.25)); //-12.0
System.out.println();
//ceil: 返回大的數
System.out.println(Math.ceil(12.5)); //13.0
System.out.println(Math.ceil(11.5)); //12.0
System.out.println(Math.ceil(-12.5)); //-12.0
System.out.println(Math.ceil(-11.5)); //-11.0
System.out.println(Math.ceil(12.01)); //13.0
System.out.println(Math.ceil(-11.25)); //-12.0
System.out.println();
//abs:求絕對值
System.out.println(Math.abs(-12.5));
System.out.println(Math.abs(-11.5));
System.out.println(Math.abs(12.01));
System.out.println(Math.abs(-11.25));
System.out.println();
//sqrt: 計算平方根
System.out.println(Math.sqrt(16));
//cbrt: 計算立方根
System.out.println(Math.cbrt(125));
//pow: 計算a的b次方
System.out.println(getType(Math.pow(2,10)));
}
public static Object getType(Object o) {
return o.getClass().getName();
}
}
運行結果:
11
12
-11
-12
10.0
-10.0
-11.0
12.0
-12.0
10.0
-10.0
12.0
11.0
-13.0
-12.0
12.0
-12.0
13.0
12.0
-12.0
-11.0
13.0
-11.0
12.5
11.5
12.01
11.25
4.0
5.0
java.lang.Double
5、基本數據類型
/**
* Java Base data type;
*
* -----------------------------------------------------
* 類型 | 位數 | 佔字節 (bit數) | 範圍
* -----------------------------------------------------
* byte | 8 | 1 | -2^7 ~ 2^7 -1
* -----------------------------------------------------
* short | 16 | 2 | -2^15 ~ 2^15 -1
* -----------------------------------------------------
* int | 32 | 4 | -2^31 ~ 2^31 -1
* -----------------------------------------------------
* long | 64 | 8 | -2^63 ~ 2^63 -1
* -----------------------------------------------------
* float | 32 | 4
* -----------------------------------------------------
* double | 64 | 8
* -----------------------------------------------------
* char | 16 | 2
* -----------------------------------------------------
* boolean | 8 | 1
* -----------------------------------------------------
*
*
* 注:String 是個類,不是基本類型
*
*/
System.out.println("Byte max = " + Byte.MAX_VALUE);
System.out.println("Byte min = " + Byte.MIN_VALUE);
System.out.println("short max = " + Short.MAX_VALUE);
System.out.println("short min = " + Short.MIN_VALUE);
System.out.println("int max = " + Integer.MAX_VALUE);
System.out.println("int min = " + Integer.MIN_VALUE);
System.out.println("long max = " + Long.MAX_VALUE);
System.out.println("long min = " + Long.MIN_VALUE);
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);
運行結果:
Byte max = 127
Byte min = -128
short max = 32767
short min = -32768
int max = 2147483647
int min = -2147483648
long max = 9223372036854775807
long min = -9223372036854775808
Float max = 3.4028235E38
Float min = 1.4E-45
double max = 1.7976931348623157E308
double min = 4.9E-324
6、a = a + b 與 a += b 的區別
+= 隱式的將加操作的結果類型強制轉爲持有結果的類型。a + b 操作會將a,b提升爲int類型,然後將int類型賦值給a,假如a不是int 類型,可能會出現編譯錯誤。但是 += 操作是沒問題的。
public static void main(String[] args) {
byte a = 1;
byte b = 2;
System.out.println(getType(a + b)); //class java.lang.Integer
System.out.println(getType(a += b)); //class java.lang.Byte
}
public static String getType(Object o) {
return o.getClass().toString();
}
參考連接:https://blog.csdn.net/doujinlong1/article/details/80614158
7、32 位和 64 位的 JVM,int 類型變量的長度是多數?
32 位和 64 位的 JVM 中,int 類型變量的長度是相同的,都是 32 位或者 4 個字節。
關於32位和64位的JVM之間的區別 連接:https://www.cnblogs.com/jcj21/p/5225006.html
8、反碼,補碼,原碼,位符號運算
原碼:符號位加上真值的絕對值,即第一位表示符號位,其餘位表示值。
反碼:正數的反碼是其本身。負數的反碼是在其原碼的基礎上,符號位不變,其餘各個位取反。
補碼:正數的補碼是其本身。負數的補碼是在其原碼的基礎上,符號位不變,其餘各個位取反,然後加1.
關於浮點型的二進制表示參考連接: https://blog.csdn.net/shuangchen/article/details/4941807
位運算:
public static void main(String[] args) throws Exception {
//位異或 運算(^): 兩個數轉爲二進制,然後從高位比較如果相同就爲0,不相同就爲1.
System.out.println( 8 ^ 1); //9
//位與 運算( & ): 兩個數轉爲二進制,然後從高位比較如果兩個數爲1則爲1,否則爲0
System.out.println( 8 & 1); //0
//位或 運算(|): 兩個數轉爲二進制,然後從高位比較,兩個數只要有一個爲1則爲1,否則爲0
System.out.println( 8 | 1); //9
//位非 運算(~): 如果位爲0則爲1,如果是1則爲0
System.out.println(~8); //-9
// 位左移 運算(<<) 針對二進制,轉換成二進制後向左移位,後面用0補
System.out.println(8 << 3); //64
System.out.println(-8 << 2); //-32
// 位右移 運算(>>) 針對二進制,轉換成二進制後向右移位,後面用0補
System.out.println(8 >> 2); //2
System.out.println(-8 >> 2); //-2
// 位無符號左移 運算(>>>)
// 10進制轉二進制的時候,因爲二進制數一般分8位、 16位、32位以及64位 表示一個十進制數,所以在轉換過程中,最高位會補零。
// 在計算機中負數採用二進制的補碼錶示,10進制轉爲二進制得到的是源碼,將源碼按位取反得到的是反碼,反碼加1得到補碼
// 二進制的最高位是符號位,0表示正,1表示負。
// >>>與>>唯一的不同是它無論原來的最左邊是什麼數,統統都用0填充。
// ——比如,byte是8位的,-1表示爲byte型是11111111(補碼錶示法)
// b>>>4就是無符號右移4位,即00001111,這樣結果就是15。
System.out.println(-16 >>> 2); //1073741820
System.out.println(16 >>> 2); //4
System.out.println(16 >> 2); //4
}
9、字符型常量和字符串常量的區別
a、形式上:
字符常量是單引號引起的一個字符,字符串常量是雙引號引起的若干個字符
b、含義上:
字符常量相當於一個整形值(ASCII值),可以參加表達式運算,字符串常量代表一個地址值(該字符串在內存中存放位置)
c、佔內存大小:
字符常量只佔一個字節字符串常量佔若干個字節(至少一個字符結束標誌)
10、Java線程的生命週期
線程是操作系統能夠進行運算的最小調度單位。線程的生命週期可以分爲六中狀態。
New(初始化狀態),Runnable(可運行/運行狀態),Blocked(阻塞狀態),Waiting(無時間限制等待狀態),Timed_Waiting(有時間限制等待狀態),Terminated(終止狀態)。
參考鏈接:https://baijiahao.baidu.com/s?id=1628531347800273249&wfr=spider&for=pc