java笔记整理:常用类
目录
常用类
Object类
- 超类、基类,所有类的直接或间接父类,位于继承树的最顶层
- 任何类,如果没有书写extends显示继承某个类,都默认直接继承Object类,否则为间接继承
- Object类中所定义的方法,是所有对象都具备的方法
- Object类型可以存储任何对象
- 作为参数,可接受任何对象
- 作为返回值,可接受任何对象
getClass()方法
- public final Class<?> getClass(){}
- 返回引用中存储的实际对象类型
- 应用:通常用于判断两个引用中实际存储对象类型是否一致
package com.getClass;
public class student {
private String name;
private int age;
public student(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;
}
}
package com.getClass;
public class TestStudent {
public static void main(String[] args) {
student s1 = new student("aaa",20);
student s2 = new student("bbb",18);
//判断s1和s2是不是同一类型
Class c1 = s1.getClass();
Class c2 = s2.getClass();
if(c1==c2){
System.out.println("s1和s2是同一种类型");
}else{
System.out.println("s1和s2不是同一种类型");
}
}
}
hashCode()方法
- public int hashCode(){}
- 返回该对象的哈希码值
- 哈希值根据对象的地址或字符串或数字使用hash算法计算出来的int类型的数值
- 一般情况下相同对象返回相同的哈希码
public class TestStudent {
public static void main(String[] args) {
student s1 = new student("aaa",20);
student s2 = new student("bbb",18);
//hashCode方法
System.out.println(s1.hashCode());
System.out.println(s2.hashCode());
student s3 = s1;
System.out.println(s3.hashCode());
}
}
toString()方法
- public String toString(){}
- 返回该对象的字符串表示(表现形式)
- 可以根据程序需求覆盖该方法,如:展示对象各个属性值
public class TestStudent {
public static void main(String[] args) {
student s1 = new student("aaa",20);
student s2 = new student("bbb",18);
//toString方法
System.out.println(s1.toString());
System.out.println(s2.toString());
}
}
equals()方法
- public boolean equals(Object obj){}
- 默认实现为(this==obj),比较两个对象地址是否相同
- 可进行覆盖,比较两个对象的内容是否相同
public class TestStudent {
public static void main(String[] args) {
student s1 = new student("aaa",20);
student s2 = new student("bbb",18);
//equals方法
System.out.println(s1.equals(s2));
}
}
- equals()方法覆盖步骤
- 比较两个引用是否指向同一个对象
- 判断obj是否为null
- 判断两个引用指向的实际对象类型是否一致
- 强制类型转换
- 依次比较各个属性值是否相同
finalize()方法
- 当对象被判定为垃圾对象时,由JVM自动调用此方法,用以标记垃圾对象,进入回收队列
- 垃圾对象:没有有效引用指向此对象时,为垃圾对象
- 垃圾回收:由GC销毁垃圾对象,释放数据存储空间
- 自动回收机制:JVM的内存耗尽,一次性回收所有垃圾对象
- 手动回收机制:使用System.gc();通知JVM执行垃圾回收
包装类
什么是包装类
- 基本数据类型所对应的引用数据类型
- Object可统一所有数据,包装类的默认值是null

类型转换与装箱、拆箱
- 装箱:将基本类型转换为引用类型
- 拆箱:将引用类型转换为基本类型
- 8种包装类提供不同类型之间的转换方式:
- Number父类中提供的6个共性方法
package BaoZhuangLei;
public class Demo01 {
public static void main(String[] args) {
int num = 10;
//jdk1.5之前
//基本转换:装箱,基本类型转换为引用类型的过程
//基本类型
int num1 = 18;
//使用Integer类创建对象
Integer integer1 = new Integer(num1);
Integer integer2 = Integer.valueOf(num);
System.out.println(integer1);
System.out.println(integer2);
//类型转换:拆箱,引用类型转换为基本类型
Integer integer3 = new Integer(100);
int num2 = integer3.intValue();
System.out.println(num2);
//jdk1.5之后,提供自动装箱和拆箱
int age = 22;
//自动装箱
Integer integer4 = age;
//自动拆箱
int num3 = integer4;
System.out.println(integer4);
System.out.println(num3);
}
}
- parseXXX()静态方法
package BaoZhuangLei;
public class Demo02 {
public static void main(String[] args) {
//基本类型和字符串类型之间的转换
//1.基本类型转换为字符串
int n1 = 100;
//1.1使用+号
String s1 = n1+"";
//1.2使用Integer中的toString()方法
String s2 = Integer.toString(n1);
System.out.println(s1);
System.out.println(s2);
//toString重载方法
String s3 = Integer.toString(100,16);
System.out.println(s3);
//字符串转换为基本类型
String s4 = "150";
//使用Integer.parseXXX();
int n2 = Integer.parseInt(s4);
System.out.println(n2);
//bool类型字符串形式转成基本类型,“true”----->true 非true---->false
String s5 = "true";
boolean b1 = Boolean.parseBoolean(s5);
System.out.println(b1);
}
}
注意:需保证类型兼容,否则抛出NumberFormatException异常
整数缓冲区
- java预先创建了256个常用的整数包装类型对象
- 在实际应用中,对已创建的对象进行复用
String类
- 字符串是常量,创建后不可改变
- 字符串字面值存储在字符池中,可以共享
- 创建字符串两种方式:
- String s = “”;//产生一个对象,字符串池中存储
- String s = new String(“”);//产生两个对象,堆、池各存储一个
- 常用方法
- length():返回字符串长度
- charAt(int index):根据下标获取字符串
- contains(String str):判断当前字符串中是否包含str
- toCharArray():将字符串转换成数组
- indexOf()(String str):查找str首次出现的下标,存在,则返回改下标;不存在,则返回-1
- lastIndexOf(String str):查找字符串在当前字符串中最后一次出现的下标索引
- trim():去掉字符串前后的空格
- toUpperCase():将小写转成大写(toLowerCase();大写转小写)
- endWith(String str):判断字符串是否以str结尾(startWith是否以(str)为开头)
- replace(char oldChar,char newChar):将旧字符串替换为新的字符串
- split(String str):根据str做拆分
- substring():拼接字符串
package BaoZhuangLei;
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Locale;
public class Demo03 {
public static void main(String[] args) {
String name = "张三";
String name2 = "李四";
//字符串方法的使用
//1、length();返回字符串的长度
int a = name.length();
System.out.println(a);
//2.charAt(int index);返回某个位置的字符
char b = name.charAt(name.length()-1);
System.out.println(b);
//3.contains(String str);判断是否包含某个子字符串
System.out.println(name.contains("张"));
String content = "java是世界上最好的语言";
//4.public char[] toCharArray():将字符串转换成数组
System.out.println(Arrays.toString(content.toCharArray()));
//5.public int indexOf()(String str):查找str首次出现的下标,存在,则返回改下标;不存在,则返回-1
System.out.println(content.indexOf("最好的"));
//6. public int lastIndexOf(String str):查找字符串在当前字符串中最后一次出现的下标索引
System.out.println(content.lastIndexOf("最好的"));
String sc = "劳资蜀道山Lzsds ";
//7.trim():去掉字符串前后的空格
System.out.println(sc.trim());
//8.toUpperCase():将小写转成大写
System.out.println(sc.toUpperCase());
//9.endWith(String str):判断字符串是否以str结尾
System.out.println(sc.endsWith(" "));
//10.replace(char oldChar,char newChar):将旧字符串替换为新的字符串
System.out.println(sc.replace(" ","LZSDS"));
//11.split(String str):根据str做拆分
String say = "java is the bast programing,language";
String[] arr = say.split("[ ,]+");
System.out.println(arr.length);
for(String string:arr){
System.out.println(string);
}
//补充两个方法equals、compare();比较大小
String s1 = "Hello";
String s2 = "Hello";
System.out.println(s1.equals(s2));
String s3 = "ABC";
String s4 = "DEF";
System.out.println(s3.compareTo(s4));
}
}
举例展示
- 需求:
- 已知String str = “this is a text”;
- 1.将str中的单词单独获取出来
- 2.将str中的text替换为practice
- 3.在text前插入一个easy
- 4.将每个单词的首字母该为大写
package BaoZhuangLei;
/*
- 需求:
- 已知String str = "this is a text";
- 1.将str中的单词单独获取出来
- 2.将str中的text替换为practice
- 3.在text前插入一个easy
- 4.将每个单词的首字母该为大写
*/
public class Demo04 {
public static void main(String[] args) {
String str = "this is a text";
//1.将str中的单词单独获取出来
String[] s1 = str.split(" ");
for(String s:s1){
System.out.println(s);
}
//2.将str中的text替换为practice
String s2 = str.replace("text","practice");
System.out.println(s2);
//3.在text前插入一个easy
String s3 = str.replace("a text","a easy text");
System.out.println(s3);
//4.将每个单词的首字母该为大写
for(int i=0;i<s1.length;i++){
char first = s1[i].charAt(0);
//char类型的包装类是Character
char upperfirst = Character.toUpperCase(first);
//substring()方法拼接字符串
String news = upperfirst+s1[i].substring(1);
System.out.println(news);
}
}
}
可变字符串
- StringBuffer:可变长字符串,JDK1.0提供,运行效率慢、线程安全
- StringBuilder:可变长字符串,JDK5.0提供,运行效率块、线程不安全
- 常用方法:
- 1.append();追加
- 2.insert();添加
- 3.repace();替换(可指定位置)
- 4.delete();删除
package BaoZhuangLei;
/*
*StringBUffer和StringBuilder的使用
* 两者使用的方法一样
* 区别:1、效率比String高
* 2、比String节省内存
*/
public class Demo05 {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer();
//1.append();追加
sb.append("java世界第一");
System.out.println(sb.toString());
//2.insert();添加
sb.insert(0,"我觉得");
System.out.println(sb.toString());
//3.repace();替换(可指定位置)
sb.replace(0,5,"Hello");
System.out.println(sb.toString());
//4.delete();删除
sb.delete(0,5);
System.out.println(sb.toString());
//清空
sb.delete(0,sb.length());
System.out.println(sb.length());
}
}
BigDecimal
- 很多实际应用中需要精确运算,而double是近似值存储,不在符合要求,需要借助BigDecimal
- 位置:java.math包中
- 作用:精确计算浮点数
- 创建方式:BigDecimal bd=new BigDecimal(“1.0”);
- 方法:
- 加法add
- 减法subtract
- 乘法multiply
- 除法divide
- 注:进行除法运算时,如果不能准确地计算出结果是需要指定保留的位数和取舍方式
- 除法:divide(BigDecimal bd,int scal,RoundingMode mode)
- 参数scal:指定精确到小数后几位
- 参数mode:指定小数部分的取舍模式,通常采用四舍五入的模式
- 取值为BigDecimal。ROUND_HALF_UP
package BaoZhuangLei;
import java.math.BigDecimal;
public class Demo06 {
public static void main(String[] args) {
//BigDecimal,大的浮点数精确计算
BigDecimal bd1 = new BigDecimal("1.0");
BigDecimal bd2 = new BigDecimal("0.9");
//减法subtract
BigDecimal r1 = bd1.subtract(bd2);
System.out.println(r1);
//加法add
BigDecimal r2 = bd1.add(bd2);
System.out.println(r2);
//乘法multiply
BigDecimal r3 = bd1.multiply(bd2);
System.out.println(r3);
//除法divide
BigDecimal r4 = new BigDecimal("1.4")
.subtract(new BigDecimal("0.5"))
.divide(new BigDecimal("0.9"));
System.out.println(r4);
}
}
Date
- Date表示特定的瞬间,精确到毫秒。Date类中的大部分方法都已经被Calendar类中的方法所取代
- 时间单位
- 1秒=1000毫秒
- 1毫秒 = 1000微秒
- 1微秒 = 1000纳秒
Calender
- Calender提供了获取或设置各种日历字段的方法
- 构造方法
- protected Calender():由于修饰符是protected,所以无法直接创建该对象
- 其他方法

SimpleDateFormat
- SimpleDateFormat是一个以与语言环境有关的方式来格式化和解析日期的具体类
- 进行格式化(日期 -> 文本)、解析(文本 -> 日期)
- 常用的时间模式字母

package BaoZhuangLei;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Demo08 {
public static void main(String[] args) throws ParseException {
//1.创建一个SimpleDateFormat对象 y 年 M 月
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
//创建一个date
Date date1 = new Date();
//格式化date(把日期转换为字符串)
String str =sdf.format(date1);
System.out.println(str);
//解析(把字符串转换为日期)
Date date2 = sdf.parse("2023/01/02");
System.out.println(date1);
}
}
System类
- System系统类,主要用于获取系统的属性数据和其他操作,构造方法私有
- 常用方法:

package BaoZhuangLei;
public class Demo09 {
public static void main(String[] args) {
//arraycopy:数组的复制
//src:源数组
//srcPos:从哪个位置开始复制
//dest:目标数组的位置
//destPos:目标数组的位置
//length:复制的长度
int[] arr = {20,18,15,8,35,26,45,1};
int[] dest = new int[8];
System.arraycopy(arr,0,dest,0,8);
for (int i=0;i<dest.length;i++){
System.out.print(dest[i]+",");
}
}
}


被折叠的 条评论
为什么被折叠?



