Java入門知識點 Java入門知識點

Java入門知識點

 

Java源代碼的流程html

  Java程序由.java文件生成,經過JVM進行編譯獲得字節文件.classjava

class HelloWorld {
    public static void main(String[] args){
        System.out.println("Hello World!");
    }
}

關於class有以下幾點規則:數組

  1. 文件的名字必須和class的名字一致(public級別的class名字)。
  2. 文件必須只包含一個public訪問基本的class(能夠包含多個非public級別的class)。

命名規則:安全

  1. 包(其實就是文件夾,用於解決相同類名問題):包名要求所有小寫,通常是公司的域名倒着寫 com.heima.包的做用
  2. 類或者接口:若是是一個單詞,要求首字母大寫,若是是多個單詞要求每一個單詞首字母大寫(駝峯命名)
  3. 方法和變量: 若是是一個單詞,每一個字母都小寫,若是是多個單詞,從第二個單詞開始首字母大寫
  4. 常量: 若是是一個單詞,全部字母大寫,若是是多個單詞也是全部字母大寫,可是用_分開 MAX MAX_VALUE

Java語言優點:app

  徹底面向對象,安全可靠,跨平臺性ide

(補充)有關於dos命令行方式:函數

  dir  md  rd  cd  cd..  cd/  del  exitpost

標識符this

  由英文字母,數字,_ ,$組成,其中數字不能開頭url

數據類型(11)

 8種原子類型(基本數據類型)

  1. 整數類型:byte(1)、short(2)、int(4)和long(8)。
  2. 小數類型:float(4)和double(8)。
  3. 字符類型:char(2)。
  4. 布爾類型:bool。

 3種引用數據類型

  interface、class和array

 小數類型的常量默認是double類型,聲明float類型的常量須要使用F做爲後綴。

運算符

  1. 算術運算符:+、-、*、/ 和 %,兩個整數相除,結果仍是整數。
  2. 賦值運算符:=、+=、-=、*=、/=、%=、&=、|=、~=、^=、<<=、>>= 、 >>>=、++ 和 --。
  3. 比較運算符:==、!=、<、<=、> 和 >=。
  4. 邏輯運算符:&&、|| 和 !。
  5. 位運算符:&、|、~、^、<<、>> 和 >>>。

注意:

  1. 字符串數據和任何數據使用+都是相鏈接,最終都會變成字符串。//System.out.println(「5+5=」+5+5);//5+5=55
  2. 出現負數取餘隻看左邊。//System.out.println(-1%5);
  3. s+=5;//只一次賦值運算,進行自動轉換
  4. &和&&的區別 [&:不管左邊是true是false,右邊都運算 &&:當左邊爲false時,右邊不運算。同理,|和||的區別一致]

鍵盤錄入

複製代碼
import java.util.Scanner;
class Demo_Scanner {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("請輸入一個整數:");
        int x=sc.nextInt();
        System.out.println(x);
    }
}
複製代碼

控制結構

  1. 條件:if-else if-else、switch-case-default和三元運算符(?:)。
  2. 循環:while、do-while、for和foreach。
  3. Labeled block。

注意:

  1. if else結構簡寫格式:變量=(條件表達式)?表達式1:表達式2;
  2. switch語句中被選擇類型的值只有四種:byte short int char;
  3. switch語句裏case和default定義位置隨機,執行順序從case開始。
  4. 語句嵌套形式:循環嵌套(大圈套小圈)

  //尖朝上,可改變條件,讓條件隨着外循環變化
  //尖朝下,可初始化值,讓初始化隨着外循環變化

  5. break與continue的區別

  break:應用範圍:選擇結構和循環體 跳出所在當前循環
  continue:只能用於循環結構 結束本次循環,繼續下一次循環

複製代碼
public class Program {

    public static void main(String[] args) {
        task: {
            int age = 25;

            System.out.println("start");

            if (age < 30) {
                break task;
            }

            System.out.println("end");
        }
    }
}
複製代碼

字符串

  String是擁有「值語義」的引用類型,字符串常量實現了「享元模式」,equals會按照內容進行比較,==按照地址比較。

複製代碼
public class Program {

    public static void main(String[] args) {
        String x = "小咕嚕";
        String y = new String("小咕嚕");
        
        System.out.println(x.equals(y)); // true
        System.out.println(x == y); // false
    }

}
複製代碼

 爲了高效的修改字符串Java引入了StringBuffer

複製代碼
{
            StringBuffer sb = 
                    new StringBuffer()
                    .append("小")
                    .append("咕")
                    .append("嚕");
            
            System.out.println(sb.toString());
        }
複製代碼

數組

 聲明語法

  DataType[] name 或 DataType name[]。

複製代碼
public class Program {

    public static void main(String[] args) {
        {
            String[] strs = { "小", "咕", "嚕" };

            for (String item : strs) {
                System.out.print(item);
            }
        }
    }

}
複製代碼

 多維數組

  只有不等長多維數組DataType[][],沒有DataType[xxx, xxx]。

方法

  Java中全部的賦值和方法調用都是「按值「處理的,引用類型的值是對象的地址,原始類型的值是其自身。

  Java支持變長方法參數。

複製代碼
public class Program {

    public static void main(String[] args) {
        print("小咕嚕", "大咕嚕");
        print(new String[] { "小咕嚕", "大咕嚕" });
    }

    private static void print(String... args) {
        for (String item : args) {
            System.out.println(item);
        }
    }
}
複製代碼

 ① 變量在內存中的存儲方式:  

  1. 成員變量做用於整個類中,存儲於堆內存中,由於對象的存在,纔在內存中存在,必有初始化值,可參與運算
  2. 局部變量做用於函數中或語句中,存在於棧內存中

  this的應用:

  1. 當定義類中功能時,該函數內部要調用該函數的對象 但凡本類功能內部使用了本類對象,都用this表示。
  2. 用於區分局部變量和成員變量重名的狀況表明本類的對象,表明它所在函數所屬對象的引用。
  3. 只能定義在構造函數的第一行。

 ③ 構造代碼塊: 給全部對象進行統一初始化,優先於構造函數執行

注意:(內存機構:分區)
  1.棧內存:用於存儲局部變量,當數據使用完,所佔空間會自動釋放
  2.堆內存:①.數組和對象,經過New創建的實例都存放在堆內存中
       ②.每個實體都有內存地址值
       ③.實體中的變量都有默認初始化值
       ④.實體再也不被使用,會在不肯定的時間內被垃圾回收器回收
  3.方法區,本地方法區,寄存器

複製代碼
public class Program {

    public static void main(String[] args) {
        Point point = new Point(100);

        System.out.print(point);
    }
}

class Point {
    private int x = 0;
    private int y = 0;

    public Point(int x, int y) {
        this.x = x;
        this.y = y;
    }

    public Point(int x) {
        this(x, x);
    }

    public String toString() {
        return "(x:" + this.x + ",y:" + this.y + ")";
    }
}
複製代碼

靜態成員

  Java中相似靜態構造方法的結構,稱之爲:靜態初始化代碼塊,與之對應的是實例初始化代碼塊,見下例:

複製代碼
public class Program {

    public static void main(String[] args) {
        System.out.println(Point.getValue());
        System.out.println(new Point());
    }
}

class Point {
    private static int value = 0;

    public static int getValue() {
        return value;
    }

    static {
        value++;
    }

    static {
        value++;
    }

    private int x = 0;
    private int y = 0;

    {
        this.x = 10;
    }

    {
        this.y = 10;
    }

    public String toString() {
        return "(x:" + this.x + ",y:" + this.y + ")";
    }
}
複製代碼

注意:

  1. 靜態方法只能訪問靜態成員 非靜態方法既能夠靜態也能夠訪問非靜態
  2. 靜態方法中不能夠定義this,super等關鍵字 由於靜態優先於對象存在
  3. 主函數是靜態的
  4. 類變量隨着類的加載而存在於方法區中 實例變量隨着對象的創建而存在於堆內存中

繼承

  繼承使用 extends,抽象類和抽象方法使用abstract聲明,向下轉型使用 (ChildType)instance,判斷是不是某個類型使用 instanceof,見下例:

複製代碼
public class Program {

    public static void main(String[] args) {
        printAnimal(new Animal());
        printAnimal(new Dog());
    }

    private static void printAnimal(Animal animal) {
        if(animal instanceof Dog){
            System.out.println("I am a " + (Dog) animal);
        }
        else
        {
            System.out.println("I am an " + animal);
        }
    }
}

class Animal {
    public String toString() {
        return "Animal";
    }
}

class Dog extends Animal {
    public String toString() {
        return "Dog";
    }
}
複製代碼

重寫

 Java中的重寫規則比較靈活,具體以下:

  1. 除了 private 修飾以外的全部實例方法均可以重寫,不須要顯式的聲明。
  2. 重寫的方法爲了顯式的表達重寫這一律念,使用 @Override進行註解。
  3. 重寫的方法能夠修改訪問修飾符和返回類型,只要和父類的方法兼容(訪問級別更高,返回類型更具體)。
  4. 可使用final將某個方法標記爲不可重寫。
  5. 在構造方法中使用 super(xxx, xxx)調用父類構造方法,在常規實例方法中使用 super.method(xxx, xxx)調用父類方法。
  6. Java不支持覆蓋(new)。
複製代碼
public class Program {

    public static void main(String[] args) {
        Animal animal = new Animal();
        Animal dog = new Dog();

        animal.say();
        dog.say();

        animal.eat(animal);
        dog.eat(dog);
        
        System.out.println(animal.info());
        System.out.println(dog.info());
    }
}

class Animal {
    private String name = "Animal";

    protected void say() {
        System.out.println("Animal" + " " + this.name);
    }

    public void eat(Animal food) {
        System.out.println("Animal eat " + food);
    }

    public Object info() {
        return "Animal";
    }
    
    @Override
    public String toString() {
        return "Animal";
    }
}

class Dog extends Animal {
    private String name = "Dog";

    @Override
    public final void say() {
        System.out.println("Dog" + " " + this.name);
    }

    @Override
    public final void eat(Animal food) {
        super.eat(food);
        
        System.out.println("Dog eated");
    }

    @Override
    public final String info() {
        return "Dog";
    }

    @Override
    public final String toString() {
        return "Dog";
    }
}
複製代碼

Java支持三種導入語法:

  1. 導入類型:import xxx.xxx.xxxClass。
  2. 導入包:import xxx.xxx.xxx.*。
  3. 導入靜態成員:import static xxx.xxx.*。
複製代碼
import static util.Helper.*;

public class Program {

    public static void main(String[] args) {
        puts("小咕嚕");
    }
}
複製代碼

訪問級別

  Java支持四種訪問級別:public、private、protected 和 default(默認),類型和接口只能使用public 和 default,成員和嵌套類型可使用全部,下面簡單的解釋一下 protected 和 default。

  • protected 修飾過的成員只能被本身、子類和同一個包裏的(不包括子包)其餘類型訪問。
  • default 修改過的類型或成員只能被本身和同一個包裏的(不包括子包)其餘類型訪問。

嵌套類

Java支持以下幾種嵌套類:

  1. nested class,定義在類型內部的類型。
    1. static nested class,使用 static 聲明的 nested class,static nested class 能夠訪問全部外部類的靜態成員。
    2. inner class,沒有使用 static 聲明的 nested class,inner class 能夠訪問全部外部類的實例成員,inner class 不能定義靜態成員。
複製代碼
public class Program {

    /**
     * @param args
     */
    public static void main(String[] args) {
        OuterClass outer = new OuterClass();
        OuterClass.InnerClass inner = outer.new InnerClass();
        OuterClass.InnerClass.InnerInnerClass innerInner = inner.new InnerInnerClass();
        outer.show();
        inner.show();
        innerInner.show();
        
        OuterClass.StaticNestedClass staticNested=new OuterClass.StaticNestedClass();
        OuterClass.StaticNestedClass.StaticNestedNestedClass staticNestedNested=new OuterClass.StaticNestedClass.StaticNestedNestedClass();
        
        staticNested.show();
        staticNestedNested.show();
    }
}

class OuterClass {
    int x = 1;
    static int i = 1;

    void show() {
        System.out.println(x);
        System.out.println(i);
    }

    class InnerClass {
        int y = 2;

        void show() {
            System.out.println(x);
            System.out.println(y);
        }

        class InnerInnerClass {
            int z = 3;

            void show() {
                System.out.println(OuterClass.this.x);
                System.out.println(y);
                System.out.println(z);
            }
        }
    }

    static class StaticNestedClass {
        static int j = 2;

        void show() {
            System.out.println(i);
            System.out.println(j);
        }

        static class StaticNestedNestedClass {
            static int k = 3;

            void show() {
                System.out.println(i);
                System.out.println(j);
                System.out.println(k);
            }
        }
    }
}
複製代碼

特殊的inner class:local class

複製代碼
public class LocalClassExample {

    static String staticValue = "static value";
    String instanceValue = "instance value";

    public void test() {

        final String finalLocalValue = "final local value";

        class LocalClass {
            void test() {
                System.out.println(staticValue);
                System.out.println(instanceValue);
                System.out.println(finalLocalValue);
            }
        }

        LocalClass local = new LocalClass();
        local.test();
    }
}
複製代碼

除了inner class的規則以外,local class能夠訪問局部final變量,在Java8中有更多的改進。

注意:

1.被final修飾的類不能夠被繼承,爲了不被繼承,被子類複寫功能

2.被final修飾的方法不能夠被複寫

3.被final修飾的變量是一個常量,只能賦值一次i,既能夠修飾成員變量,也能夠修飾局部變量 做宏定義使用

4.內部類定義在類中的局部位置上時,只能訪問該局部被final修飾的局部變量

特殊的local class:anonymous class

複製代碼
public class Program {

    /**
     * @param args
     */
    public static void main(String[] args) {
        execute(new Action() {
            @Override
            public void execute() {
                System.out.println("執行業務邏輯");
            }
        });
    }

    static void execute(Action action) {
        System.out.println("事物開始");
        action.execute();
        System.out.println("事物結束");
    }
}

interface Action {
    void execute();
}
複製代碼

常量

複製代碼
public final class Program {
    static final String STATIC_CONSTANTS = "STATIC_CONSTANTS";
    final String INSTANCE_CONSTANTS = "INSTANCE_CONSTANTS";

    public static void main(String[] args) {
        final String LOCAL_CONSTANTS = "LOCAL_CONSTANTS";

        System.out.println(STATIC_CONSTANTS);
        System.out.println(new Program().INSTANCE_CONSTANTS);
        System.out.println(LOCAL_CONSTANTS);
        new Program().test("PARAMETER_CONSTANTS");
    }

    public final void test(final String msg) {
        System.out.println(msg);
    }
}
複製代碼

有一點須要注意的是:只有一種狀況Java的常量是編譯時常量(編譯器會幫你替換),其它狀況都是運行時常量,這種狀況是:靜態類型常量且常量的值能夠編譯時肯定。

接口

Java的接口能夠包含方法簽名、常量和嵌套類,見下例:

複製代碼
public final class Program {
    public static void main(String[] args) {
        Playable.EMPTY.play();

        new Dog().play();
    }
}

interface Playable {
    Playable EMPTY = new EmptyPlayable();

    void play();

    class EmptyPlayable implements Playable {

        @Override
        public void play() {
            System.out.println("無所事事");
        }

    }
}

class Dog implements Playable {

    @Override
    public void play() {
        System.out.println("啃骨頭");
    }

}
複製代碼

異常

Java中的異常分爲checked和unchecked,checked異常必須聲明在方法中或被捕獲,這點我以爲比較好,一定:異常也是API的一部分,見下例:

複製代碼
public final class Program {
    public static void main(String[] args) {
        try {
            test();
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

    public static void test() throws Exception {
        throw new Exception("I am wrong!");
    }
}
複製代碼

全部繼承Exception的異常(除了RuntimeException和它的後代以外)都是checked異常。

 注意:

  throws:使用在函數上,後面跟的是異常類,能夠跟多個,用逗號隔開  throw:使用在函數內,跟的是異常對象

相關文章
相關標籤/搜索