Java中工具类

58 阅读4分钟

Object类

  1. Obejct是根基类,是所有类的父类

Object中的方法

equals()方法

==和equals的区别

  1. ==代表比较两个值是否相同。如果基本类型则表示值相等,如果是引用类型则表示地址值是否相等,如果地址值相等则为同一个对象
  2. equals方法默认就是比较两个对象的地址值,equals可以由相应类来重写,从而可以通过属性值比较两个对象是否相等

hashCode()方法

返回的是对象的散列码,Object中的equals是native修饰的,代表其是用java以外的代码实现的,是通过对象的地址值计算的。可以通过重写hashCode方法来实现属性值相同的时候生成同样的散列码

toString方法

默认是返回对象的包名+类名+hashCode,可以通过类中重写来输出对象的属性值

包装类

[1]为什么使用包装类
A、实现基本数据类型和引用数据类型之间的转化
B、以后会学习集合,集合在内容存储的时候只能存储引用数据类型,不能存储基本数据类型
八个类中除了Character和Boolean以外,其他都是数字型,都是java.lang.Number的子类

和基本数据类型对应的包装类:

基本类型包装类
byteByte
booleanBoolean
shortShort
charCharacter
intInteger
longLong
floatFloat
doubleDouble

常用的属性和方法:

  1. Integer.MAX_VALUE
  2. Integer.MIN_VALUE
  3. Integer.TYPE
  4. Integer.parseInt(st);//把一个字符串类型的数据转成int类型
  5. toString();//把一个Integer类型转成字符串
  6. Integer.valueOf(b);//这个方法也是可以把string 字符串转成Integer类型数据

自动装箱机制

System.out.println("手动装箱和拆箱");
int a = 10;
//手动装箱把基本数据类型转为包装类
Integer b = new Integer(a);
int c = b.intValue();//手动拆箱:是吧包装类转为基本数据类型

System.out.println("JDK1.5自动装箱和自动拆箱");
int a1 = 123;
Integer a2 = a1;//自动装箱 底层默认调用valueOf(),valueOf()底层还是new Integer();
int a3 =a2;//自动拆箱 底层默认调用intValue();
System.out.println("隐藏的默认拆箱和装箱的工作");
Integer b2 = new Integer(123);

b2++;//进行算数运算的时候会把包装类自动转为基本数据类型

int c2 = 123;
Integer c3 = 123;
//把基本类型的数据在这里面自动的转成包装类,equals方法只能比较对象
System.out.println(c3.equals(c2));
c3.toString();

//在底层缓存数组中直接取值并返回,127~-128 ,地址相同,默认执行valueOf方法
//底层缓存区适用于整数类型,而浮点型不适用
Integer a4 = 20;
Integer a5 = 20;
System.out.println(a4==a5);//true
//底层是新建了new Integer()对象,地址不同
Integer c4 = 200;
Integer c5 = 200;
System.out.println(c4==c5);//false

String类

String类方法的使用

System.out.println("String类定义");
String str = "bjsxt";
String str2 = new String("bjsxt");
String str3 = "";
String str4 = null;
System.out.println("------------最简单方法-------------");
String str5 = "bjsxt";
System.out.println(str5.length());//返回值为字符串长度
System.out.println(str5.isEmpty());//false 判断字符串是否为空
System.out.println(str5.startsWith("b"));//判断字符串是否以b开头
System.out.println(str5.endsWith("t"));//判断字符串是否以t结尾
System.out.println(str5.toLowerCase());//将字符串中的所有字母转为小写
System.out.println(str5.toUpperCase());//将字符串中的所有字母转为大写
System.out.println(str5.contains("jsx"));//判断字符串中是否包含指定内容


System.out.println("----------常用的方法------------");
String str7 = "bjsxt";
System.out.println(str7.charAt(2));//返回对应下标的字符,下标从零开始

System.out.println(str7.indexOf("bs"));//根据指定字符串返回对应下标,如果找不到就返回-1
System.out.println(str7.indexOf(98));//根据字符对应ASCII返回下标
System.out.println(str7.indexOf("b"));//返回第一个元素出现的位置
System.out.println(str7.indexOf("b",3));//返回从后面的下标开始的第一个对应元素的下标
System.out.println(str7.lastIndexOf("b"));//从后往前寻找,返回寻找到的第一个元素的下标

System.out.println(str7.substring(2));//从指定下标开始到字符串结束
System.out.println(str7.substring(2,4));//从指定范围获得字符串[2,4]

//案例:给定一个字符串为a.b.c.g.jpg,输出jpg
String str8 = "a.b.c.g.jpg";
//获得最后一个点的下标
int i = str8.lastIndexOf(".");
System.out.println(str8.substring(i+1));
String str = "bjsxt";

//String字符串可以转成byte数组 作用:我们以后会使用这个处理中文乱码问题
byte[] bytes = str.getBytes();


//字符串的分割
String str2 = "b-j-s-x-t";
String[] split = str2.split("-");//按照指定字符把字符串进行分割 分割结束之后返回值是一个数组
System.out.println(Arrays.toString(split));
//字符串替换
String str3 = "bjsxtbj";
String s = str3.replaceFirst("bj","**");
String s2 = str3.replaceAll("bj","**");
System.out.println(s);
System.out.println(s2);

//进行某些字符替换
Scanner sc = new Scanner(System.in);
System.out.println("请输入您要说的话");
String next = sc.next();

boolean f = next.contains("2b");
if(f){
    String replacestr =next.replaceAll("2b","小可爱");
    System.out.println(replacestr);
}else{
    System.out.println(next);
}
String str = " b  j s x   t ";
System.out.println("----->"+str+"<-----");
System.out.println("----->"+str.trim()+"<-----");//该方法能去掉左右两边的空格

//以后结合treeMap使用
String str2 = "bjsxt";
String str3 = "bj";
System.out.println(str2.compareTo(str3));
//通过比较挨个比较两个字符串的元素的ACSII码值,如果一个比另一个大那么就输出,前面都相等哪个元素多哪个大,如果str2大于str3结果为1,相等则为0,小于则输出一个负数
System.out.println(str2.compareToIgnoreCase(str3));//这种比较是不区分大小写的