1. 程式人生 > 其它 >《Redis設計與實現》讀書筆記(一)——簡單動態字串(SDS)

《Redis設計與實現》讀書筆記(一)——簡單動態字串(SDS)

  • Object類
Object類位於java.lang包中,java.lang包包含著Java最基礎和核⼼的類,在編譯時會⾃動導⼊
Object類是所有Java類的祖先,每個類都使⽤ Object 作為超類

# 常用方法
public final native Class<?> getClass()
    獲取物件的運⾏時class物件,class物件就是描述物件所屬類的物件, 類的物件可以獲取這個類的基本資訊,如名、包、欄位、⽅法等(⽤於反射會⽐較多,以後有機會再瞭解)
public native int hashCode()
    獲取物件的雜湊值,集合框架中應⽤,⽐如HashMap
public boolean equals(Object obj)
    ⽐較兩個物件,如果這兩個物件引⽤指向的是同⼀個物件,那麼返回true,否則返回false集合框架中有講
public String toString()
    ⽤於返回⼀個可代表物件的字串,看原始碼可以得知,預設返回格式如下:物件的class名稱 + @ + hashCode的⼗六進位制字串,所以前⾯課程寫物件時候,需要重寫這個⽅法,⽅便除錯
  • Math類
Java運算元學運算相關的類
建構函式被私有化,所以不允許建立物件
都是靜態⽅法,使⽤是直接類名.⽅法名

# 常用api
//計算平⽅根
System.out.println(Math.sqrt(16));

//計算⽴⽅根
System.out.println(Math.cbrt(8));

//兩個數的最⼤,⽀持int, long, float,double
System.out.println(Math.max(2.9,4.5));
System.out.println(Math.min(2.9,4.5));

//ceil向上取整,更⼤的值⽅向靠攏, 中⽂是天花板
System.out.println(Math.ceil(19.7));
System.out.println(Math.ceil(-20.1));

//floor向下取整,更⼩的值⽅向靠攏,中⽂是地板意思
System.out.println(Math.floor(19.7));
System.out.println(Math.floor(-20.1)); 

//隨機數
System.out.println(Math.random()); //⼩於1⼤於0的double型別的數

//產⽣1到10的隨機數,int⽅法進⾏轉換它會去掉⼩數掉後⾯的數字即只獲取整數部分,不是四舍五⼊
int random=(int)(Math.random()*10+1);
  • String
字串是物件,不是簡單資料型別
封裝在java.lang包,⾃動導⼊

# 字串比較
== 是⽐較地址
內容是否相等需要⽤ equals()⽅法⽐較

# 常用api
String str = "adsfasdfas"
//獲取字串⻓度:
str.length();

//通過下標獲取字元:
char char = str.charAt(5);

//字串⽐較:
boolean result = str1.equals(str2);

//字串⽐較忽略⼤⼩寫
boolean result = str1.equals(str2);

//查詢字串出現的位置
int index = str.indexOf(".");

//字串擷取
String result1 = str.substring(index);
String result2 = str.substring(index1, index2);

//字串拆分
String [] arr = str.split("\\.");

//字串替換
str.replace("x","a");

//字串⼤⼩寫轉換
str.toUpperCase();
str.toLowerCase();

//字串去除空格
str1.trim();

# 型別轉換
boolean bool = Boolean.getBoolean("false"); //字串型別轉換為布林型別
int integer = Integer.parseInt("20"); //字串型別轉換為整形
long LongInt = Long.parseLong("1024"); //字串型別轉換為⻓整形
float f = Float.parseFloat("1.521"); //字串型別轉換為單精度浮點型
double d = Double.parseDouble("1.52123");//字串型別轉換為雙精度浮點型
  • System類
位於java.lang包中,它是系統類,代表程式所在系統,提供了對應的⼀些系統屬性資訊和系統操作
final型別,建構函式被私有化

# 常用api
//輸⼊輸出包含三個成員變數,分別是in,out,err
System.out //常⽤除錯
System.in //⽤於資料讀取,少⽤
System.err //⽤在錯誤輸出,少⽤

//獲取系統當前毫秒值
System.currentTimeMillis()

//獲取系統環境的屬性
System.getProperties()

//根據指定key獲取系統屬性
System.getProperties(key)
  • 基礎資料型別對應的包裝型別
# Java是⼀個⾯相物件的程式設計語⾔,但基本型別並不具有物件的性質,為了讓基本型別也具有物件的特徵,就出現了包裝型別
# 集合框架⾥⾯需要儲存物件,不能儲存基本資料型別,所以需要儲存包裝型別

基本型別  包裝器型別
boolean   Boolean
char      Character
int       Integer
byte      Byte
short     Short
long      Long
float     Float
double    Double