Java-常用类
1. Object类
-
超类、基类,所有类的直接或间接父类,位于继承树的最顶层。
-
任何类,如没有书写extends显示继承某个类,都默认直接继承Object类,否则为间接继承。
-
Object类中所定义的方法,是所有对象都具备的方法。
-
Object类型剋有存储任何对象。
-
作为参数,可接受任何对象。
-
作为返回值,可返回任何对象。
-
getClass()方法
-
public final Class<?> getClass(){}
-
返回引用中存储的实际对象类型。
-
应用:通常用于判断两个引用中实际存储对象类型是否一致。
hashCode()方法
-
public int hashCode(){}
-
返回该对象的哈希码值
-
哈希值根据对象的地址或字符串或数字使用hash算法计算出来的int类型的数值。
-
一般情况下相同对象返回相同哈希码。
toString()方法
-
public String toString(){}
-
返回该对象的字符串表示(表现形式)。
-
可以根据程序需求覆盖该方法,如:展示对象各个属性值。
equal()方法
-
public boolean equals(Object obj){}
-
默认实现为(this == obj),比较两个对象地址是否相同。
-
可进行覆盖,比较两个对象的内容是否相同。
-
equal()方法覆盖步骤
-
比较两个引用是否只想同一个对象。
-
判断obj是否为null。
-
判断两个引用指向的实际对象类型是否一致。
-
强制类型转换。
-
依次比较各个属性值是否相同。
-
finalize()方法
-
当对象被判定为垃圾对象时,有JVM自动调用此方法,用以标志垃圾对象,进入回收队列。
-
垃圾对象:没有有效引用指向此对象时,为垃圾对象。
-
垃圾回收:有GC销毁垃圾对象,释放数据存储空间。
-
自动回收机制:JVM的内存耗尽,一次回收所有垃圾对象。
-
手动回收机制:使用System.gc(); 通知JVM执行垃圾回收。
2. 包装类
-
根据数据类型所对应的引用数据类型。
-
Object可统一所有数据,包装类的默认值时null。
基本数据类型 包装类 byte Byte short Short int Integer long Long float Float double Double boolean Boolean char Character
类型转换与装箱、拆箱
-
//jdk1.5前
//类型转换:装箱,基本类型转成引用类型的过程
//基本类型
int num = 18;
//使用Integer类创建对象
Integer integer1 = new Integer(num1);
Integer integer2 = Integer.valueOf(num1);
//类型转换:拆箱,引用类型转成基本类型
Integer integer3 = new Integer(100);
int num2 = integer3.intValue();
//jdk1.5后,提供自动装箱和拆箱
int num3 = 30;
//自动装箱
Integer integer4 = num3;
//自动拆箱
int num4 = integer4;
-
8种包装类提供不同类型间的转换方式:
-
Number父类中提供的6个共性方法。
byteValue();
doubleValue();
floatValue();
intValue();
longValue();
shortValue(); -
paraseXXX()静态方法。
-
valueOf()静态方法。
-
-
注意:需保证类型兼容,否则抛出NumberFotmatException异常。
整数缓冲区
-
Java预先创建了256个常用的整数包装类型对象(-128~127)。
-
在实际应用当中,对已创建的对象进行复用。
3. String类
-
字符串时常量,创建之后不可改变。
-
字符串字面值存储在字符串池中,可以共享。
-
String s = "Hello"; //产生一个对象,字符串池中存储。
String s new String("Hello") //产生两个对象,堆、池各存储一个。
String的常用方法
-
//返回字符串的长度。
public int length(); -
//根据下标获取字符。
public char charAt(int index); -
//判断当前字符串中是否包含str。
public boolean contains(String str); -
//字符串转换成数组。
public char[] toCharArray(); -
//查找str首次出现的下标。
public int indexOf(String str); -
//查找字符串在当前字符串中最后一次出现的下标索引。
public int lastIndexOf(String str); -
//去掉字符串前后的空格。
public String trim(); -
//小写转大写。
public String toUpperCase(); -
//大写转小写。
public String toLowerCase(); -
//判断字符串是否以str结尾。
public boolean endWith(String str); -
//判断字符串是否以str开头。
public boolean startWith(String str); -
//字符串替换为新字符串。
public String replace(char oldChar, char newChar); -
//根据str拆分。
public Stirng[] split(String str);
String案例演示
-
需求:
-
已知String str = "this is a text";
-
将str中的单词单独获取出来
-
将str中的text替换为practice
-
在text前面插入一个easy
-
将每个单词的首字母改为大写
-
-
-
String str = "this is a text";
//1.将str中的单词单独获取出来
String arr[] = str.split(" ");
for (String s:arr) {
System.out.println(s);
}
//2. 将str中的text替换为practice
String str2 = str.replace("text","practice");
System.out.println(str2);
//3. 在text前面插入一个easy
String str3 = str.replace("text","easy text");
System.out.println(str3);
//4. 将每个单词的首字母改为大写
String str4 = "";
for(int i = 0; i<arr.length; i++){
char first = arr[i].charAt(0);
char upperFirst = Character.toUpperCase(first);
String news = upperFirst + arr[i].substring(1);
if(i == arr.length-1){
str4 += news;
}else{
str4 += news+" ";
}
}
System.out.println(str4);
可变字符串
-
StringBuffer:可变长字符串,JDK1.0提供,运行效率慢,线程安全。
-
StringBuilder:可变长字符串,JDK5.0提供,运行效率快,线程不安全。
-
StringBuffer、StringBuilder与String的区别:
-
比String效率快
-
比String节省内存
-
-
//追加
append();
//添加
insert();
//替换
replace();
//删除
delete();
4. BigDecimal类
-
位置:java.math包中。
-
作用:精确计算浮点数。
-
创建方式:BigDecimal bd = new BigDecimal("1.0");
-
方法:
BigDecimal add(BigDecimal bd); //加
BigDecimal subtract(BigDecimal bd); //减
BIgDecimal mutiply(BigDecimal bd); //乘
BigDecimal divide(BigDecimal bd); //除 -
利用BigDecimal可以进行数值计算:
public class TestBigDecimal {
public static void main(String[] args){
BigDecimal bd1 = new BigDecimal("1.0");
BigDecimal bd2 = new BigDecimal("0.9");
BigDecimal result1 = bd1.add(bd2);
System.out.println("bd1+bd2="+result1);
BigDecimal result2 = bd1.substract(bd2);
System.out.println("bd1-bd2="+result2);
BigDecimal result3 = bd1.multiply(bd2);
System.out.println("bd1*bd2="+result3);
BigDecimal result4 = bd1.divide(bd2);
System.out.println("bd1/bd2="+result4);
}
}-
除法:divide(BigDecimal bd, int scal, RoundingMode mode)
-
参数scal:指定精确到小数点后几位。
-
参数mode:
-
指定小数部分的取舍模式,通常采用四舍五入的模式。
-
取值为BigDecimal.ROUND_HALF_UP。
-
-
5. Date类
-
Date表示特定的瞬间,精确到毫秒。Date类中的大部分方法都已经被Calendar类中的方法取代。
-
时间单位
-
1秒 = 1000毫秒
-
1毫秒 = 1000微秒
-
1微秒 = 1000纳秒
-
6. Calendar类
-
Calendar提供了获取或设置各种日历字段的方法。
-
构造方法
-
protected Calendar(); //由于是修饰符是protected,所以无法直接创建对象。
//创建Calendar对象
Calendar calendar = Calendar.getInstance();
-
-
其他方法
方法名 说明 static Calendar getInstance(); 使用默认时区和区域获取日历 void set(int year, int month, int date, int hourofday, int minute, int second); 设置日历的年、月、日、时、分、秒 int get(int field); 返沪给定日历字段的值。字段比如年、月、日等 void setTime(Date date); 用给定的Date设置此日历的时间。Date-Calendar Date getTime(); 返回一个Date表示此日历的时间。Calendar-Date void add(int field, int amount); 按照日历的规则,给指定字段添加或减少时间量 long getTimeInMillies(); 毫秒为单位返回该日历的时间值
7. SimpleDateFormat
-
SimpleDateFormat是一个以与语言环境有关的方式来格式化和解析日期的具体类。
-
进行格式化(日期--->文本)、解析(文本--->日期)
-
字母 日期或时间 示例 y 年 2019 M 年中月份 08 d 月中天数 10 H 1天中小时数(0-23) 12 m 分钟 30 s 秒 46 S 毫秒 699 -
//创建SimpleDateFormat对象 y年MM月d日
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
8. System类
-
System系统类,主要用于获取系统的属性数据和其他操作,构造方法私有。
-
方法名 说明 static void arraycopy(...) 复制数组 static long currentTimeMillis(); 获取当前系统时间,返回的是毫秒值 static void gc(); 建议JVM启动垃圾回收器回收垃圾 static void exit(int status);
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix