面向对象学完之后,我们开始学习了Java给我们提供的常用类API
所谓API就是Java系统中给我们提供的类和方法,防止我们重复造轮子
1.基本数据类型包装类
1.基本数据类型~~>包装类
byte~~>Byte short~~>Short int~~>Integer long~~>Long
float~~>Float double~~>Double char~~>Character
boolean~~>Boolean
2.基本数据类型与包装类之间的转换
装箱:将基本数据类型装换成对应的包装类
- 通过对应包装类的构造器
- 通过对应包装类的静态方法ValueOf()
Integer a1=Integer.valueOf(128);//包装,将基本数据类型变为包装数据类型
拆箱:将包装类转换成对应的基本数据类型
- 通过对应包装类***Value()方法。***表示基本数据类型
3.基本数据类型与包装数据类型的区别
- 包装类是对象,拥有方法和字段,对象的调用都是通过引用对象的地址,基本类型则不然。
- 包装类型都是引用的传递,基本数据类型是值得传递
- 声明的方式不一样,基本类型不需要new关键字,而包装类型需要new在堆内存中进行new来分配内存空间
- 存储位置不同,基本数据类型直接将值保留在栈中,包装类型是把对象放在堆中,然后通过对象的引用来调它们
- 初始值不同,基本数据类型初始值为0,包装类型初始值为null(Boolean初始值为false)
- 使用方法不同,基本数据类型直接赋值使用就好,包装类型是集合如collection、Map时会使用
2.Object类
Object类是Java.long包下的核心类,Object类是所有类的父类,如果一个类没有继承父类的话,那么他就默认继承Object;
1.Object类的常用方法
方法名称 | 类型 | 描述 |
toString() | 普通 | 取得对象信息 |
equals() | 普通 | 对象内容比较 |
==和equals的区别:
- 基本数据类型比较用==,比较的是值是否相同
- 引用数据类型比较,调用equals()方法进行比较,比较的是对象内容是否相同
int a=10;
int b=20;
System.out.println(a==b);//false 基本类型比较时比较的是数值的大小
Integer c=new Integer(20);
Integer d=new Integer(20);
System.out.println(c==d);//false 引用类型变量比较是对象的地址
Integer e=10;
Integer f=10;
System.out.println(e==f);//ture 在-128~127中取缓存数组中,取的是同一份
Integer a1=Integer.valueOf(128);//包装,将基本数据类型变为包装数据类型
Integer b1=128;
System.out.println(a1==b1);//false 在其余中是重新new了俩个新对象
System.out.println(a1.equals(b1));//true 只比较的是数值的大小
3.Arrays类:
返回类型 | 方法 | 说明 |
String | toString(arrays) | 将数组array转换成字符串 |
void | sort(array) | 对数组进行升序排序 |
void | fill(arr,val) | 将数组arr全部元素赋值为val |
boolean | equals(arr1,arr2) | 判断两个数组是否相等 |
与arrl类型相同 | copyOf(arr,length) | 将数组ar复制成一个长度为lengthde新数组 |
int | binarySearch(arr,val) | 查询元素val在arr中的下标值 |
equalsIgnoreCase()忽略大小写进行判断
contains()判断是否包含指定字符串
isEmpty()判断是否为空串
startwith()判断是否以***开头
endwith()判断是否以***结尾
charAt()给索引找元素
indexOf()给元素找索引
lastIndexof()给***字符找最后一个索引
substring()从指定的位置截取新的字符串到结尾
equalsIgnoreCase():
String a="abcdefg";
String b="Abcdefg";
System.out.println(a.equals(b));//结果为false
String a="abcdefg";
String b="Abcdefg";
System.out.println(a.equalsIgnoreCase(b));//结果为ture
contains():
String a="abcdefg";
System.out.println(a.contains("abc"));//结果为ture
System.out.println(a.contains("abg"));//结果为false
注意:判断字符串必须是连续的字符串
isEmpty():
String a="abcdefg";
System.out.println(a.isEmpty());//结果为false
String a="";
System.out.println(a.isEmpty());//结果为ture
startwith():
String a="abcdefg";
System.out.println(a.startsWith("abc"));//结果为ture
endwith():
String a="abcdefg";
System.out.println(a.endsWith("fg"));//结果为ture
charAt():
String a="abcdefg";
System.out.println(a.charAt(2));//输出为c
indexOf():
String a="abcdefg";
int b=a.charAt(0);
System.out.println(a.indexOf("d"));//输出为3
lastIndexof():.
String a="abcdaefag";
System.out.println(a.lastIndexOf("a"));//输出为7
substring():
String a="abcdaefag";
String b=a.substring(2);
System.out.println(b);//输出为cdaefag
substring():
String a="abcdaefag";
String b=a.substring(2,4);
System.out.println(b);//输出为cd