java中8個常用類
Object類
-
超類、基類,所有類的直接或間接父類,位於繼承數的最頂層
-
任何類,如果沒有寫extends顯示繼承某個類,都預設直接繼承Object類,否則為間接繼承
-
Object類中所定義的方法,是所有物件都具備的方法
-
Object型別可以儲存任何物件
-
作為引數,可接受任何物件
-
-
getClass()方法
public final Class<?> getClass(){}
返回引用中儲存的實際物件型別
應用:通常用於判斷兩個引用中實際儲存物件型別是否一致
package com.wan.class8.object0;
public class GetClass {
private String name;
private int age;
public static void main(String[] args) {
GetClass g1 = new GetClass("aaa",20);
GetClass g2 = new GetClass("bbb",21);
//判斷g1和g2是不是同一個型別
Class class1 = g1.getClass();
Class class2 = g2.getClass();
if(class1 == class2){
System.out.println("g1和g2屬於同一個型別");
}else{
System.out.println("g1和g2不屬於同一個型別");
}
}
public GetClass() {
}
public GetClass(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
hashCode()方法
public int hashCode(){}
返回該物件的雜湊碼值
雜湊值根據物件的地址或字串或數字使用hash演算法計算出來的int型別的數值
一般情況下相同物件返回相同雜湊碼
toString
public String toString(){}
返回該物件的字串表示(表現形式)
可以根據程式需要覆蓋該方法,如:展示物件各個屬性值
equals()方法
public boolean equals(Object obj){}
預設實現為(this == obj),比較兩個物件的地址是否相同
可進行覆蓋,比較兩個物件的內容是否相同
equals()方法覆蓋步驟
-
比較兩個引用是否指向同一個物件
-
判斷obj是否為null
-
判斷兩個引用指向的實際物件型別是否一致
-
強制型別轉換
-
依次比較各個屬性值是否相同
finalize()方法
-
當物件被判定為垃圾物件時,有JVM自動呼叫此方法,用以標記垃圾物件,進入回收佇列
-
垃圾物件:滅有有效引用指向此物件,為垃圾物件
-
垃圾回收:由GC銷燬垃圾物件,釋放資料儲存空間
-
自動回收機制:JVM記憶體耗盡,一次性回收所有垃圾物件
-
手動回收機制:使用System.gc(); 通知JVM執行垃圾回收
-
包裝類
包裝類對應
型別轉換與裝箱、拆箱
裝箱:基本型別轉換為引用型別
拆箱:引用型別轉化為基本型別
8種包裝類提供不同型別的轉換方式:
-
Number父類中提供6個共性方法
-
parseXXX()靜態方法
-
valueOf()靜態方法
注意:需要保證型別相容,否則丟擲NumberFormatException異常
整數緩衝區
-
java預先建立了256個常用的整數包裝型別物件
-128~127
-
在實際應用當中,對已建立的物件進行復用
String類
-
字串是常量,建立之後不可改變
-
字串字面值儲存的字串池中,可以共享
-
String s = "hello";產生一個物件,字串池中儲存
-
String s = new String("hello");//產生兩個物件,堆、池各儲存一個
常用方法
-
public int length():返回字串的長度
-
public char charAt(int index):根據下標獲取字元
-
public boolean caotains(String str):判斷當前字串中是否包含str
-
public char[] toCharArray():將字串轉換成陣列
-
public int indexOf(String str):查詢str首次出現的下標,存在,則返回下標;不存在,則返回-1
-
public int lastIndexOf(String str);查詢字串在當前字串中最後一次出現的下標索引
-
public String trim();去掉字串前後的空格
-
public String toUpperCase();將小寫轉成大寫
-
public boolean endWith(String str);判斷字串是否已str結尾
-
public String replace(char oldChar,char newChar);將就字串替換成新字串
-
public String[] split(String str);根據str做拆分
-
新增
案例演示
package com.wan.class8.string0;
/*
案例演示
需求:
已知String str = "this is a text";
1.將str中的單詞單獨獲取出來
2.將str中的text替換為practice
3.在text前面插入一個easy
4.將每個單詞的首字母改為大寫
*/
public class Demo03 {
public static void main(String[] args) {
String str = "this is a text";
System.out.println("++++++++1將str中的單詞單獨獲取出來++++++++");
//1將str中的單詞單獨獲取出來
String[] str1 = str.split(" ");
for (int i = 0; i < str1.length; i++) {
System.out.println(str1[i]);
}
System.out.println("++++++++2將str中的text替換為practice++++++++");
//2將str中的text替換為practice
System.out.println(str.replace("text", "practice"));
System.out.println("++++++++3在text前面插入一個easy++++++++");
//3.在text前面插入一個easy
System.out.println(str.replace("text","easy text"));
System.out.println("++++++++4.將每個單詞的首字母改為大寫++++++++");
//4.將每個單詞的首字母改為大寫
for (int i = 0; i < str1.length; i++) {
char first = str1[i].charAt(0);
//把第一個字元轉成大寫
char upperfirst = Character.toUpperCase(first);
String news = upperfirst+str1[i].substring(1);
System.out.print(news+" ");
}
}
}
可變字串
-
StringBuffer:可邊長字串,JDK1.0提供,執行效率慢、執行緒安全
-
StringBuilder:可邊長字串,JDK5.0提供,執行效率快、執行緒不安全
StringBuffer與StringBuilder擁有相同的方法
BigDecimal類
double是近似儲存,不符合實際要求,需要藉助BigDeximal
-
位置:java.math包中
-
作用:精確計算浮點數
-
建立方式:BigDecimal bd new BigDecimal("1.0");
-
方法:
-
BigDecimal add(BigDecimal bd) 加
-
BigDecimal subtract(BigDecimal bd) 減
-
BigDecimal multiply(BigDecimal bd) 乘
-
BigDecimal divide(BigDecimal bd) 除
-
package com.wan.class8.bigdecimal;
import java.math.BigDecimal;
public class Demo01 {
public static void main(String[] args) {
double d1 = 1.0;
double d2 = 0.9;
System.out.println(d1-d2);
//面試題
double result = (1.4-0.5)/0.9;
System.out.println(result);
//BigDecimal,大的浮點數精確計算
BigDecimal bd1 = new BigDecimal("1.0");
BigDecimal bd2 = new BigDecimal("0.9");
BigDecimal r1 = bd1.subtract(bd2);//減法subtract
BigDecimal r2 = bd1.add(bd2);//加法add
BigDecimal r3 = bd1.multiply(bd2);//乘法multiply
BigDecimal r4 = new BigDecimal("1.4")
.subtract(new BigDecimal("0.5"))
.divide(new BigDecimal("0.9"));//除法divide
System.out.println(r1);
System.out.println(r2);
System.out.println(r3);
System.out.println(r4);
BigDecimal r5 = new BigDecimal("20").divide(new BigDecimal("3"),2,BigDecimal.ROUND_HALF_UP);//四捨五入
System.out.println(r5);
}
}
除法:divide(BigDecimal bd,int scal,RoundingMode mode)
引數sacl:指定精確到小數點後幾位
引數mode:
-
指定小數部分的取捨模式,通常採用四捨五入的模式
-
取值為BigDecimal.ROUND_HALF_UP
Date類
Date表示特定的瞬間,精確到毫秒。Date類中的大部分方法都已經被Calendar類中的方法做取代
時間單位:
-
1秒=1000毫秒
-
1毫秒=1000微秒
-
1微秒=1000納秒
package com.wan.class8.date0;
import java.util.Date;
public class Demo01 {
public static void main(String[] args) {
//建立Date物件
//今天
Date date1 = new Date();
System.out.println(date1.toString());
System.out.println(date1.toLocaleString());
//昨天
Date date2 = new Date(date1.getTime()-60*60*24*1000);
System.out.println(date2.toLocaleString());
//2方法after before
boolean b1 = date1.after(date2);
System.out.println(b1);
boolean b2 = date1.before(date2);
System.out.println(b2);
//比較compareTo();
int d = date1.compareTo(date2);
System.out.println(d);
//比較是否相等equals();
boolean b3 = date1.equals(date2);
System.out.println(b3);
}
}
Calendar
-
Calendar提供了獲取或設定各種日曆欄位的方法
-
構造方法
-
protected Calendar():由於修飾符是protected,所以無法直接建立該物件。
-
-
其他方法
package com.wan.class8.calendar;
import java.util.Calendar;
public class Demo01 {
public static void main(String[] args) {
//1.建立Calendar物件
Calendar calendar = Calendar.getInstance();
System.out.println(calendar.getTime().toLocaleString());
System.out.println(calendar.getTimeInMillis());
//2.獲取時間資訊
//獲取年
int year = calendar.get(Calendar.YEAR);
//月
int month = calendar.get(Calendar.MONTH);
//日
int day = calendar.get(Calendar.DAY_OF_MONTH);
//小時
int hour = calendar.get(Calendar.HOUR_OF_DAY);//HOUR 12小時,HOUR_OF_DAY 4小時
//分鐘
int minute = calendar.get(Calendar.MINUTE);
//秒
int second = calendar.get(Calendar.SECOND);
System.out.println(year+"年"+(month+1)+"月"+day+"日"+hour+":"+minute+":"+second);
//修改時間
Calendar calendar2 = Calendar.getInstance();
calendar2.set(Calendar.DAY_OF_MONTH,10);
System.out.println(calendar2.getTime().toLocaleString());
//4.add修改時間
calendar2.add(Calendar.HOUR,1);//加一小時
System.out.println(calendar2.getTime().toLocaleString());
//5.補充方法
calendar2.add(Calendar.MONTH,1);//加一月
int max = calendar2.getActualMaximum(Calendar.DAY_OF_MONTH);
int min = calendar2.getActualMinimum(Calendar.DAY_OF_MONTH);
System.out.println(max);
System.out.println(min);
}
}
SimpleDateFormat
-
SimpleDateFormat是一個以語言環境有關的方式來格式化和解析日期的具體類
-
進行格式化(日期——>文字)、解析(文字——>日期)
-
常用的時間模式字母
System類
System系統類,主要用於獲取系統的屬性資料和其他操作,構造方法私有的。
package com.wan.class8.system0;
import java.util.Arrays;
public class Demo01 {
public static void main(String[] args) {
//1.arraycopy:陣列的複製
//src:原陣列
//srcPos:從哪個位置開始複製
//dest:目標陣列
//destPos:目標陣列位置
//length:複製的長度
int[] arr = {10,20,34,30,23,43,44,54};
int[] dest = new int[8];
System.arraycopy(arr,4,dest,3,4);
for (int i = 0; i < dest.length; i++) {
System.out.println(dest[i]);
}
// Arrays.copyOf();實際就是用的System.arraycopy();
//2.獲取毫秒數
System.out.println(System.currentTimeMillis());
//用於計時
long start = System.currentTimeMillis();
for (int i = 0; i < 999999999; i++) {
for (int j = 0; j < 9999999; j++) {
int result = i+j;
}
}
long end = System.currentTimeMillis();
System.out.println("用時:"+(end-start));
new Demo02("aaa",19);
new Demo02("bbb",21);
new Demo02("ccc",20);
//3.回收垃圾
System.gc();//System.gc();告訴垃圾回收器回收
//4.退出jvm
System.exit(0);
System.out.println("程式結束了。。。。");
}
}