第五章API常用类
1.什么是API??
API多指的是API文档,对java提供的类和接口中的功能进行说明.
2.基本数据类型封装类
基本数据类型: byte short int long float double char boolean
包装类: Byte Short Integer Long Float Double Character Boolean
作用:包装类封装了一个相应的基本数据类型数值,并为其提供了一系列操作方法。
/Demo就是int这个基本类型的包装类
public class Demo {
int num;
public Demo(int n){//int的构造方法
this.num=n;
}
public void check(){
}
public static void main(String[] args) {
//int a = 10;
Demo d = new Demo(10);
}
}
public static void main(String[] args) {
// int a = 10;
Integer n = new Integer(10);
// 包装的一些关于int类相关信息
System.out.println(n.MAX_VALUE);//int数据类型的最大值:2147483647
System.out.println(n.MIN_VALUE);//int数据类型的最小值:-2147483647
System.out.println(Integer.SIZE);//int数据类型的字节数:32
System.out.println(Integer.BYTES);
Integer m = new Integer("12");//给对象m赋值
System.out.println(m+10);//进行加减乘除运算
System.out.println(m.compareTo(n));//比较大小
System.out.println(n.equals(m));//比较是否相等
System.out.println(Integer.max(10,5));//比较大小
System.out.println(Integer.toBinaryString(3));
System.out.println(Integer.toHexString(17));
System.out.println(Integer.toOctalString(9));
}
}
public static void main(String[] args) {
// int a = 10;
int x = 10;
Integer n = new Integer(x);
int m = n.intValue();//取出对象中包含的int值
// m=n;
String s = "10";
int y = Integer.parseInt(s); //将String类型转为int
String s1 = n.toString(); //将整数 转为 String
Integer in = Integer.valueOf(x);
Integer in1 = Integer.valueOf("10");
}
public static void main(String[] args) {
int x = 10;
/*
基本类型转引用类型
Integer n = new Integer(x);
Integer n1 = Integer.valueOf(x);
*/
Integer n = x; //自动装箱 默认调用valueOf()方法 创建一个Integer对象
//n.intValue()
int y = n;//自动拆箱 将引用类型转为基本类型 默认调用intValue()方法 取出包含的int值
}
public static void main(String[] args) {
/*
自动装箱的问题
== 在比较基本类型时,比较的就是值是否相等
== 在比较引用类型时,比较对象在内存中的地址是否相等
自动装箱调用valueOf()
public static Integer valueOf(int i) {
判断值如果在 -128 到 127之间 会从缓存数组中取出一个对象返回,
多个值相同,返回的是同一个对象
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);//不在-128 到 127之间会创建新的对象返回
}
*/
Integer x = 127;
Integer y = 127;
System.out.println(x==y);//10 true 128 false
Integer m = new Integer(128);
Integer n = new Integer(128); //两个不同的对象比较
System.out.println(m==n);//10 false 128 false
System.out.println(127==127);
Boolean b = true;
boolean b1 = b;
}
3.Object类
Object类是所有java类的父类!!
//在类的声明中没有使用extends关键字指明其父类,默认其父类是Object
public class Car{}
//等价于
public class Car extends Object{}
1.toString方法
toString方法返回类型为String类型
public static void main(String[] args) {
int [] a = {1,2,3,4,5};
//System.out.println(a);//输出:[I@1b6d3586
//数组操作的工具类,包含操作数组的方法
System.out.println(Arrays.toString(a));//输出:1,2,3,4,5
}
2.equals方法
x.equals(y) 当x跟y返回的是同一类对象且内容相同时返回True,否则返回False
public class ObjectEquals {
/*
Object类中equals
public boolean equals(Object obj) {
return (this == obj);
}
*/
public static void main(String[] args) {
Person p1 = new Person();
p1.setName("张三");
p1.setAge(20);
Person p2 = new Person();
p2.setName("张三");
p2.setAge(20);
System.out.println(p1.equals(p2));
String s = new String("abc");
String s1 = new String("abc");
System.out.println(s==s1);//false
//String类重写了equals() 比较的是对象中包含的内容是否相等
System.out.println(s.equals(s1));//true
Integer x = 128; //new Integer(128);
Integer y = 128; //new Integer(128);
System.out.println(x==y);//false
System.out.println(x.equals(y));//true
//几乎API中所有类都重写Object类中equals(),比较的是对象中包含的内容是否相等
}
}
4.Arrays类
用于操作数组工具类,里面定义了常见操作数组的 静态 方法。
1.equals方法
比较两个数组的内容是否相等
public static boolean equals(type[]a,type[]a2)
//参数类型可以是基本数据类型,也可以是引用类型
//两者内容相等,返回True,不等,返回False
2.sort方法
对指定对象数组进行升序排序
public static void main(String[] args) {
int [] a = {5,4,3,2,1};
Arrays.sort(a);//0---length-1 对整个数组进行排序
Arrays.sort(a,0,3);//0 --- 3-1 对指定的区间进行排序
System.out.println(Arrays.toString(a));
}
public static void main(String[] args) {
Student [] students = new Student[5];
Student s1 = new Student(101, "张三1");
Student s2 = new Student(102, "张三2");
Student s3 = new Student(103, "张三3");
Student s4 = new Student(104, "张三4");
Student s5 = new Student(105, "张三5");
students[0] = s2;
students[1] = s5;
students[2] = s1;
students[3] = s4;
students[4] = s3;
//对引用类型数组进行排序时,类实现
Arrays.sort(students);
System.out.println(Arrays.toString(students));
public class Student implements Comparable<Student> {
private int num;
private String name;
public Student(int num, String name) {
this.num = num;
this.name = name;
}
//用来做比较, 提供的一个自定义排序规则的方法
//compareTo()方法会在sort()的底层中调用
@Override
public int compareTo(Student o) {
//return this.num-o.num ;// 按照学号进行排序
return this.name.compareTo(o.name);//按照姓名进行排序
}
@Override
public String toString() {
return "Student{" +
"num=" + num +
", name='" + name + '\'' +
'}';
}
}
3.binarySearch方法(二分算法)
public class ArraysBinarySearch {
public static void main(String[] args) {
//二分查询,查找指定值在数组中位置
//前提:数组要有序
int [] a = {2,1,3,7,6,5,8};
Arrays.sort(a);
// 1,2,3,5,6,7,8
System.out.println(Arrays.binarySearch(a, 4));
System.out.println(Arrays.binarySearch(a, 0,5,6));
System.out.println(mybinarySearch(a,9));
}
5.String类
String表示字符串,
所有字符串都是String类的对象
字符串的值不能改变(一旦确定字符串的值.就不能改变),因为底层用到了final修饰的char数组
private final char value[];
public static void main(String[] args) {
String s = "abc";
String s1 = new String("abc");
s1 = s1+ "efg";
s1+="hijk";
System.out.println(s1);
1.String的两种创建方式
public static void main(String[] args) {
/*
String对象的两种创建方式
1.
String s = "abc";
2.
String s1 = new String("abc");
*/
String s1 = "abc";//首先会在字符串常量池中查找,如果没有,就在常量池中创建
String s2 = "abc";//如果常量池已经存在,就不需要创建,直接返回地址即可
System.out.println(s1==s2);//true
System.out.println(s1.equals(s2));//true
String s3 = new String("abc");
String s4 = new String("abc"); //都会在堆中创建一个对象
System.out.println(s3==s4);//false
System.out.println(s3.equals(s4));//true
}
2.String的构造方法
public static void main(String[] args) {
/*
String 构造方法
*/
String s = new String("abC");//创建一个"" this.value = "".value;
String s1 = new String("abc");// this.value = original.value;
/*
判断
boolean equals(Object obj)
boolean equalsIgnoreCase(String str)
boolean contains(String str)
boolean isEmpty() boolean
startsWith(String prefix)
boolean endsWith(String suffix)
*/
System.out.println(s.equals(s1));//判断两个对象中的内容是否相等
System.out.println(s.equalsIgnoreCase(s1));//判断两个对象中的内容是否相等 忽略大小写
String s2 = "abcdefg";
System.out.println(s2.contains("ab"));//判断是否包含指定字符串
System.out.println(s2.isEmpty()); //判断是否为空("") 为空返回true
System.out.println(s2.startsWith("ab"));//判断是否以指定的字符串开头
System.out.println(s2.endsWith("ab"));//判断是否以指定的字符串结尾
System.out.println("a".compareTo("c"));//比较两个字符串的位置
/**/
}
public static void main(String[] args) {
/*
获取功能 int length()
char charAt(int index)
int indexOf(String str)
int indexOf(String str,int fromIndex)
String substring(int start)
String substring(int start,int end)
*/
String s = "abcdedfg";
// 01234567
System.out.println(s.length());
char c = s.charAt(3);//返回指定索引的字符
System.out.println(c);
int index = s.indexOf("d");//获取指定字符串首次出现的位置 从前向后找
System.out.println(index);
int index1 = s.indexOf("d",4);//从指定的位置开始,获取指定字符串首次出现的位置
System.out.println(index1);
int index2 = s.lastIndexOf("d");
System.out.println(index2);
String s3 = "abcdedfg";
// 01234567
String s4 = s3.substring(2); //从s3 截取指定区间的字符串 从指定位置开始 到 结束
System.out.println(s3);//abcdedfg
System.out.println(s4);//cdedfg
String s5 = s3.substring(2,6);//k指定区间截取 开始位置 -- 结束位置(不包含结束)
System.out.println(s5);
}
public static void main(String[] args) {
/*
String toLowerCase()
String toUpperCase()
String concat(String str)
Stirng[] split(分割符);
*/
String s = "abcdEFG";
System.out.println(s.toLowerCase());//转小写
System.out.println(s.toUpperCase());//转大写
String s1 = s.concat("HIJK"); //两个字符串拼接 并返回新的字符串对象.
System.out.println(s1);
String s2 = "ab:cd:E:FGH";
String[] array = s2.split(":");//用指定符号 来分割字符串为数组
System.out.println(Arrays.toString(array));
}