數據類型
- 每一種數據都定義了明確的數據類型,在內存中分配了不一樣大小的內存空間(字節)。
![在這裏插入圖片描述](http://static.javashuo.com/static/loading.gif)
- java 數據類型分爲兩大類 基本數據類型, 引用類型
- 基本數據類型有 8 種, 數值型 [byte , short , int , long , float ,double] char , boolean
- 引用類型 [類,接口, 數組]
1. 整數類型
1.1 基本介紹
- Java的整數類型就是用於存放整數值的,好比12,30,3456等等
1.2 案例演示
byte nl =10;
short n2 =10;
int n3 = 10;
long n4= 10;
1.3 整型的類型
![在這裏插入圖片描述](http://static.javashuo.com/static/loading.gif)
1.4 整型的使用細節 IntDetail.java
public class IntDetail {
public static void main(String[] args) {
int n1 = 1;
long n3 = 1L;
}
}
![在這裏插入圖片描述](http://static.javashuo.com/static/loading.gif)
![在這裏插入圖片描述](http://static.javashuo.com/static/loading.gif)
2. 浮點類型
2.1 基本介紹
- Java的浮點類型能夠表示一個小數,好比123.4 , 7.8 ,0.12 等等
2.2 案例演示
double score = 66.6;
2.3 浮點型的分類
![在這裏插入圖片描述](http://static.javashuo.com/static/loading.gif)
2.4 說明一下
- 關於浮點數在機器中存放形式的簡單說明,
浮點數=符號位+指數位+尾數位
- 尾數部分可能丟失,形成精度損失(
小數都是近似值
)。
2.5 浮點型使用細節
- 注意:Java 默認浮點常量爲
double
型,聲明 float
類型常量,須加 「f」或 「F」
![在這裏插入圖片描述](http://static.javashuo.com/static/loading.gif)
public class FloatDetail {
public static void main(String[] args) {
float num2 = 1.1F;
double num3 = 1.1;
double num4 = 1.1f;
double num5 = .123;
System.out.println(num5);
System.out.println(5.12e2);
System.out.println(5.12E-2);
double num9 = 2.1234567851;
float num10 = 2.1234567851F;
System.out.println(num9);
System.out.println(num10);
double num11 = 2.7;
double num12 = 8.1 / 3;
System.out.println(num11);
System.out.println(num12);
if( num11 == num12) {
System.out.println("num11 == num12 相等");
}
if(Math.abs(num11 - num12) < 0.000001 ) {
System.out.println("差值很是小,到個人規定精度,認爲相等...");
}
System.out.println(Math.abs(num11 - num12));
}
}
3. 字符類型(char)
3.1 基本介紹
- 字符類型能夠表示單個字符,字符類型是
char
,char
是兩個字節(能夠存放漢字),多個字符咱們用字符串 String
3.2 案例演示
char c1 = 'a';
char c2 = '\t';
char c3 = '兮';
char c4 = 97;
System.out.println(c1);
System.out.println(c2);
System.out.println(c3);
System.out.println(c4);
![在這裏插入圖片描述](http://static.javashuo.com/static/loading.gif)
3.3 字符類型使用細節
![在這裏插入圖片描述](http://static.javashuo.com/static/loading.gif)
- Unicode編碼轉換:http://tool.chinaz.com/Tools/Unicode.aspx
- a 轉換後爲 97
![在這裏插入圖片描述](http://static.javashuo.com/static/loading.gif)
![在這裏插入圖片描述](http://static.javashuo.com/static/loading.gif)
char c1 = 97;
System.out.println(c1);
char c2 = 'a';
System.out.println((int)c2);
char c3 = '兮';
System.out.println((int)c3);
char c4 = 20846;
System.out.println(c4);
System.out.println('a' + 10);
char c5 = 'b' + 1;
System.out.println((int)c5);
System.out.println(c5);
![在這裏插入圖片描述](http://static.javashuo.com/static/loading.gif)
3.4 字符類型本質探討
![在這裏插入圖片描述](http://static.javashuo.com/static/loading.gif)
3.5 ASCII 碼介紹(瞭解)
![在這裏插入圖片描述](http://static.javashuo.com/static/loading.gif)
3.6 Unicode 編碼介紹(瞭解)
![在這裏插入圖片描述](http://static.javashuo.com/static/loading.gif)
3.7 UTF-8 編碼介紹(瞭解)
![在這裏插入圖片描述](http://static.javashuo.com/static/loading.gif)
4. 布爾類型 (boolean)
![在這裏插入圖片描述](http://static.javashuo.com/static/loading.gif)
boolean isPass = true;
if(isPass == true) {
System.out.println("考試經過,恭喜");
} else {
System.out.println("考試沒有經過,下次努力");
}
![在這裏插入圖片描述](http://static.javashuo.com/static/loading.gif)