生活随笔
收集整理的這篇文章主要介紹了
Java核心类库篇2——lang
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Java核心類庫篇2——lang
1、Object
該類是所有類的父類,每個類都使用它作為超類,沒有任何屬性
方法聲明功能介紹
| Object() | 使用無參方式構造對象 |
| boolean equals(Object obj) | 用于判斷調用對象是否與參數對象相等。 該方法默認比較兩個對象的地址是否相等 |
| int hashCode() | 用于獲取調用對象的哈希碼值(內存地址的編號) |
| String toString() | 用于獲取調用對象的字符串形式 該方法默認返回的字符串為:包名.類名@哈希碼值的十六進制 |
| Class getClass() | 用于返回調用對象執行時的Class實例,反射機制使用 |
- equals:與 == 運算符的結果一致,若希望比較兩個對象的內容,則需要重寫該方法。 若該方法被重寫后,則應該重寫hashCode方法來保證結果的一致性
- hashCode:若兩個對象調用equals方法相等,則各自調用該方法的結果必須相同,若兩個調用對象equals方法不相等,則各自調用該方法的結果應該不相同,為了使得該方法與equals方法保持一致,需要重寫該方法
- toString:為了返回更有意義的數據,需要重寫該方法 使用print或println打印引用或字符串拼接引用都會自動調用該方法
2、包裝類
基本類型并不具有對象的性質,為了與其他對象“接軌”就出現了包裝類型(如我們在使用集合類型Collection時就一定要使用包裝類型而非基本類型),它相當于將基本類型“包裝起來”,使得它具有了對象的性質,并且為其添加了屬性和方法,豐富了基本類型的操作。
基本類型包裝類
| boolean | java.lang.Boolean |
| char | java.lang.Character |
| byte | java.lang.Byte |
| short | java.lang.Short |
| int | java.lang.Integer |
| long | java.lang.Long |
| float | java.lang.Float |
| double | java.lang.Double |
- 基本類型的優勢:數據存儲相對簡單,運算效率比較高
- 包裝類的優勢:有的容易,比如集合的元素必須是對象類型,滿足了java一切皆是對象的思想
- 聲明方式不同,基本類型不適用new關鍵字,而包裝類型需要使用new關鍵字來在堆中分配存儲空間;
- 存儲方式及位置不同,基本類型是直接將變量值存儲在堆棧中,而包裝類型是將對象放在堆中,然后通過引用來使用;
- 初始值不同,基本類型的初始值如int為0,boolean為false,而包裝類型的初始值為null
- 使用方式不同,基本類型直接賦值直接使用就好,而包裝類型在集合如Collection、Map時會使用到
2.1、包裝類的裝箱拆箱
public class Test {public static void main(String[] args
) {int a
=10;Integer integer
=a
;int b
=integer
;System.out
.println(a
);System.out
.println(integer
);System.out
.println(b
);int a1
=10;Integer integer1
=new Integer(a1
);int b1
=integer1
.intValue();System.out
.println(a
);System.out
.println(integer
);System.out
.println(b
);}
}
2.2、包裝類與字符串的轉換
public class Test {public static void main(String[] args
) {String a
="100";System.out
.println(Integer.parseInt(a
));String b
="100.2";System.out
.println(Float.parseFloat(b
));String c
="true";System.out
.println(Boolean.parseBoolean(c
));Integer d
=100;System.out
.println(d
.toString());}
}
3、Math
3.1、屬性
public class Test {public static void main(String[] args
) {System.out
.println("math屬性================");System.out
.println(Math.PI
);System.out
.println(Math.E);}
}
math屬性================
3.141592653589793
2.718281828459045
3.2、三角函數方法
方法聲明功能介紹
| public static double sin(double radians) | 正弦函數 |
| public static double cos(double radians) | 余弦函數 |
| public static double tan(double radians) | 正切函數 |
| public static double toRadians(double degree) | 度轉換成弧度 |
| public static double toDegree(double radians) | 弧度轉換成度 |
| public static double asin(double a) | 反正弦 |
| public static double acos(double a) | 反余弦 |
3.2、指數函數方法
方法聲明功能介紹
| public static double exp(double x) | e^x |
| public static double log(double x) | ln(x) |
| public static double log10(double x) | log 10(x) |
| public static double pow(double a,double b) | a^b |
| public static double sqrt(double x) | √x |
3.3、取整方法
方法聲明功能介紹
| public static double ceil(double x) | 天花板的意思,就是逢余進一 |
| public static double floor(double x) | 地板的意思,就是逢余舍一 |
public class Test {public static void main(String[] args
) {double a
=2.5;System.out
.println(Math.ceil(a
));System.out
.println(Math.floor(a
));}
}
3.0
2.0
3.4、min、max、abs方法
方法聲明功能介紹
| public static int abs(int a) | 返回一個數的絕對值 |
| public static int min(int a, int b) | 返回兩個數的最小值 |
| public static int max(int a, int b) | 返回兩個數的最大值 |
| public static double random() | 生成大于等于0.0且小于1.0的double型隨機數 |
public class Test {public static void main(String[] args
) {int a
=5;int b
=9;System.out
.println(Math.max(a
,b
));System.out
.println(Math.min(a
,b
));int c
=-9;System.out
.println(Math.abs(c
));System.out
.println(Math.random());}
}
9
5
9
0.30741550461739375
4、String
- String類被final關鍵字修飾,意味著String類不能被繼承,并且它的成員方法都默認為final方法
- 字符串一旦創建就不能再修改
- String類實現了Serializable、CharSequence、 Comparable接口
- String實例的值是通過字符數組實現字符串存儲的
4.1、構造方法
方法聲明功能介紹
| String str=“hello world!” | 字面量創建 |
| public String() | 無參構造方法 |
| public String(String original) | 用已知的字符串value創建一個String對象 |
| public String(char value[]) | 用字符數組value創建一個String對象 |
| public String(char value[], int offset, int count) | 用字符數組chars的offset開始的count個字符創建一個String對象 |
| public String(byte bytes[]) | 用byte數組values創建一個String對象 |
常用的為字面量創建
4.2、字符串長度
方法聲明功能介紹
| public int length() | 字符串的長度 |
public class Test {public static void main(String[] args
) {String str
="hello world!";System.out
.println(str
.length());}
}
12
4.3、字符串中指定位置的字符
方法聲明功能介紹
| public char charAt(int index) | 字符串中指定位置的字符 |
public class Test {public static void main(String[] args
) {String str
="hello world!";System.out
.println(str
.charAt(1));}
}
e
4.4、提取子串
方法聲明功能介紹
| public String substring(int beginIndex) | 從beginIndex位置起,從當前字符串中取出剩余的字符 |
| public String substring(int beginIndex, int endIndex) | 從beginIndex位置起,從當前字符串中取出到endIndex-1位置的字符 |
public class Test {public static void main(String[] args
) {String str
="hello world!";System.out
.println(str
.substring(2));System.out
.println(str
.substring(2,4));}
}
llo world!
ll
4.5、字符串比較
方法聲明功能介紹
| public int compareTo(String anotherString) | 對字符串內容按字典順序進行大小比較 |
| public int compareToIgnore(String anotherString) | 同上,忽略大小寫 |
| public boolean equals(Object anotherObject) | 比較當前字符串和參數字符串 |
| public boolean equalsIgnoreCase(String anotherString) | 比較字符串,忽略大小寫 |
public class Test {public static void main(String[] args
) {String str
="a";String str1
="A";System.out
.println(str
.compareTo(str1
));System.out
.println(str
.compareToIgnoreCase(str1
));System.out
.println("===========================");System.out
.println(str
.equals(str1
));System.out
.println(str
.equalsIgnoreCase(str1
));}
}
32
0
===========================
false
true
4.6、字符串連接
方法聲明功能介紹
| public String concat(String str) | 將字符串str連接到當前字符串的后面,效果等價于"+" |
與直接+一致
4.7、字符串中單個字符查找
方法聲明功能介紹
| public int indexOf(int ch/String str) | 查找當前字符串中字符或子串,返回第一次出現的位置 |
| public int indexOf(int ch/String str, int fromIndex) | 從fromIndex開始查找當前字符串中字符或子串,返回第一次出現的位置 |
| public int lastIndexOf(int ch/String str) | 倒著查找當前字符串中字符或子串,返回第一次出現的位置 |
| public int lastIndexOf(int ch/String str, int fromIndex) | 倒著從fromIndex開始查找當前字符串中字符或子串,返回第一次出現的位置 |
public class Test {public static void main(String[] args
) {String str
="hello world!";System.out
.println(str
.indexOf('o'));System.out
.println(str
.lastIndexOf('o'));}
}
4
7
4.8、字符串大小寫轉換
方法聲明功能介紹
| public String toLowerCase() | 所有字符轉換成小寫 |
| public String toUpperCase() | 所有字符轉換成大寫 |
public class Test {public static void main(String[] args
) {String str
="Hello World!";System.out
.println(str
.toUpperCase());System.out
.println(str
.toLowerCase());}
}
HELLO WORLD!
hello world!
4.9、字符串中字符的替換
方法聲明功能介紹
| public String replace(char oldChar, char newChar) | 用字符newChar替換當前字符串中所有的oldChar字符 |
| public String replaceFirst(String regex, String replacement) | 用字符replacement的內容替換當前字符串中遇到的第一個和字符串regex相匹配的子串 |
| public String replaceAll(String regex, String replacement) | 用字符replacement的內容替換當前字符串中遇到的所有和字符串regex相匹配的子串 |
public class Test {public static void main(String[] args
) {String str
="Hello World!";System.out
.println(str
.replace('o','v'));}
}
Hellv Wvrld!
4.10、其他類方法
方法聲明功能介紹
| public String trim() | 截去字符串兩端的空格,但對于中間的空格不處理 |
| public boolean startsWith(String prefix) | 判斷當前字符串是否以指定字符串開始 |
| public boolean endWith(String suffix) | 判斷當前字符串是否以指定字符串結束 |
| public contains(String str) | 判斷參數s是否被包含在字符串中 |
| public String[] split(String str) | 將str作為分隔符進行字符串分解 |
public class Test {public static void main(String[] args
) {String str
=" Hello World! ";System.out
.println(str
.trim());System.out
.println(str
.contains("ello"));System.out
.println(str
.startsWith(" Hello"));System.out
.println(str
.endsWith("World! "));}
}
Hello World!
true
true
true
4.11、字符串轉為基本類型
方法聲明功能介紹
| public static byte parseByte(String s) | String轉為byte |
| public static short parseShort(String s) | String轉為short |
| public static short parseInt(String s) | String轉為int |
| public static long parseLong(String s) | String轉為long |
| public static float parseFloat(String s) | String轉為float |
| public static double parseDouble(String s) | String轉為double |
4.12、基本類型轉為字符串
方法聲明功能介紹
| public static String valueOf(char data[]) | byte轉String |
| public static String valueOf(char data[], int offset, int count) | char[]轉String |
| public static String valueOf(boolean b) | boolean轉String |
| public static String valueOf(char c) | char轉String |
| public static String valueOf(int i) | int轉String |
| public static String valueOf(long l) | long轉String |
| public static String valueOf(float f) | float轉String |
| public static String valueOf(double d) | double轉String |
4.13、StringBuffer
4.13.1、構造函數
方法聲明功能介紹
| public StringBuffer() | 構造一個字符串緩沖區,其中沒有字符,初始容量為16個字符 |
| public StringBuffer(CharSequence seq) | 構造一個包含與指定字符相同的字符串緩沖區 |
| public StringBuffer(int capacity) | 構造一個字符串緩沖區,其中沒有字符,但是包含指定的初始容量capacity |
| public StringBuffer(String str) | 構造一個指定字符串內容的字符串緩沖區 |
4.13.2、方法
方法聲明功能介紹
| public StringBuffer append(String str) | 將指定的字符串追加到此字符序列 |
| public StringBuffer append(StringBuffer sb) | 將指定的內容附加StringBuffer到此序列 |
| public int capacity() | 返回當前容量 |
| public char charAt(int index) | 返回char指定索引處的此序列中的值 |
| public StringBuffer delete(int start, int end) | 刪除此序列的子字符串中的字符 |
| public StringBuffer deleteCharAt(int index) | 刪除指定位置字符 |
| public int indexOf(String str) | 指定子字符串第一次出現的字符串中的索引 |
| public StringBuffer insert(int offset, String str) | 將字符串插入此字符序列 |
| public int length() | 返回該字符串的長度 |
| public StringBuffer replace(int start, int end, String str) | 用指定的字符替換此序列的子字符串中的字符String |
| public StringBuffer reverse() | 此字符序列的反向替換 |
| public void setCharAt(int index, char ch) | 指定索引處的字符設置為ch |
| public void setLength(int newLength) | 設置字符序列的長度 |
| public String toString() | 此序列中數據的字符串 |
4.14、StringBuilder
- StringBuilder類的對象能夠被多次的修改,并且不產生新的未使用對象
- 屬于非線程安全的類,效率比較高
4.14.1、構造方法
方法聲明功能介紹
| public StringBuilder() | 構造一個沒有字符的字符串構建器,初始容量為16個字符 |
| public StringBuilder(CharSequence seq) | 構造一個包含與指定的相同字符的字符串構建器 |
| public StringBuilder(int capacity) | 造一個沒有字符的字符串構建器,由構capacity參數指定的初始容量 |
| public StringBuilder(String str) | 構造一個初始化為指定字符串內容的字符串構建器 |
4.14.2、方法
方法聲明功能介紹
| public StringBuilder append(String str) | 將指定的字符串附加到此字符序列 |
| public StringBuilder append(StringBuffer sb) | 將指定 StringBuffer追加到這個序列 |
| public int capacity() | 當前容量 |
| public char charAt(int index) | 獲取指定索引位的字符 |
| public StringBuilder delete(int start, int end) | 刪除指定索引間的字符串 |
| public StringBuilde deleteCharAt(int index) | 刪除指定位置的字符 |
| public int indexOf(String str) | 指定子字符串第一次出現的字符串內的索引 |
| public StringBuilder insert(int offset, String str) | 將字符串插入到此字符序列中 |
| public int length() | 返回長度 |
| public StringBuilder replace(int start, int end, String str) | 用指定的String中的字符替換此序列的子字符串中的String |
| public StringBuilder reverse() | 字符串順序顛倒 |
| public void setCharAt(int index, char ch) | 指定索引處的字符設置為ch |
| public void setLength(int newLength) | 設置字符序列的長度 |
| public String substring(int start) | 返回指定位置后的字符串 |
| public String toString() | 此順序中的數據的字符串 |
5、System
主要用于獲取系統的屬性數據
5.1、屬性
屬性介紹
| PrintStream | err標準錯誤輸出流 |
| InputStream | in標準輸入流 |
| PrintStream | out標準輸出流 |
5.2、方法
方法聲明功能介紹
| public static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length) | 將指定源數組從指定位置復制到目標數組的指定位置 |
| public static long currentTimeMillis() | 返回當前時間(以毫秒為單位) |
| static void exit(int status) | 終止當前運行的Java虛擬機 |
| public static void gc() | 提醒運行垃圾回收器 |
| public static String getenv(String name) | 獲取指定環境變量的值 |
| static Properties getProperties() | 獲取當前的系統屬性 |
| static String getProperty(String key) | 獲取指定鍵指示的系統屬性 |
| static String lineSeparator() | 系統相關的行分隔符字符串 |
| static void setProperties(Properties props) | 將系統屬性設置為Properties參數 |
| static void setSecurityManager(SecurityManager s) | 設置系統安全性 |
public class Test {public static void main(String[] args
) {System.out
.println(System.currentTimeMillis());System.out
.println(System.getenv());}
}
總結
以上是生活随笔為你收集整理的Java核心类库篇2——lang的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。