Object
1.概念
Object是所有类的父类
Object类中的方法都可以被子类使用
2.常用方法
1.getClass:返回对象的字节码文件对象
2.hashCode:返回对象的哈希码值(对象的内存地址)
3…equals:比较两个对象是否相等
4. toString: getClass().getName() + “@” + Integer.toHexString(hashCode());
类的全路径名 + @ + 对象的哈希码值的十六进制
如果我们想按照自己的想法输出对象的信息,我们就需要重写toString方法
5.getClass():获取类名
6.equals(Object obj):判断两个内存地址是否一样
示例代码
public static void main(String[] args){
Person person = new Person();
System.out.println(person);
System.out.println(person.hashCode());
System.out.println("hello word");
}
equals
底层的代码就是
equals: 只能比较引用数据类型
可以比较基本数据类型和引用数据类型,比较基本数据类型的时候比较的是值,比较引用数据类型的时候比较的是地址值
String
1.含义
被final修饰的类,不能被继承
字符串存在于常量池中。 如果new String() 会在堆内存中开辟空间,如果是直接赋值的话,会在常量池中开辟空间
示例代码
ublic static void main(String[] args) {
StringBuffer sb = new StringBuffer("小马");
//在后续增加 append("内容")
sb.append("是个大帅哥");
//中间插入 insert(位数,“内容”)
sb.insert(2, "帅哥");
System.out.println(sb);
//删除某位的字 deleteCharAt(位数)
sb.deleteCharAt(2);
//删除几位到几位的字 delete(开始位数,最后位数)
sb.delete(5,6);
//替换某位到某位的字 replace(开始位数,结束位数,"要替换的字")
sb.replace(5, 8, "大渣男");
System.out.println(sb);
//只显示某位到某位的字 substring(开始位数,结束位数)
String subshtring = sb.substring(5, 8);
System.out.println(subshtring);
//倒序显示 reverse()
sb.reverse();
System.out.println(sb);
基本数据类型
基本数据类型: 是没有方法的,但是可以通过包装类来实现方法啊的调用
* 写法上只有
* int Integer
* char Character
* 剩下的都是首字母大写
*
* 装箱:穿裤衩(变强) 基本数据类型—>包装类
* 拆箱:脱裤衩(变弱) 包装类—>基本数据类型
示例代码
ublic static void main(String[] args) {
//同类型包装
int a = 1;
Integer b = a ;
int c = b;
int i = b.intValue();
//字符串必须是数字,否则报错
Integer r = Integer.valueOf("123");
System.out.println(r);
int i1 = Integer.parseInt("12");
//判断输入是否为大写
boolean a1 = Character.isUpperCase('A');
System.out.println(a1);
//判断输入是否为小写
boolean a2 = Character.isLowerCase('a');
System.out.println(a2);
boolean a3 = Character.isDefined('A');
System.out.println(a3);
char a4 = Character.toLowerCase('A');
System.out.println(a4);
Demo01 demo01 = new Demo01();
int aa = demo01.toCase("123");
System.out.println(aa);
}
Math工具包
示例代码
public static void main(String[] args) {
int a = -100;
System.out.println(a);
//ceil 向上取证
double b = 3.56;
double ceil = Math.ceil(b);
System.out.println(ceil);
//floor 向下取整
double floor = Math.floor(b);
System.out.println(floor);
//取最大值 Math.max
int max = Math.max(100, 200);
System.out.println(max);
//取最小值 Math.pow
double pow = Math.pow(2, 10);
System.out.println(pow);
long round = Math.round(b);
System.out.println(round);
double random = Math.random();
int aa = (int) (random * 100);
System.out.println(aa);
System.out.println(Math.sqrt(9));
}
时间用法
date表示日期
1 秒 = 1000毫秒
SimpleDateFormat:
format Date -> String
parse String -> Date
示例代码
public static void main(String[] args) {
Date date = new Date();
System.out.println(date);
//获取当前时间的毫秒值
long time = date.getTime();
long l = System.currentTimeMillis();
System.out.println(time);
System.out.println(l);
时间格式化
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss:SSS");
String format = sdf.format(date);
System.out.println(format);
try {
Date parse = sdf.parse("2025年07月15日 15:20:22:452");
System.out.println(parse);
} catch (ParseException e) {
throw new RuntimeException(e);
}
日历类
示例代码
ublic static void main(String[] args) {
Calendar instance = Calendar.getInstance();
System.out.println(instance);
instance.add(Calendar.MONTH,2);
instance.add(Calendar.DAY_OF_MONTH,-2);
// calender转换成date
Date time = instance.getTime();
System.out.println(time);
// date转换成calendar
instance.setTime(new Date());
}