1. 程式人生 > >java萌新的進化旅程02

java萌新的進化旅程02

初識java(02)

這篇文章從最基本的一些東西開始講起,因為博主也剛學,所以… 講的不好請見諒,哪裡不對還請指出
涉及到的內容有:資料型別,資料型別轉換,資料型別取值範圍,裝箱拆箱,按位與或非運算

一、資料型別

首先了解資料型別有哪些
在這裡插入圖片描述
資料型別之間的關係
在這裡插入圖片描述
我們知道不同型別的資料是不能直接拿來拼湊使用的,所以這裡就涉及到了型別轉換
型別轉換分為自動型別轉換強制型別轉換
這裡舉例說明

int a=1;
byte b=2;
byte c=a+b;

像這樣從int(大型別)到byte(小型別)的轉換無需其他步驟,這樣的資料轉換稱為自動型別轉換
再舉一個例子來說明

int a=1;
byte b=2;
int c=a+b;           //錯誤
int c=(int)(a+b);     //這裡的步驟就是強制資料轉換

像這樣從byte到int(小到大)的轉換則必須用到強制資料轉換
注意:強制資料轉換時一定要注意轉換前後的資料型別,取值範圍,以及合理性
既然說到資料型別的取值範圍,那就有必要列出一些常用的資料型別取值範圍了
首先,一個位元組(byte)對應8個位數
在這裡插入圖片描述
首位(紅色)對應的為符號位,首位為0時對應為+,為1時對應為-
這樣,一個位元組所儲存的數值最大值為127,最小值為-128
這裡還要補充一個無符號資料型別
拿無符號byte來舉例,其最大值為255,最小值為0,也就是說相當於去掉了負數部分
各個資料型別的位元組數

資料型別 位元組數
byte 1
short 2
int 4
long 8
float 4
double 8
char 2 (Unicode)

按照byte的取值範圍我們可以類推出其他資料型別的取值範圍

資料型別 取值範圍
byte -128 — 127
short -32768—32767
int -2^31 — (2^31)-1
long -2^63 — (2^63)-1
char \u0000 (0) — \uffff(65535)
boolean false — true

當然還有各個基本型別對應的包裝類
byte =》Byte
short=》Short
int =》 Integer
long=》Long
float=》Float
double=》Double
char=》Character
boolean=》Boolean
這裡要講解下裝箱與拆箱

二、裝箱拆箱

首先我們要理解裝箱與拆箱的概念
裝箱:把基本型別用它們相應的引用型別包裝起來,使其具有物件的性質。int包裝成Integer、float包裝成Float以此類推
拆箱:和裝箱相反,將引用型別的物件簡化成值型別的資料
我們以int來舉例,假設我們要得到一個數值為1的Integer資料

Integer i = new Integer(10);//這是在java SE5之前的裝箱操作
//從java SE5 開始提供了自動裝箱功能
Integer i = 1; //裝箱
int n = i;     //拆箱

看似簡單的一個過程,但卻不能小瞧它所包含的內容
在這裡我們呼叫java原始碼來詳細看看

public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}

/**
* The value of the {@code Integer}.
*
* @serial
/
private final int value;
/
*
* Constructs a newly allocated {@code Integer} object that
* represents the specified {@code int} value.
*
* @param value the value to be represented by the
* {@code Integer} object.
/
public Integer(int value) {
this.value = value;
}

/
*
* Constructs a newly allocated {@code Integer} object that
* represents the {@code int} value indicated by the
* {@code String} parameter. The string is converted to an
* {@code int} value in exactly the manner used by the
* {@code parseInt} method for radix 10.
*
* @param s the {@code String} to be converted to an
* {@code Integer}.
* @exception NumberFormatException if the {@code String} does not
* contain a parsable integer.
* @see java.lang.Integer#parseInt(java.lang.String, int)
*/

這裡我們會發現在裝箱的時候自動呼叫的是Integer的valueOf(int)方法。而在拆箱的時候自動呼叫的是Integer的intValue方法。
在這裡只是介紹下裝箱和拆箱,看似簡單的東西往往會暴露出很多的問題,例如這些 點我檢視

三、按位運算

1.與
仍然是舉個例子,比如 5&3

public class Test {
	public static void main(String[] args) {
		System.out.println(5 & 3);//結果為1
	}
}

要想搞清楚與運算是如何實現的我們必須把10進位制數換為2進位制數
5轉換為二進位制:0000 0000 0000 0000 0000 0000 0000 0101
3轉換為二進位制:0000 0000 0000 0000 0000 0000 0000 0011
與的運算規則是 0與0為0,1與0為0,1與1為1
所以5&3的結果為1
1轉換為二進位制:0000 0000 0000 0000 0000 0000 0000 0001

下面我們來依次展示或、非、異或的按位運算

2.或

public class Test {
	public static void main(String[] args) {
		System.out.println(5 | 3);//結果為7
	}
}

或的運算規則是 0或0為0,1或0為1,1或1為1
5轉換為二進位制:0000 0000 0000 0000 0000 0000 0000 0101
3轉換為二進位制:0000 0000 0000 0000 0000 0000 0000 0011
7轉換為二進位制:0000 0000 0000 0000 0000 0000 0000 0111

3.非

public class Test {
	public static void main(String[] args) {
		System.out.println(~5);//結果為-6
	}
}

非的運算規則是 0變1,1變0
5轉換為二進位制:0000 0000 0000 0000 0000 0000 0000 0101
-6轉換為二進位制:1111 1111 1111 1111 1111 1111 1111 1010

4.異或

public class Test {
	public static void main(String[] args) {
		System.out.println(5 ^ 3);//結果為6
	}
}

異或的運算規則 0異或1為1,0異或0為0,1異或1為0
5轉換為二進位制:0000 0000 0000 0000 0000 0000 0000 0101
3轉換為二進位制:0000 0000 0000 0000 0000 0000 0000 0011
6轉換為二進位制:0000 0000 0000 0000 0000 0000 0000 0110

這裡還要介紹兩種特殊的位運算:左移和右移
1.左移

public class Test {
	public static void main(String[] args) {
		System.out.println(5<<2);//執行結果是20
	}
}

0000 0000 0000 0000 0000 0000 0000 0101 左移2位,右邊空位補0
0000 0000 0000 0000 0000 0000 0001 0100 執行結果為20

2.右移

public class Test {
	public static void main(String[] args) {
		System.out.println(5>>2);//執行結果是1
	}
}

0000 0000 0000 0000 0000 0000 0000 0101 右移2位,左邊空缺補0
0000 0000 0000 0000 0000 0000 0000 0001 執行結果為1

3.無符號左移(誤)
這個東西其實是沒有的,因為左移是在後面補0,而右移是在前面邊補1或0
有無符號是取決於數的前面的第一位是0還是1
所以右移是會產生到底補1還是0的問題。
而左移始終是在右邊補,不會產生符號問題。
所以沒有必要無符號左移<<<。
無符號左移<<<和左移<<是一樣的概念

4.無符號右移
我們先來比較一下右移無符號右移的區別

public class Test {
	public static void main(String[] args) {
		System.out.println(-5>>3);//結果是-1
		System.out.println(-5>>>3);//結果是536870911
	}
}

-5右移3位後結果為-1,二進位制為: 1111 1111 1111 1111 1111 1111 1111 1111 (用1進行補位)
-5無符號右移3位後的結果 536870911 二進位制為: 0001 1111 1111 1111 1111 1111 1111 1111 (用0進行補位)

這裡要寫一道面試題
如何用最快的方法把一個數據縮小到1/2倍
答案是右移兩位
因為在計算機中,位的運算要快於常規的賦值運算

到此為止,有關本篇涉及的內容以及介紹完了
最後還要說一句

記得經常儲存!!!