2020年java基礎知識課堂筆記

2020年java基礎知識課堂筆記

1.註釋、 標識符、關鍵字

註釋

註釋不會被執行,是寫給別人看的

  • 單行註釋 // 註釋語句
  • 多行註釋 /* 註釋語句*/
  • 文檔註釋
/*
 *@author Afan
 *@version 
 */

標識符

定義:類名、變量名以及方法名都被稱爲標識符

  • 所有的標識符都應該由字母(A-Z或者a-z),美元符($),下劃線(_)開始
  • 首字母之後可以是字母(A-Z或者a-z),美元符($),下劃線(_)或數字的任何字符組合
  • 不能使用關鍵字作爲變量名或方法名
  • 標識符是大小寫敏感
  • 合法標識符舉例:age、$salary、_value
  • 非法標識符舉例:123abc、-salary、#abc

關鍵字

abstract assert boolean break byte
case catch char class const
continue default do double else
enum extends final finially float
for goto if implements import
instanceof int interface long native
new package private protected public
return strictfp short static super
switch synchronized this throw throws
transient try void volatile while

2. 數據類型講解

java是強類型語言

強類型語言:要求變量的使用要嚴格規定,所有變量都必須先定義後才能使用

java的數據類型

  • 基本數據類型
    • 數值類型
      • 整數類型
        1. byte 佔1個字節
        2. short 佔2個字節
        3. int 佔4個字節
        4. long 佔8個字節
      • 浮點類型
        1. float 佔4個字節
        2. double 佔8個字節
      • 字符類型
        1. char 佔2個字節
    • boolean類型
      • true和false 佔1個字節

java8種基本數據類型

數據類型 字節 包裝類 默認值 最小值 最大值
byte 1 Byte 0 -128 127
short 2 Short 0 -32768 32768
int 4 Integer 0 -2^31 2^31-1
long 8 Long 0L -2^63 2^63-1
float 4 Float 0.0f 1.4***E***-45 3.4028235***E***38
double 8 Double 0.0 1.4***E***-324 1.7976931348623157***E***308
char 2 Character 0 65535
boolean 1 Boolean false false true

實例代碼

public class Test
{
    public static void main(String[] args) {
        //八大數據類型

        //整數
        int num1 = 10;//最常用
        byte num2 = 20;
        short num3 = 30;
        long num4 = 30L;//Long類型要在數字後面加個L

        //浮點數:小數
        float num5 = 50.1F;//float類型要在後面加個F
        double num6 = 3.141592653589793238462643;

        //字符
        char name1 = '凡';
        //字符串:String不是關鍵字,是類
        //String name2 = "阿凡不平凡";

        //布爾值:是非
        boolean flag = true;
        //boolean flag = false;
    }
}
  • 引用數據類型
    • 類型
    • 接口
    • 數組

3. java數據類型拓展

實例代碼

public class Test
{
    public static void main(String[] args) {
        //整數拓展:   進制      二進制0b       十進制     八進制0        十六進制0x

        int i = 10;
        int i2 = 010;  //八進制0   逢8進1
        int i3 = 0x10;  //十六進制0x   0~9 A~F 逢16進1

        System.out.println(i);  //10
        System.out.println(i2); //8
        System.out.println(i3); //16
        // -----------------------------------
        //浮點數拓展:浮點數是有限的,離散的,舍入誤差,大約,接近但不等於
        //float
        //double
        //結論:浮點數最好完全不要用於比較

        float f = 0.1f;  //0.1
        double d = 1.0/10;  //0.1
        System.out.println(f==d); //false

        float d1 = 22222222222222222222f;
        float d2 = d1 + 1;
        System.out.println(d1==d2); //true
        //------------------------------------
        //字符拓展: 所有字符本質還是數字
        //編碼 Unicode表:(97 = a 65 = A)    2字節     0~65536

        char c1 = 'a';
        char c2 = '凡';
        System.out.println(c1);      //a
        System.out.println((int)c1); //97
        System.out.println(c2);      //凡
        System.out.println((int)c2); //20961

        //U0000~UFFFF
        char c3 = '\u0061';
        System.out.println(c3); //a
        //轉義字符
        //  \t   製表符
        //  \n   換行
        //  ......
        System.out.println("Hello\nWorld");

        //----------------------------------
        //String創建對象
        String s1 = new String("hello world");
        String s2 = new String("hello world");
        System.out.println(s1==s2);   //false

        String s3 = "hello world";
        String s4 = "hello world";
        System.out.println(s3==s4);   //true

        //布爾值擴展:布爾值默認值是false
        boolean flag = true;
        if(flag == true){}
        if(flag){}     //兩者含義相同

    }
}

4. 類型轉換

  • 強制轉換 (數據類型)變量名 高容量–>低容量
    • 注意點
      1. 不能對布爾值轉換
      2. 不能把對象類型轉換爲不相干的類型
      3. 在把高容量轉化爲低容量時,強制轉換
      4. 轉換的時候可能存在內存溢出,或者精度問題

實例代碼

public class Test
{
    public static void main(String[] args) {
        
        //操作比較大的數的時候注意溢出問題
        //JDK新特性,數字之間可以用下劃線分割
        int money = 10_0000_0000;
        int years = 20;

        int total = money*years;
        long total2 = money*years;
        long total3 = money*((long)years);
        long total4 = ((long)money)*years;

        System.out.println(total);  //-1474836480 ,計算時溢出
        System.out.println(total2); //-1474836480 ,轉換之前已經出現問題
        System.out.println(total3); //20000000000 ,先把一個數強制類型轉換
        System.out.println(total4); //20000000000

    }
}
  • 自動轉換 低容量–>高容量

5. 變量、常量、作用域

變量

  • 變量:就是可以變化的量
  • java是一種強類型語言,每個變量都必須聲明其類型
  • java變量是程序中最基本的存儲單元,其要素包括變量名變量類型作用域

變量的命名規範

  • 所有變量、方法、類名:見名知意
  • 類成員變量:首字母小寫和駝峯原則:如monthSalary 除了第一個單詞首字母小寫,後面其他單詞首字母大寫
  • 局部變量:首字母小寫和駝峯原則
  • 常量:大寫字母和下劃線:MAX_VALUE
  • 類名:首字母大寫和駝峯原則:GoodMan
  • 方法名:首字母小寫和駝峯原則:runRun()

注意事項

  • 每個變量都有類型,類型可以是基本類型,也可以是引用類型
  • 變量名必須是合法的標識符
  • 變量聲明是一條完整的語句,因此每一個聲明都必須以分號結束

聲明變量: 數據類型 變量名 = 值;

常量

  • 常量:初始化(initialize)後不能在改變值!不會變動的值

  • 所謂常量可以理解爲一種特殊的變量,它的值被設定後,在程序運行過程中不允許被改變

    final 常量名 = 值;

    final double PI = 3.14;

  • 常量名一般使用大寫字符

public class Test{
    //修飾符,不存在先後順序
    static final double PI = 3.14;
    
    public static void main(String[] args) {
        System.out.println(PI); //3.14
    }
}    

變量作用域

  • 類變量:有static關鍵字

  • 實例變量:從屬於對象,如果不自行初始化,則是這個類型的默認值

    布爾值:默認值是false

    除了基本類型,其餘的默認值都是null

  • 局部變量:必須聲明和初始化值

public class Variable
{   
    static int allClicks = 0;   //類變量
    String str = "hello world"; //實例變量
    
    public void method(){
        int i = 0; //局部變量
    }
}

實例代碼

public class Test{
    //類變量 static
    static double salary = 2500; 

    //屬性:變量
    //實例變量:除了8種基本類型,其餘類型默認值爲null
    String name; 
    int age;

    //main方法
    public static void main(String[] args) {
        //局部變量:必須聲明和初始化值
        int i = 10;
        System.out.println(i); //10

        //變量類型 變量名字 = new 變量類型();
        Test test = new Test();
        System.out.println(test.age);  //0
        System.out.println(test.name); //null

        System.out.println(salary); //2500.0
    }
}

6. 基本運算符

在這裏插入圖片描述

二元運算符實例代碼

public class Test{
    public static void main(String[] args) {
        // 二元運算符
        //ctr+D: 複製當前行到下一行
        int  a = 10;
        int  b = 20;
        int  c = 6;

        System.out.println(a+b);  //30
        System.out.println(a-b);  //-10
        System.out.println(a*b);  //200
        //System.out.println(a/b);  //0   除法運算特殊,與數學不同,需要強制轉換
        System.out.println(a/(double)b);  //0.5
        System.out.println(a%b) //4 取餘 模運算
    }
}
public class Test{
    public static void main(String[] args) {
        long a = 123123123123123L;
        int b = 123;
        short c = 10;
        byte d = 8;
        /*注意:
          1. long參與運算,運算結果是long類型
          2. 比int類型容量低的基本類型參與運算,運算結果是int類型
          */

        System.out.println(a+b+c+d);  //123123123123264  Long類型
        System.out.println(b+c+d);  //141  int類型
        System.out.println(c+d);  //18  int類型
        
    }
}

關係運算符實例代碼

public class Test{
    public static void main(String[] args){
        //關係運算符返回的結果:正確  錯誤  布爾值
        int a =10;
        int b =20;

        System.out.println(a>b);  //false
        System.out.println(a<b);  //true
        System.out.println(a==b);  //false
        System.out.println(a!=b);  //true
        
        Test test = new Test();
        System.out.println(test instanceof Test); //true 
        
        /*instanceof 用來測試一個對象是否爲一個類的實例
         *boolean result = obj instanceof class
         *注意點:
         * 1. obj必須爲引用類型,不能爲基本類型
         * 2. obj可以爲null
         * 3. obj可以爲class類的直接或間接子類
         * 4. obj可以爲class接口的實現類
         */
    }
}

一元運算符

public class Test{
    public static void main(String[] args){
        //++ -- 自加,自減  一元運算符
        int a = 3;

        int b = a++; //執行完這行代碼後,先給b賦值,a再自增1
        System.out.println(a); //4

        int c = ++a;   //執行完這行代碼後,a自增1,再賦值給b

        System.out.println(a);  //5
        System.out.println(b);  //3
        System.out.println(c);  //5
        //很多數學運算,會使用一些工具類來操作
        double pow =Math.pow(2,3);
        System.out.println(pow); //8.0
    }
}

邏輯運算符

public class Test{
    public static void main(String[] args){
       // 與(and)    或(or)     非(!)
        boolean a = true;
        boolean b = false;

        System.out.println(a&&b);    //false
        System.out.println(a||b);    //true
        System.out.println(!(a&&b)); //true

        //短路運算
        int c = 5;
        boolean d = (c < 4)&&(c++ < 4);
        System.out.println(d);  //false
        System.out.println(c);  //5
    }
}

位運算

  • 左移:<< 相當於*2
  • 右移:>> 相當於 /2
public class Test{
    public static void main(String[] args){
        /*
         位運算是二進制運算
         A = 0011 1100
         B = 0000 1101
         -------------------
         A&B = 0000 1100
         A|B = 0011 1101
         A^B = 0011 0001
         ~B = 1111 0010   (瞭解!!)

         <<   *2
         >>   /2    效率極高!!!
         */
        System.out.println(2<<3);  //16
        System.out.println(8>>2);  //2
    }
} 

三元運算符

public class Test{
    public static void main(String[] args){
        int a = 10;
        int b = 20;

        a+=b; //a=a+b;
        a-=b; //a=a-b;

        //字符串連接符  + ,String
        System.out.println(a+b);  //30
        System.out.println(""+a+b); //1020
        System.out.println(a+b+""); //30

        //三元運算符  x ? y : z
        //如果 x == true,則結果爲y,否則結果爲z

        int score = 80;
        String type = score < 60 ? "不及格":"及格";
        System.out.println(type);  //及格
    }
}

7. 包機制

  • 爲了更好地組織類,java提供了包機制,用於區別類名的命名空間
  • 包語句的語法格式

package 包名[.包名2[.包名3]];

  • 一般利用公司域名倒置作爲包名

eg: com.fan.Xxx

  • 在java中導入某一個包的成員

import 包名.類名;

8. javaDoc生成文檔

  • javadoc命令用來生成自己的API文檔的

  • 參數信息

    • @author 作者名
    • @version 版本號
    • @since 指明需要最早使用的jdk版本
    • @param 參數名
    • @return 返回值情況
    • @throws 異常拋出情況
  • 使用方法

    • cmd命令行:javadoc 參數(-encoding UTF-8 -charset UTF-8) java文件
    • IDEA生成API文檔:打開idea,選擇項目。點擊工具欄 Tools->Generate JavaDOC

9. 後言

  • 本網課筆記知識來源於 B站 狂神說java
  • 該網課乾貨滿滿,老師講課清晰,重點是免費,值得推薦 !!!
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章