Java入門到精通(一)語言基礎
Java入門到精通(二)流程控制
Java入門到精通(三)數組介紹
Java入門到精通(四)字符串
Java入門到精通(五)面向對象
java入門到精通(六)面向對象知識拓展
講給老年人聽的異常處理
給大家安利一個jdk文檔查看網站各種jdk版本文檔都可以查看,我下面文章只提及常見的方法
jdk文檔查看
包裝類
包裝類可以將基本數據類型的數據轉換爲對象進行處理。
包裝類及其對應的基本類型
包裝類 | 對應的數據類型 |
---|---|
Byte | byte |
Integer | int |
Float | float |
Character | char |
Short | short |
Long | long |
Double | double |
Boolean | boolean |
Integer類
Integer的構造函數
Integer(int val)
//Constructs a newly allocated Integer object that represents the specified int value.
//傳入一個int變量獲取Integer對象
Integer(String str)
//Constructs a newly allocated Integer object that represents the int value indicated by the String parameter.
//傳入一個String變量獲取Integer對象,注意:字符串變量一定要是數值型否則編譯器會拋出NumberFormatException。
方法 | 返回值 | 描述 |
---|---|---|
valueOf(String str) | Integer | 返回保存指定的String值的Integer對象 |
parseInt(String str) | int | 返回包含在由str指定的字符串中的數字的等價整數值 |
toString() | String | 返回一個表示該Integer值的String對象 |
equals(Object obj) | boolean | 比較此對象與指定的對象是否相等 |
intValue() | int | 以int型返回此Integer對象 |
compareTo(IntegeranotherInteger) | int | 在數字上比較兩個Integer對象、如果這兩個值相等,則返回0;如果小於IntegeranotherInteger返回負值,反之返回正值 |
Integer 常量
常見的常量有哪些?
MAX_VALUE = 2^31-1
MIN_VALUE = -2^31
SIZE 用來以二進制的形式來表示int值的位數
TYPE 表示基本類型int的Class實列
Double類
Double對象的構造函數
Double(double value)
//Constructs a newly allocated Double object that represents the primitive double argument.
//傳入double形式的參數創建Double對象
Double(String s)
//Constructs a newly allocated Double object that represents the floating-point value of type double represented by the string.
//傳入String變量作爲參數創建Double對象
方法 | 返回值 | 描述 |
---|---|---|
valueOf(String str) | Double | 返回保存指定的String值的Double對象 |
parseDouble(String str) | double | 將String變量轉換爲Double返回一個Double值 |
doubleValue () | double | 以double形式返回Double對象 |
intValue() | int | 將double轉換爲int型 |
compareTo(IntegeranotherInteger) | int | 在數字上比較兩個Integer對象、如果這兩個值相等,則返回0;如果小於IntegeranotherInteger返回負值,反之返回正值 |
equals(Object obj) | boolean | 比較此對象與指定的對象是否相等 |
toString() | String | 返回一個表示該double值的String對象 |
Boolen類
Boolean(boolean value)
//Allocates a Boolean object representing the value argument.
//創建表示value參數的對象
Boolean(String s)
//Allocates a Boolean object representing the value true if the string argument is not null and is equal, ignoring case, to the string "true".
//以String變量作爲參數創建對象
方法 | 返回值 | 描述 |
---|---|---|
booleanValue() | boolean | 將Boolean對象的值以對應的boolean值返回 |
equals(Object obj) | boolean | 比較此對象與指定的對象是否相等並且值不爲null |
parseBoolean(String s) | boolean | 將字符串轉換爲Boolean |
toString() | String | 返回一個表示該boolean值的String對象 |
valueOf(String s) | boolean | 返回一個用指定字符串表示值的Boolean值 |
Character類
Character的構造函數
Character(char value)
Constructs a newly allocated Character object that represents the specified char value.
//將基本數據類型char轉換爲Character對象
方法 | 返回值 | 描述 |
---|---|---|
compareTo(IntegeranotherInteger) | int | 在數字上比較兩個Integer對象、如果這兩個值相等,則返回0 |
equals(Object obj) | boolean | 比較此對象與指定的對象是否相等 |
toUpperCase(char ch) | char | 將ch變爲大寫 |
toLowerCase(char ch) | char | 將ch變爲小寫 |
toString() | String | 返回一個表示該char值的String對象 |
charValue() | char | 返回此Character對象的值 |
isDigit(char ch) | boolean | 判斷是否是數字 |
isLetter(char ch) | boolean | 判斷是否是字母 |
Math數學類
static double E
//自然對數底數e的值
//The double value that is closer than any other to e, the base of the natural logarithms.
static double PI
//表示圓周率PI的值
//The double value that is closer than any other to pi, the ratio of the circumference of a circle to its diameter.
Math類常見的方法
方法 | 介紹 |
---|---|
sqrt(double a) | a的平方根 |
pow(double a,double b) | a的b次方 |
ceil(double a) | 將a向上取整 |
floor(double a) | 將a向下取整 |
max(double a,double b) | 將a和b做比較返回最大值 |
min(double a,double b) | 將a和b作比較返回最小值 |
abs(double a) | 放回a的絕對值 |
random() | 生成隨機數 |
//random()方法 產生1~100的隨機數切包括100
System.out.println(1 + Math.random()*100);
System.out.println(Math.random()*101);
Random類
java編譯器以系統當前時間作爲隨機數生成器種子,因爲每時每刻的時間不可能相同,所以生成的隨機數將不同,但是如果運行速度太快,也會生成兩次運行結果相同的隨機數。
Random random = new Random();
int i = random.nextInt() * 100;//產生隨機整數
float v = random.nextFloat()*100;//產生隨機浮點數
boolean b = random.nextBoolean();//產生隨機布爾值
拆裝包
裝包
自動裝包:把基本類型用它們對應的引用類型包裝起來,使它們具有對象的特質,可以調用toString()、hashCode()、getClass()、equals()等方法。
Integer a = 10;//裝包
編譯器調用的是valueOf(int i)返回一個表示指定int值的Integer對象,如下:
Integer a = 10; => Integer a = Integer.valueOf(10);
Integer a = 100;
Integer b = 200;
Integer c = 100;
Integer d = 200;
Integer e = new Integer(10);
Integer f = new Integer(10);
System.out.println(a == b);//false
System.out.println(a == c);//true
System.out.println(b == d);//false
System.out.println(e == f);//false
System.out.println(b.equals(d));//true
System.out.println(e.equals(f));//true
- a與b兩個值不相等,並且使兩個不同的對象所以會輸出false
- a與c輸出true兩個值相等,且是同一個對象,所以輸出true
- b與d兩個不同的對象,值相等,但是==比較的是兩個對象的地址所以輸出false
- e與f兩個不同的對象,值相等,兩個對象的地址不同輸出false
- 最後兩個輸出語句是調用equals方法比較地址下面的值
以下是裝包的源碼:
public static Integer valueOf(int i){
if(i>=IntegerCache.low && i<=IntegerCache.high){
return IntegerCache.cache[i+(-IntegerCache.low)];
}
return new Integer(i);
}
-
兩個Integer比較的時候,由於直接賦值的時候會進行自動的裝箱,一個是-128<=
x<=127的整數,將會直接緩存在IntegerCache中,當賦值在這個區間的時候,不會創建新的Integer對象,而是從緩存中獲取已經創建好的Integer對象。 -
當賦值不在這個範圍內,編譯器直接創建對象,就是上述代碼中的Integer b 與 Integer d作比較。
拆包
所謂拆包就是將引用類型轉換爲基本數據類型,編譯器內部會調用int intValue()返回該Integer對象的int值。
int a = new Integer(10);//拆箱
自動裝箱和拆箱是由編譯器來完成的,編譯器會在編譯期根據語法決定是否進行裝箱和拆箱動作。
Integer a = 100;
int b = 100;
Integer c = 200;
System.out.println(c > a);//true
System.out.println(b == a);//true
- c 與 a將c、a拆包成基本數據類型 然後相互作比較輸出true
- b 與 a將a拆包成基本數據類型然後與b作值比較
描述不對的地方麻煩大佬補充
看完感覺有用的點個贊,沒有的也點個贊