java.lang包中常用的類及其方法


JAVA API(圖片來源:實驗樓)

包裝類

Integer包裝類

方法 返回值 功能描述
byteValue() byte 以 byte 類型返回該 Integer 的值
intValue() int 以 int 型返回此 Integer 對象
zebra stripes are neat 以 int 型返回此 Integer 對象
toString() String 返回一個表示該 Integer 值的 String 對象
valueOf(String str) Integer 返回保存指定的 String 值的 Integer 對象
parseInt(String str) int 返回包含在由 str 指定的字符串中的數字的等價數值

//十進制轉成十六進制,返回值為String類型
Integer.toHexString(int i)
//十進制轉成八進制
Integer.toOctalString(int i)
//十進制轉成二進制
Integer.toBinaryString(int i)
//十六進制轉成十進制
Integer.valueOf("FFFF",16)
//八進制轉成十進制
Integer.valueOf("876",8)
//二進制轉十進制
Integer.valueOf("0101",2)
//功能和Integer.valuesOf()一樣
Integer.parseInt(String,radix);

String bb="1100";
String cc="1101";
int e=Integer.valueOf(bb,2);//功能一樣
int ee=Integer.parseInt(cc, 2);
System.out.println(e);
System.out.println(ee);

String類

String類

方法 返回值 功能描述
indexOf(int ch) int 搜索字符 ch 第一次出現的位置
indexOf(String value) int 搜索字符串 value 第一次出現的位置
lastIndexOf(int ch) int 搜索字符ch最后一次出現的位置
lastIndexOf(String value) int 搜索字符串 value 最后一次出現的位置
substring(int index) String 提取從位置索引開始到結束的字符串
substring(int beginindex, int endindex) String 提取beginindex和endindex之間的字符串部分
trim() String 返回一個前后不含任何空格的調用字符串的副本,意思是把字符串前后的空格部分去除

//字符串連接
concat();

StringBuffer類

StringBuffer 類是可變的。

方法 返回值 功能描述
insert(int offsetm,Object s) StringBuffer 在 offetm 的位置插入字符串s
append(Object s) StringBuffer 在字符串末尾追加字符串s
length() int 確定 StringBuffer 對象的長度
setCharAt(int pos,char ch) void 使用 ch 指定的新值設置 pos 指定的位置上的字符
toString() String 轉換為字符串形式
reverse() StringBuffer 反轉字符串
delete(int start, int end) StringBuffer 刪除調用對象中從 start 位置開始直到 end 指定的索引(end-1)位置的字符序列
replace(int start, int end, String s) StringBuffer 使用一組字符替換另一組字符。將用替換字符串從 start 指定的位置開始替換,直到 end 指定的位置結束

Math類

方法 返回值 功能描述
sin(double numvalue) double 計算角 numvalue 的正弦值
cos(double numvalue) double 計算角 numvalue 的余弦值
acos(double numvalue) double 計算 numvalue 的反余弦
asin(double numvalue) double 計算 numvalue 的反正弦
atan(double numvalue) double 計算 numvalue 的反正切
pow(double a, double b) double 計算 a 的 b 次方
sqrt(double numvalue) double 計算給定值的平方根
abs(int numvalue) int 計算 int 類型值 numvalue 的絕對值,也接收 long、float 和 double 類型的參數
ceil(double numvalue) double 返回大於等於 numvalue 的最小整數值
floor(double numvalue) double 返回小於等於 numvalue 的最大整數值
max(int a, int b) int 返回 int 型 a 和 b 中的較大值,也接收 long、float 和 double 類型的參數
min(int a, int b) int 返回 a 和 b 中的較小值,也可接受 long、float 和 double 類型的參數
rint(double numvalue) double 返回最接近 numvalue 的整數值
round(arg) arg 為 double 時返回 long,為 float 時返回 int 返回最接近arg的整數值
random() double 返回一個介於0與1之間的偽隨機數

Class類

String obj=new String();
Class c;
c=obj.getClass();
System.out.println("String對象的類型是"+c.getName());;
c=Integer.class;
System.out.println("Integer對象的類型是"+c.getName());;
c=Class.forName("java.lang.String");
System.out.println("Character對象的類型是"+c.getName());;
c=c.getSuperclass();
System.out.println("Character對象的類型是"+c.getName());;
}

Object類

方法 返回值 功能描述
equals(Objectobj) boolean 將當前對象實例與給定的對象進行比較,檢查它們是否相等
finalize() throws Throwable void 當垃圾回收器確定不存在對象的更多引用時,由對象的垃圾回收器調用此方法。通常被子類重寫
getClass() Class 返回當前對象的 Class 對象
toString() String 返回此對象的字符串表示
wait() throws InterruptedException void 使當前線程進入等待狀態


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM