Java基礎(chǔ)知識點整理(二)

數(shù)組求和

案例代碼

package day04;

public class ArrayTest {
    
    /**
     * 對整數(shù)數(shù)組進(jìn)行求和
     * @param x 表示整數(shù)數(shù)組
     * @return
     */
    static int sum(int[] x) {
        int z=0;
        for(int e:x) {
            z+=e;
        }
        return z;
    }

    public static void main(String[] args) {
        //System.out.println(args);
        int[] arr = {1,2,3};
        //System.out.println(arr);
        System.out.println(sum(arr));
    }

}

分析
調(diào)用sum函數(shù)的時候,發(fā)生參數(shù)的傳遞,數(shù)列(也稱數(shù)組對象)在堆里面的內(nèi)存地址0x6d06d69c傳遞給了x, 這樣的話,x也指向了該數(shù)列,變量名x可以理解為arr數(shù)組的別名。

image.png

常量

常量是特殊的變量,常量里面保存的內(nèi)容不能被改變。

package day03;

public class FinalTest {
    static final byte HOURS_OF_DAY=24;//1天有24個小時
    static final short MONTHS_OF_YEAR=12;//1年有12個月
    static final int PLAYERS_OF_FOOTBALLTEAM=11;//1支足球隊上場比賽的人數(shù)
    static final long SPEED_OF_LIGHT=300000;//光速30w公里每秒
    static final boolean TAIWAN_BELONG_TO_CHINA=true;//臺灣屬于中國
    static final char FLAG_OF_RMB='¥';//人民幣符號
    static final double TAX_THRESHHOLD=5000.0;//個稅起征點
    
    public static void main(String[] args) {
        System.out.println("光速為每秒鐘:"+SPEED_OF_LIGHT/10000+"萬公里");
    }
}

new關(guān)鍵字

new是新建的意思,new操作會在堆里面開辟空間。

案例代碼

package day06;

class StudentTest {

    public static void main(String[] args) {
//      int x = 1;
        Student[] students = new Student[30];//開辟30個Student類型的空間
        students[0] = new Student();//實例化
        students[0].sno = 1001;
        students[0].sname = "張三";
        students[0].isMale = true;
        students[1] = new Student();
        students[1].sno = 1002;
        students[1].sname = "李四";
        students[1].isMale = true;      
        System.out.println(students[0]);
        System.out.println(students[1]);
        System.out.println(students[2]);//null
        System.out.println(students);
//      Student scx = new Student();//scx表示一個學(xué)生對象
//      System.out.println(scx);
//      System.out.println(scx.sno+","+scx.sname+","+scx.isMale);
//      scx.sno = 1001;
//      scx.sname = "宋超鑫";
//      scx.isMale = true;
//      System.out.println(scx.sno+","+scx.sname+","+scx.isMale);       
//      System.out.println(scx);
    }
}

image.png

this關(guān)鍵字

this變量引用當(dāng)前對象,比如:this.age就表示當(dāng)前對象的年齡
1)this可以調(diào)用屬性(成員變量)
2)this可以調(diào)用功能(成員方法)

籃球運動員類Player

package day07;

public class Player {
    private String name;
    private int age;//成員變量
    private boolean isMale;//成員變量
    private String position;
    private int score;
    private int no=10;          //球衣號碼,默認(rèn)10
    private int rate;           //1~100之間
    
    
    /**
     * 
     * @param name  接收姓名
     * @param age       接收年齡
     * @param isMale    接收性別
     * @param position  接收場上位置
     * @param score 接收場上得分
     * @param no        接收球衣號碼
     * @param rate      接收命中率
     */
    public Player(String name, int age, boolean isMale, String position, int score, int no, int rate) {
        super();
        this.name = name;
        this.age = age;
        this.isMale = isMale;
        this.position = position;
        this.score = score;
        this.no = no;
        this.rate = rate;
    }
    

    public Player() {
        super();
    }


    /**
     * 獲取年齡的接口
     * @return
     */
    public int getAge() {
        return age;
    }

    /**
     * 修改年齡的接口
     * @param age   接收輸入的年齡
     */
    public void setAge(int age) {
        if(age>=0) {
            this.age = age;
        }
    }

    /**
     * 獲取命中率
     * @return
     */
    public int getRate() {
        return rate;
    }

    /**
     * 修改命中率
     * @param rate
     */
    public void setRate(int rate) {
        if(rate>=0&&rate<=100) {
            this.rate = rate;
        }   
    }

    @Override
    public String toString() {
        return "Player [name=" + name + ", age=" + age + ", position=" + position + "]";
    }
    
    /**
     * 投籃的方法
     * @return      true表示投進(jìn),false表示沒投進(jìn)
     */
    public boolean shoot() {
        int x = (int)(1+Math.random()*100);//x表示一個1~100之間的隨機整數(shù)
        //假設(shè)rate是90,那么x<=90的概率是rate%
        if(x<=rate) {//如果x小于等于rate,則投籃投進(jìn)(得2分)
            score+=2;
            return true;
        }
        return false;
    }
    
}

測試類

package day07;

public class PlayerTest2 {

    public static void main(String[] args) {
        Player zs = new Player();
        Player ls = new Player();
        System.out.println(zs);
        System.out.println(ls);
        zs.setAge(23);//修改的是張三的年齡
        //ls.setAge(27);//修改的是李四的年齡
        System.out.println(zs.getAge());//查詢
    }

}

image.png

static關(guān)鍵字

1)靜態(tài)成員變量位于方法區(qū),對應(yīng)的空間個數(shù)只有1個
2)非靜態(tài)成員變量位于堆內(nèi)存,對應(yīng)的空間個數(shù)取決于有多少個該類型的對象
3)靜態(tài)的成員方法里面不能直接調(diào)用非靜態(tài)的成員變量
4)靜態(tài)的成員方法里面不能直接調(diào)用非靜態(tài)的成員方法
5)靜態(tài)的成員方法里面可以通過對象名調(diào)用非靜態(tài)的方法,比如:

    public static void main(String[] args){
        Player linshuhao = new Player();
        linshuhao.shoot();//投籃
    }

6)靜態(tài)的成員方法里面不能直接調(diào)用非靜態(tài)的成員
7)非靜態(tài)的成員方法里面可以直接調(diào)用靜態(tài)的成員變量
8)非靜態(tài)的成員方法里面可以直接調(diào)用靜態(tài)的成員方法
9)靜態(tài)成員可以通過類名調(diào)用,也可以通過對象名調(diào)用。比如:把成員方法設(shè)置成靜態(tài)的,我們就可以通過類名去調(diào)用,否則的話,需要先實例化,然后通過對象名去調(diào)用
10)靜態(tài)成員方法里面不能使用this變量
11)靜態(tài)代碼塊的語法格式(了解)

static{//JVM在加載類的時候,會執(zhí)行該類里面的靜態(tài)代碼塊,并且該靜態(tài)代碼塊只會被執(zhí)行一次
    ...
}

學(xué)生類

package day06;

/**
 * 學(xué)生類
 * @author yangzc
 *
 */
public class Student {
    public int sno;//學(xué)號
    public String sname;//姓名
    public boolean isMale;//性別
    public int age;//年齡
    public static int count=0;//學(xué)生的數(shù)量
    
    public Student() {
        count++;
    }


    /**
     * 自我介紹的方法
     */
    public void info() {
        System.out.println("大家好!我叫"+sname+",今年"+age+"歲");
        //System.out.println(count);
    }
}

測試類

package day07;

import day06.Student;

public class StudentTest4 {

    public static void main(String[] args) {
        //Student.count=30;
        Student zs = new Student();
        zs.sname="張三";
        zs.sno=1001;
        Student ls = new Student();
        ls.sname="李四";
        ls.sno=1002;
        System.out.println(zs);
        System.out.println(ls);
        //System.out.println(zs.sname);//
        //System.out.println(zs.sno);//1001
        //System.out.println(ls.sname);//
        System.out.println(Student.count);
    }

}

image.png

多態(tài)的特點

1)父類的變量可以接收子類對象,比如:

public double pay(Emp obj) {//父類變量obj可以接收子類對象
    //obj表面上是員工
    if(obj instanceof PM) {//如果obj實際上是項目經(jīng)理的話
        //向下造型(把Emp類型強轉(zhuǎn)成PM類型)
        PM manager = (PM)obj;
        return obj.getSalary()+manager.getBonus();
    }
    return obj.getSalary();
}

2)通過一個指向子類對象的父類引用調(diào)用父類的方法,并且子類里面對該方法進(jìn)行了重寫,那么,執(zhí)行的時候?qū)嶋H調(diào)用的是子類的方法,比如:

Emp e = new SE();//程序員的工資默認(rèn)是5000.0
e.setSalary(-1000.0);//實際調(diào)用的是子類的setSalary接口
System.out.println(e.getSalary());//實際調(diào)用的是父類的getSalary接口

說明:
使用多態(tài)的話,代碼維護(hù)起來比較方便

向上造型(子類可以自動轉(zhuǎn)換成父類)

Cat類型可以自動轉(zhuǎn)成Animal類型

//父類變量可以接收子類對象
//Cat是Animal的子類,可以自動轉(zhuǎn)成Animal
Animal obj = new Cat();//貓是動物,正確
//子類變量不能接收父類對象
Cat cat = new Animal();//動物是貓,錯誤

向下造型(通過強制轉(zhuǎn)換可以將父類轉(zhuǎn)換成子類)

把Animal類型強轉(zhuǎn)成Cat類型

Animal obj = new Cat();
if(obj instanceof Dog){//如果animal所指向的對象是狗的話
    //把Animal(父類)強制轉(zhuǎn)換成Dog(子類)
    Dog wangcai = (Dog)obj;
}
if(obj instanceof Cat){//如果animal所指向的對象是貓的話
    //把Animal(父類)強制轉(zhuǎn)換成Cat(子類)
    Cat hellokitty = (Cat)obj;
}

Cat類和Dog類之間沒有繼承關(guān)系,把Cat類強轉(zhuǎn)成Dog類會發(fā)生編譯錯誤

Cat cat = new Cat();
Dog dog = (Dog)cat;//編譯出錯(不能把Cat強轉(zhuǎn)成Dog)

把Animal類強轉(zhuǎn)成Dog類會發(fā)生運行異常,因為obj實際上是貓

Animal obj = new Cat();
Dog dog = (Dog)obj;//運行出錯

字符串比較

//判斷兩個字符串對象的內(nèi)容是否相等
System.out.println(str.equals(str1));
//比較的是對象的內(nèi)存地址
System.out.println(對象名==對象名);

案例代碼

public static void main(String[] args) {
    String str = "123";
    String str1 = new String("123");
    String str2 = "123";
    System.out.println(str == str1);
    System.out.println(str == str2);
}
image.png

字符串替換

public String replace(char oldChar,char newChar){
  ...
}

參數(shù):
oldChar - 原字符。
newChar - 新字符。
返回:
一個從此字符串派生的字符串,它將此字符串中的所有 oldChar 替代為 newChar。
說明:
返回一個新的字符串,它是通過用 newChar 替換此字符串中出現(xiàn)的所有 oldChar 得到的。
如果 oldChar 在此 String 對象表示的字符序列中沒有出現(xiàn),則返回對此 String 對象的引用。否則,創(chuàng)建一個新的 String 對象,它所表示的字符序列除了所有的 oldChar 都被替換為 newChar 之外,與此 String 對象表示的字符序列相同。

案例代碼

public static void main(String[] args) {
    System.out.println("String".replace('g', 'G') == "String".replace('g','G'));
    System.out.println("String".replace('t', 't') == "String".replace('t','t'));
}
image.png

UTF-8編碼和Unicode編碼

漢字字符集查詢:
https://www.qqxiuzi.cn/bianma/zifuji.php

中文字符 UTF-8編碼(16進(jìn)制) Unicode編碼(16進(jìn)制) GBK編碼(16進(jìn)制)
E5B08F 5C0F D0A1
E7B1B3 7C73 C3D7
package examples;

import java.util.ArrayList;
import java.util.List;

public class UTF8Test {

    public static void main(String[] args) throws Exception {
        String str = "小米";
        byte[] arr = str.getBytes("utf-8");
        char[] arr2 = str.toCharArray();
        byte[] arr3 = str.getBytes("GBK");
        List<String> list = new ArrayList<String>();
        List<String> list2 = new ArrayList<String>();
        List<String> list3 = new ArrayList<String>();

        for(byte e:arr) {
            list.add(String.format("%x", e));
        }
        //輸出中文字符串的UTF-8編碼
        System.out.println(list);//[e5, b0, 8f, e7, b1, b3]
        
        for(char e:arr2) {
            list2.add(Integer.toHexString(e));
        }
        //輸出中文字符串的Unicode編碼
        System.out.println(list2);//[5c0f, 7c73]
        
        for(byte e:arr3) {
            list3.add(String.format("%x", e));
        }
        //輸出中文字符串的GBK編碼
        System.out.println(list3);//[d0, a1, c3, d7]
    }

}


參考資料

[01] 運算符的結(jié)合性
https://www.cnblogs.com/softwaretesting/archive/2011/08/16/2139068.html
[02] Java運算符優(yōu)先級
https://www.cnblogs.com/zjfjava/p/5996666.html
[03] 在JAVA命令行中輸入?yún)?shù),星號是如何處理的?
https://zhidao.baidu.com/question/56783574.html
[04] JVM內(nèi)存初學(xué) 堆、棧、方法區(qū)
https://www.cnblogs.com/dingyingsi/p/3760730.html
[05] Java中的堆內(nèi)存、棧內(nèi)存和方法區(qū)總結(jié)
https://orochimaru-sama.iteye.com/blog/2372341
[06] 你所不知道的Java之char默認(rèn)值
http://www.itdecent.cn/p/d20d5f8bb878
[07] JAVA經(jīng)典算法40題
https://www.cnblogs.com/jianmang/articles/4878924.html
[08] java中this關(guān)鍵字的作用
https://www.cnblogs.com/lzq198754/p/5767024.html
[09] java里的靜態(tài)成員變量是放在了堆內(nèi)存還是棧內(nèi)存
https://zhidao.baidu.com/question/1643722234531216060.html
[10] Java基礎(chǔ)-方法區(qū)以及static的內(nèi)存分配圖
https://blog.csdn.net/wang_1997/article/details/52267688

微信掃一掃關(guān)注該公眾號【測試開發(fā)者部落】

image.png

點擊鏈接加入群聊【軟件測試學(xué)習(xí)交流群】
https://jq.qq.com/?_wv=1027&k=5eVEhfN
軟件測試學(xué)習(xí)交流QQ群號:511619105

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容