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