數組求和
案例代碼
package day04;
public class ArrayTest {
/**
* 對整數數組進行求和
* @param x 表示整數數組
* @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));
}
}
分析
調用sum函數的時候,發生參數的傳遞,數列(也稱數組對象)在堆里面的內存地址0x6d06d69c傳遞給了x, 這樣的話,x也指向了該數列,變量名x可以理解為arr數組的別名。
常量
常量是特殊的變量,常量里面保存的內容不能被改變。
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支足球隊上場比賽的人數
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關鍵字
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表示一個學生對象
// 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);
}
}
this關鍵字
this變量引用當前對象,比如:this.age就表示當前對象的年齡
1)this可以調用屬性(成員變量)
2)this可以調用功能(成員方法)
籃球運動員類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; //球衣號碼,默認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表示投進,false表示沒投進
*/
public boolean shoot() {
int x = (int)(1+Math.random()*100);//x表示一個1~100之間的隨機整數
//假設rate是90,那么x<=90的概率是rate%
if(x<=rate) {//如果x小于等于rate,則投籃投進(得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());//查詢
}
}
static關鍵字
1)靜態成員變量位于方法區,對應的空間個數只有1個
2)非靜態成員變量位于堆內存,對應的空間個數取決于有多少個該類型的對象
3)靜態的成員方法里面不能直接調用非靜態的成員變量
4)靜態的成員方法里面不能直接調用非靜態的成員方法
5)靜態的成員方法里面可以通過對象名調用非靜態的方法,比如:
public static void main(String[] args){
Player linshuhao = new Player();
linshuhao.shoot();//投籃
}
6)靜態的成員方法里面不能直接調用非靜態的成員
7)非靜態的成員方法里面可以直接調用靜態的成員變量
8)非靜態的成員方法里面可以直接調用靜態的成員方法
9)靜態成員可以通過類名調用,也可以通過對象名調用。比如:把成員方法設置成靜態的,我們就可以通過類名去調用,否則的話,需要先實例化,然后通過對象名去調用
10)靜態成員方法里面不能使用this變量
11)靜態代碼塊的語法格式(了解)
static{//JVM在加載類的時候,會執行該類里面的靜態代碼塊,并且該靜態代碼塊只會被執行一次
...
}
學生類
package day06;
/**
* 學生類
* @author yangzc
*
*/
public class Student {
public int sno;//學號
public String sname;//姓名
public boolean isMale;//性別
public int age;//年齡
public static int count=0;//學生的數量
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);
}
}
多態的特點
1)父類的變量可以接收子類對象,比如:
public double pay(Emp obj) {//父類變量obj可以接收子類對象
//obj表面上是員工
if(obj instanceof PM) {//如果obj實際上是項目經理的話
//向下造型(把Emp類型強轉成PM類型)
PM manager = (PM)obj;
return obj.getSalary()+manager.getBonus();
}
return obj.getSalary();
}
2)通過一個指向子類對象的父類引用調用父類的方法,并且子類里面對該方法進行了重寫,那么,執行的時候實際調用的是子類的方法,比如:
Emp e = new SE();//程序員的工資默認是5000.0
e.setSalary(-1000.0);//實際調用的是子類的setSalary接口
System.out.println(e.getSalary());//實際調用的是父類的getSalary接口
說明:
使用多態的話,代碼維護起來比較方便
向上造型(子類可以自動轉換成父類)
Cat類型可以自動轉成Animal類型
//父類變量可以接收子類對象
//Cat是Animal的子類,可以自動轉成Animal
Animal obj = new Cat();//貓是動物,正確
//子類變量不能接收父類對象
Cat cat = new Animal();//動物是貓,錯誤
向下造型(通過強制轉換可以將父類轉換成子類)
把Animal類型強轉成Cat類型
Animal obj = new Cat();
if(obj instanceof Dog){//如果animal所指向的對象是狗的話
//把Animal(父類)強制轉換成Dog(子類)
Dog wangcai = (Dog)obj;
}
if(obj instanceof Cat){//如果animal所指向的對象是貓的話
//把Animal(父類)強制轉換成Cat(子類)
Cat hellokitty = (Cat)obj;
}
Cat類和Dog類之間沒有繼承關系,把Cat類強轉成Dog類會發生編譯錯誤
Cat cat = new Cat();
Dog dog = (Dog)cat;//編譯出錯(不能把Cat強轉成Dog)
把Animal類強轉成Dog類會發生運行異常,因為obj實際上是貓
Animal obj = new Cat();
Dog dog = (Dog)obj;//運行出錯
字符串比較
//判斷兩個字符串對象的內容是否相等
System.out.println(str.equals(str1));
//比較的是對象的內存地址
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);
}
字符串替換
public String replace(char oldChar,char newChar){
...
}
參數:
oldChar - 原字符。
newChar - 新字符。
返回:
一個從此字符串派生的字符串,它將此字符串中的所有 oldChar 替代為 newChar。
說明:
返回一個新的字符串,它是通過用 newChar 替換此字符串中出現的所有 oldChar 得到的。
如果 oldChar 在此 String 對象表示的字符序列中沒有出現,則返回對此 String 對象的引用。否則,創建一個新的 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'));
}
UTF-8編碼和Unicode編碼
漢字字符集查詢:
https://www.qqxiuzi.cn/bianma/zifuji.php
中文字符 | UTF-8編碼(16進制) | Unicode編碼(16進制) | GBK編碼(16進制) |
---|---|---|---|
小 | 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] 運算符的結合性
https://www.cnblogs.com/softwaretesting/archive/2011/08/16/2139068.html
[02] Java運算符優先級
https://www.cnblogs.com/zjfjava/p/5996666.html
[03] 在JAVA命令行中輸入參數,星號是如何處理的?
https://zhidao.baidu.com/question/56783574.html
[04] JVM內存初學 堆、棧、方法區
https://www.cnblogs.com/dingyingsi/p/3760730.html
[05] Java中的堆內存、棧內存和方法區總結
https://orochimaru-sama.iteye.com/blog/2372341
[06] 你所不知道的Java之char默認值
http://www.lxweimin.com/p/d20d5f8bb878
[07] JAVA經典算法40題
https://www.cnblogs.com/jianmang/articles/4878924.html
[08] java中this關鍵字的作用
https://www.cnblogs.com/lzq198754/p/5767024.html
[09] java里的靜態成員變量是放在了堆內存還是棧內存
https://zhidao.baidu.com/question/1643722234531216060.html
[10] Java基礎-方法區以及static的內存分配圖
https://blog.csdn.net/wang_1997/article/details/52267688
微信掃一掃關注該公眾號【測試開發者部落】
點擊鏈接加入群聊【軟件測試學習交流群】
https://jq.qq.com/?_wv=1027&k=5eVEhfN
軟件測試學習交流QQ群號:511619105