Java基礎篇
java基礎
Java的特性優勢
- 簡單性
- 面向對象
- 可移植性
- 高性能
- 分散式
- 動態性 【反射機制】
- 多執行緒
- 安全性 【異常機制】
- 健壯性
java三大版本
- write once 、run anywhere
- javaSE:標準版 【桌面程式、控制台開發….】
- javaME:嵌入式開發【手機,小家電….】 用的人很少了
- javaEE:E企業級開發【web端,伺服器開發…..】
JDK JRE JVM
JVM —》 JRE —》 JVM
java程式運行機制
-
編譯型: 把程式全部翻譯成電腦可以執行的語言 compile 編譯器
-
解釋性: 邊執行邊解釋
-
程式運行機制:
-
源程式(.java)—》 java編譯器—》位元組碼(.class) —》
類裝載器—》位元組碼檢驗器—》解釋器—-》作業系統平台
-
標識符
- java標識符大小寫敏感;
- java標識符只能以字母 $ _ 開頭
數據類型
-
強類型語言
要求變數的使用要嚴格符合規定,所有變數都必須先定義後才能使用
-
弱類型語言
浮點數
float f = 0.1f;
double d = 1/10;
System.out.println(f==d);//fasle
float d1 = 23232323231f;
float d2 = d1+1;
System.out.println(d1==d2);//true
- float 有限 離散 舍入誤差 大約 接近但不等於
- double
- 最好完全避免使用浮點數進行比較
- BigDecimal 類 數學用具類 大數據類 【用這個】
字元
char c1 ='a';
char c2 = '中';
System.out.println(c1);// a
System.out.println((int)c1);// 97 強制類型轉換
//所有的字元本質還是數字
//編碼 Unicode 有表:(97 = a 65 =A) 2位元組 0 - 65526
// U0000 UFFFF
char c3 = '\u0061'
System.out.println(c3);// a
//轉義字元
// \t 製表符
// \n 換行符
System.out.println("Hello\tworld");
System.out.println("Hello\nworld");
//看一個e.g.
String sa = new String("hello world");
String sb = new String("hello world");
System.out.println(sa==sb);//false
String sc = "hello world";
String sd = "hello world";
System.out.println(sc==sd);//true
類型轉換
-
由於Java是強類型語言,所以要進行有些運算的時候,需要用到類型轉換
低 ———————到———————-高
byte , short , char —> int —->long —->float —->double
-
強制類型轉換 高 賦值 到 低 [會有記憶體溢出 和 精度 問題]
-
自定類型轉換 低 賦值 到 高
int i = 128;
byte b = (int) i;//記憶體溢出 -128
double d = i;
//操作比較大的輸的時候。注意溢出問題
int money = 10_0000_0000;//JDK7 新特性 數字之間可以用下劃線分割
int years = 20;
int total = money * years; // -147483648,計算的時候溢出了
long total2 = money * years;//默認是int 轉換之前還是int 就有問題了
long total3 = money * ((long)years);//先把一個數轉化為long √
變數
變數作用域
- 類變數 必須有static
- 實例變數
- 局部變數
常量
- 用final來定義 一般名大寫
運算符
- 算術運算符 : + – * / % ++ —
int e = 3;
int f = e++; //執行這行程式碼,先給f賦值,在自增
System.out.println(e);//4
int g = ++e;//執行這行程式碼,先自增,後給g賦值
int h = e++;//先賦值,在自增
System.out.println(e);//6
System.out.println(f);//3
System.out.println(g);//5
System.out.println(h);//5
// 字元串連接符 +
int a = 10;
int b = 20;
System.out.println(""+a+b);//1020
System.out.println(a+b+"");//30
- 賦值運算符 : =
- 關係運算符 : > < >= <= == != instanceof
- 邏輯運算符 : && || ! 與或非
[註:] &&: 有0 返回 || : 有 1 返回 ——— 短路機制
- 位運算符 : & | ^ ~ >> << >>>(了解)
System.out.println(2<<4);//<<左移乘2 >>右移除2
- 條件運算符 : ? :
- 擴展賦值運算符: += -= *= /=
JavaDos
/**
* @author shuang 作者
* @version 1.0 版本號
* @since 1.8 指明需要最早使用的JDK版本
* @param in 參數名
* @return 返回值情況
* @throws 異常拋出情況
*/
public String test(String in){
return in;
}
>javadoc -encoding utf-8 -charset utf-8 Doc.java
1、在cmd中運行上面命令會生成在線文檔 點擊index.html 可查看
2、也可以用IDEA生成javaDoc文檔
Java流程式控制制
Scanner對象
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("第一次輸入數據:");//hei baby
// nextLine輸出回車之前的數據 以回車分割
String str = scanner.nextLine();
System.out.println(str);//hei baby
System.out.println("第二次輸入數據:");//hello world
// next輸出空格之前的數據 以空格分割
str = scanner.next();
System.out.println(str);//hello
scanner.close();
}
示例:
public static void main(String[] args) {
//輸入多個數字 求總和 平均值 每輸入一個數字回車,通過非法數字來結束輸入輸出執行結果
Scanner scanner = new Scanner(System.in);
double sum = 0;
int m = 0;
System.out.println("請輸出數據:");
while (scanner.hasNextDouble()){
double x = scanner.nextDouble();
m++;
sum+=x;
System.out.println("輸入了第【"+m+"】個數據 當前總和為:"+sum);
}
System.out.println(m+"個數總和:"+sum);
System.out.println(m+"個數平均值:"+(sum/m));
scanner.close();
}
結果:
請輸出數據:
36
輸入了第【1】個數據 當前總和為:36.0
42
輸入了第【2】個數據 當前總和為:78.0
7.6
輸入了第【3】個數據 當前總和為:85.6
a
3個數總和:85.6
3個數平均值:28.53333333333333
順序結構
- def:語句與語句之間是按從上到下的順序進行的,它是由若干依次執行的處理步驟組成的,它是任何一個演算法都離不開的一種基本演算法結構。
選擇結構
- if單選擇結構
if(布爾表達式){
// 布爾表達式為true
}
- if雙選擇結構
if(布爾表達式){
// 布爾表達式為true
}else{
// 布爾表達式為false
}
- if多選擇結構
if(布爾表達式1){
// 布爾表達式1為true
}else if(布爾表達式2){
// 布爾表達式2為true
}else if(布爾表達式3){
// 布爾表達式3為true
}else if(布爾表達式4){
// 布爾表達式4為true
}else{
// 以上布爾表達式都不為true 執行程式碼
}
示例:
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("請輸入成績:");
int score = scanner.nextInt();
if (score == 100){
System.out.println("恭喜~~滿分~~");
}else if (score >= 90 && score < 100){
System.out.println("A級");
}else if (score >= 80 && score < 90){
System.out.println("B級");
}else if (score >= 70 && score < 80){
System.out.println("C級");
}else if (score >= 60 && score < 70){
System.out.println("D級");
}else if (score >= 0 && score < 60){
System.out.println("不及格");
}else {
System.out.println("成績不合法!!!");
}
scanner.close();
}
結果:
請輸入成績:
100
恭喜~~滿分~~
- switch多選擇結構
switch(expression){
case value:
//語句
break;//可選
case value:
//語句
break;//可選
case value:
//語句
break;//可選
default://可選
//語句
}
//註: case標籤 必須為 字元串常量或字面量
//了解一下反編譯 java ---- 位元組碼 ---- 反編譯(IDEA) --- java
反編譯(IDEA)示例
查看工程結構 projectStructure — project —project compiler output
可以找到.class文件 — 右鍵任意文件 — show in explorer(打開目錄) —
把.class文件拷貝到此目錄下 — IDEA把位元組碼反編譯為java
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//
package struct;
public class Demo03 {
public Demo03() {
}
public static void main(String[] args) {
char grade = 108;
switch(grade) {
case 65:
System.out.println("優秀");
break;
case 66:
System.out.println("良好");
break;
case 67:
System.out.println("及格");
break;
case 68:
System.out.println("很差");
break;
case 69:
System.out.println("掛科");
break;
default:
System.out.println("未知等級!");
}
}
}
循環結構
- while循環
while(布爾表達式){
//布爾表達式true
}
//註:大多數情況會讓循環停止下來,需要讓表達式失效方式來結束循環;
示例:
public static void main(String[] args) {
// 1-100 求和
int i = 0;
int sum = 0;
while (i<100){
i++;
sum = sum + i;
}
System.out.println(sum);//5050
}
- do while 循環
do whlile 和 while 相似,不同的是 do while 循環至少執行一次
do{
//程式碼語句
}while(布爾表達式)
IDEA快捷鍵
- ctrl + D 複製當前行到下一行