JAVA API常用类
JAVA API概述
API(Aplication Programming Interface):应用程序编程接口
是对java预先定义的类或接口功能和函数功能的说明文档,目的是提供
给开发人员进行使用帮助说明.
Object类
Object类是所有java类的祖先,每个类都使用Object类作为超类(基类)。所有对象(包括数组)都继承实现这个类的方法。
默认继承Object类,等价于使用extends关键字继承Object
eg:public class 类名 等价于 public class 类名 extends Object
Object类中的方法:
toString
()其返回值是
String
类型,描述当前对象的有关信息。
输出对象时可以重写toString方法,如果该类中没有重写toString(),那么默认调用Object类中的toString(),默认输出对象hashCode值.
用static修饰的方法不能被重写
equals()
判断对象是否相等,默认使用==比较,比较的是地址。
JDK提供的一些类,如String,Date等,重写了Object的equals方 法,调用这些类的equals方法, x.equals (y) ,当x和y所引用的对象 是同一类对象且属性内容相等返回 true 否则返回 false
Arrays类
java.util.Arrays类用于操作数组工具类,里面定义了常见操作数组的 静态方法
equals 方法:
比较两个数组对象中元素是否相等.
而数组对象中的equals用来判断与另一个数组对象是否相等
sort 方法:
可以作用于整个数组
也可以作用于数组指定范围内的元素
public static void sort(type[] a, int fromIndex(包括), int
toIndex(不包括))
自定义对象排序:
自定义类实现Comparable接口
并重写compareTo方法
binarySearch -使用二分搜索算法搜索指定数组
public static int binarySearch(type[] a, type key)
public static int binarySearch(long[] a,int fromIndex,int
toIndex,long key)
●
参数:
a - 要搜索的数组。
key - 要搜索的值。
fromIndex - 要排序的第一个元素的索引(包括)。
toIndex - 要排序的最后一个元素的索引(不包括)。
●
如果key在数组中,则返回搜索值的索引;否则返回负数,表示不存在
copy Of方法:将数组元素赋值到新数组里边,并返回新数组
fill方法:将指定的int值分配给int类型数组的每一个元素
toString方法:以字符串形式输出数组元素
package com.ffyc.api.array;
import java.util.Arrays;
public class Array {
public static void main(String[] args) {
int [] a={1,2,3,4,5,6};
System.out.println(Arrays.toString(a));//以字符串输出
int[] b=new int[6];
System.out.println(Arrays.equals(a,b));//比较两个数组中元素是否相同,返回值boolean
Arrays.fill(b,0);//将int型b数组每一个元素赋值为0
int []c=Arrays.copyOf(a,6);
System.out.println(Arrays.equals(a,c));
Arrays.fill(b,66);
System.out.println(Arrays.toString(b));
}
}
package com.ffyc.api.array;
import java.util.Arrays;
public class ArrayDemo {
public static void main(String[] args) {
int []arr={1,2,1,2,1,2,12,1,2};
int[] a= Arrays.copyOf(arr,6);
Arrays.sort(arr);
Arrays.sort(a,1,5);
System.out.println(Arrays.toString(arr));
System.out.println(Arrays.toString(a));
int index=Arrays.binarySearch(arr,2);
System.out.println(index);
}
}
基本数据类型包装类
java是面向对象的高级语言,基本数据类型不是"面向对象的",所以为了更方便使用基本数据类型,将其包装为类,变为“面向对象”。
包装类(如:Integer,Double等)这些类封装了一个相应的基本数据 类型数值,并为其提供了一系列操作方法。
在设计类时为每个基本数据类型设计了一个对应的类进表示,这样八个 和基本数据类型对应的类统称为包装类。这些类封装了一个相应的基本数据类型数值,并为其提供了一系列操作方法。
![](https://i-blog.csdnimg.cn/blog_migrate/6387dd80cacdbb15f2376efce4314b8c.png)
这些第二篇详细说明:
String类/String Buffer类/String Builder类
Math类/Random类
Date类/Calendar类/ SimpleDateFormat类
BigInteger类/BigDicimal类
基本数据类型封装类:
封装类是包含属性和方法的对象,而包装类是针对基本数据类型设计的类,
提供了额外的属性和方法。
package com.ffyc.api.array;
public class TestInteger {
public static void main(String[] args) {
Integer integer=new Integer(10);//创建基本类型包装类的对象
Integer integer1=new Integer(20);
//一类是静态方法
System.out.println(Integer.MAX_VALUE);
System.out.println(Integer.MIN_VALUE);//类名调用静态方法,
System.out.println(Integer.SIZE);
System.out.println(Integer.BYTES);
//一类是非静态
System.out.println(integer);
System.out.println(integer.toString());
System.out.println(integer1.byteValue());
System.out.println(Integer.toBinaryString(6));//将参数转为二进制输出
//转换的方法 包装类型 转 基本类型
MyInt m=new MyInt(6);
int aa=Integer.parseInt("20");
//把基本类型 转为 包装类型
Integer bb=new Integer(aa);
Integer cc=Integer.valueOf(aa);
}
}
自动装箱、拆箱:
package com.ffyc.api.day6;
public class Demo1 {
public static void main(String[] args) {
Integer a=new Integer(6);
Integer b=new Integer(9);
/*
自动拆箱: 把引用类型 转为 基本类型
*/
int c=a.intValue();//本质
int d=a;//自动拆箱 默认调用intValue();方法
/*
自动装箱: 把基本类型 转为 引用类型
自动装箱时,默认调用valueOf();方法
在valueOf()中 如果 基本类型值 在-128至127之间,会从缓存数组中直接取出一个Integer对象,减少创建次数
这种情况下,两个值相同,获取的是同一个对象
如果不在这个范围,每次都会创建一个新Integer对象返回
*/
int f=10000;
Integer yy=Integer.valueOf(f);
Integer y=f;//自动装箱 默认调用valueOf()方法
Integer z=f;
System.out.println(y==z);
}
}