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("程序结束了。。。。");
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南