Java API概述
API(Application Programming Interface)应用程序编程接口,是对java预先定义的类或接口功能和函数功能的说明文档,目的是提供 给开发人员进行使用帮助说明
基本数据类型包装类
Java语言是一个面向对象的语言,但是Java中的基本数据类型却是不面 向对象的,这在实际使用时存在很多的不便,为了解决这个不足,在设 计类时为每个基本数据类型设计了一个对应的类进行代表,这样八个和 基本数据类型对应的类统称为包装类
包装类(如:Integer,Double等)这些类封装了一个相应的基本数据 类型数值,并为其提供了一系列操作方法。
比如一些Integer的一些使用方法
public class Demo1 {
public static void main(String[] args) {
Integer integer = new Integer(10);//这是引用类型
System.out.println(integer.byteValue()+10);
System.out.println(integer.toString()+10);
System.out.println(Integer.BYTES);//字节数
System.out.println(Integer.SIZE);//二进制位数
System.out.println(Float.SIZE);
System.out.println(Integer.max(10,5));
System.out.println(Integer.getInteger(String.valueOf(5)));
System.out.println(Integer.MAX_VALUE);
System.out.println(Integer.MIN_VALUE);
}
对于包装类来说,这些类的用途主要包含两种:
1.作为和基本数据类型对应的类类型存在。
2. 包含每种基本数据类型的相关属性如最大值、最小值等,以及相关的操 作方法。
以下方法以java.lang.Integer为例
/**
* public static final int MAX_VALUE 最大的int型数(231-1)
* public static final int MIN_VALUE 最小的int型数(-231)
*
* 构造方法:
* Integer(int a);
* Integer(String a);
* 比较方法
* static int compareTo(Integer a);
* boolean equals(Object);
* int max(int a,int b); i
* nt min(int a,int b);
* 转换方法
* static toBinaryString(int i);
* static String toHexString(int i);
* static String toOctalString(int i);
* int intValue(); static int parseInt(String s);
* String toString();
* static String toString(int i)
* static Integer valueOf(int i)
* static Integer valueOf(String s)
*/
装箱和拆箱Auto-boxing/unboxing
装箱:自动将基本数据类型转换为包装器类型
装箱的时候自动调用的是Integer的valueOf(int)方法
拆箱: 自动将包装器类型转换为基本数据类型
拆箱的时候自动调用的是Integer的intValue方法
public class Demo2 {
public static void main(String[] args) {
/*
装箱:
● 自动将基本数据类型转换为包装器类型
● 装箱的时候自动调用的是Integer的valueOf(int)方法
拆箱:
● 自动将包装器类型转换为基本数据类型
● 拆箱的时候自动调用的是Integer的intValue方法
*/
int a = 10;
int c = 0;
Integer b = a;
//自动装箱 默认调用valueOf(),创建一个Integer对象,将基本类型转为Integer
int y = c;
//自动拆箱,将引用类型转换为基本类型
//自动装箱的问题
Integer x = 127;//自动装箱的问题
Integer x1 = Integer.valueOf(127);//装箱
System.out.println(x==x1);//false
/*
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
在缓存区里面,区间是-128到127之间,从缓存区取出一个对象,返回,地址一样
不在区间内,创建新对象,地址会不同
*/
Integer y1 = new Integer(128);//新创建一个对象
Integer y2 = new Integer(128);
System.out.println(y1==y2);//false
//在引用数据类型中,==比较的是对象的地址而非对象的内容
}
}
Object类
Object类是所有Java类的祖先(根基类)每个类都使用 Object 作为 超类(父类)所有对象(包括数组)都实现这个类的方法。
如果在类的声明中未使用extends关键字指明其基类,则默认基类为 Object类
public class Person { … }
等价于:
public class Person extends Object { … }
toString方法
Object类中定义有public String toString()方法,其返回值是 String 类型,描述当前对象的有关信息。
在进行String与其它类型数据的连接操作时(如: System.out.println(“info”+person)),将自动调用该对象类的 toString()方法。可以根据需要在用户自定义类型中重写toString()方法。
public class Student implements Comparable<Student> {
private String name;
private int num;
public Student(String name,int num){
this.name =name;
this.num =num;
}
//这样写
@Override
public int compareTo(Student o) {
return this.num - o.num;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", num='" + num + '\'' +
'}';
}
}
equals()方法
public boolean equals(Object obj)方法: 提供定义对象是否“相等”的逻辑。 Object 的 equals 方法 定义为:x.equals ( y ) ,当 x 和 y是同一 个对象的引用时返回 true 否则返回 false
JDK提供的一些类,如String,Date等,重写了Object的equals方 法,调用这些类的equals方法, x.equals (y) ,当x和y所引用的对象 是同一类对象且属性内容相等时(并不一定是相同对象),返回 true 否则返回 false。
Object类的 equals()方法 是比较两个引用类型的对象地址是否相等,跟类似于“==”,而其他的包装类几乎都重写了基类的方法,他们是比较其内容是否相等
Object中的equals原码:
/**
* public boolean equals(Object anObject) {
* if (this == anObject) {
* return true;
* }
* if (anObject instanceof String) {
* String anotherString = (String)anObject;
* int n = value.length;
* if (n == anotherString.value.length) {
* char v1[] = value;
* char v2[] = anotherString.value;
* int i = 0;
* while (n-- != 0) {
* if (v1[i] != v2[i])
* return false;
* i++;
* }
* return true;
* }
* }
* return false;
* }
*/
代码演示equals()
public class ObjectEqual {
private static String a="20";
private static String b="20";
//public class demo1 extends Object{
//默认继承Object类
public static void main(String[] args) {
System.out.println(a.equals(b));//true
//ctrl+鼠标左键进入String包装类中,这是调用了String中的equal()方法,对比的是对象的内容
System.out.println(a==b);//true
/*
public boolean equals(Object obj) {
return (this == obj);
}
这是Object类的equal方法,比较的的是对象的地址
*/
Object object = new Object();
System.out.println(object.equals(b));//false
//类似于装箱
}
}
Arrays类
用于操作数组工具类,里面定义了常见操作数组的静态方法。
equals()方法
比较两个非同一数组是否相等,而数组本身的equals判断另一个数 组是否它本身。
声明:public static booleanequals(type[]a,type[]a2) ,参数的类型可以是原生数据类型和引用类型的任意一种类型。 返回:如果两个相等,则返回true,否则返回false
原码:
/**
* public boolean equals(Object anObject) {
* if (this == anObject) {
* return true;
* }
* if (anObject instanceof String) {
* String anotherString = (String)anObject;
* int n = value.length;
* if (n == anotherString.value.length) {
* char v1[] = value;
* char v2[] = anotherString.value;
* int i = 0;
* while (n-- != 0) {
* if (v1[i] != v2[i])
* return false;
* i++;
* }
* return true;
* }
* }
* return false;
* }
*/
sort - 排序
作用于数组的所有元素 public static void sort(type[] a)
作用于数组指定范围内的元素:public static void sort(type[] a, int fromIndex(包括), int toIndex(不包括))
将指定的类型(除boolean以外的任意原生数据类型)数组所有元素(或指定范 围内的元素)按数字升序进行排序。 object型数组,根据元素的自然顺序,对指定对象数组进行升序排序。 (fromIndex==toIndex,则排序范围为空)
binarySearch -使用二分搜索算法搜索指定数组
声明: public static int binarySearch(type[] a, type key)
public static int binarySearch(long[] a,int fromIndex,int toIndex,long key)
描述: 使用二分搜索算法搜索指定的type型数组,以获得指定的值。
参数: a - 要搜索的数组。
key - 要搜索的值。
fromIndex - 要排序的第一个元素的索引(包括)。
toIndex - 要排序的最后一个元素的索引(不包括)。
type -byte、double、float、object、long、int、short、char
public class ArraysBinarySearch {
public static void main(String[] args) {
//要查找的数组要排好序
int []a ={1,2,3,4,5,6,7};
System.out.println(Arrays.binarySearch(a,6));
//如果查找到了。输出索引
System.out.println(Arrays.binarySearch(a,12));
//没有找到,则输出负数插入点,二分查找,因为12比7大,但是8号索引不是12,所以输出-8插入点
}
/**源代码
* private static int binarySearch0(int[] a, int fromIndex, int toIndex, 也可以给定区间进行查找
* int key) {
* int low = fromIndex;
* int high = toIndex - 1;
* 给定的区间
*
* while (low <= high) { 开始值比结尾值小,符合条件,开始查找
* int mid = (low + high) >>> 1; 二进制带符号右移一位,也就是除以二
* int midVal = a[mid]; 从中间开始
*
* if (midVal < key) 如果要查找的数比中值小
* low = mid + 1; 查找左边
* else if (midVal > key) 如果要查找的数比中值大
* high = mid - 1; 查找右边
* else
* return mid; // key found 输出
* }
* return -(low + 1); // key not found.
* }
*/
}
toString() 方法
声明: public static String toString(type[] a)
描述: 返回指定数组内容的字符串表示形式。
基本数组,字符串表示形式由数组的元素列表组成,括在[],相邻元素 用“, ”(逗号加空格)分隔。
原码:
/**
* public static String toString(int[] a) {
* if (a == null)
* return "null";
* int iMax = a.length - 1; 也就是空的数组
* if (iMax == -1)
* return "[]";
*
* StringBuilder b = new StringBuilder();
* b.append('[');
* for (int i = 0; ; i++) {
* b.append(a[i]);
* if (i == iMax)
* return b.append(']').toString();
* b.append(", ");
* }
* }
*/
String类:StringBuffer类/StringBuilder类
String类概述: 字符串是由多个字符组成的一串数据(字符序列)的字符串常量,java中所有字 符串都是String类的实例
有两种创建形式:
1.String s = “abc”;
先在栈中创建一个对String类的对象引用变量s,然后去字符串常量池中查找 有没有"abc", 如果没有则在常量池中添加”abc”, s引用变量指向常量池中 的”abc”,如果常量池中有,则直接指向改地址即可,不用重新创建.
2.String s = new String(“abc”);
一概在堆中创建新对象,值存储在堆内存的对象中。
代码演示:
public class StringDemo {
public static void main(String[] args) {
//String有两种赋值的办法
/**
* String a ="abc"; 直接赋值法
* String a = new String("abc"); 构造方法赋值法
*
* 字符串值不能变,因为底层用到了一个final修饰的char数组
*/
String a = "abc";
a+= "def";
a+="jhi";
System.out.println(a);
/**
* 一旦改变,会创建一个新的数组对象
*/
String s1 = "abc";
String s2 = "adc";
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
/**
* 普通创建String类型会从 堆内存 中的 字符串常量池 中查找,如果没有,则会创建一个地址;如果有,则直接用那个地址
* new 是直接创建新对象,地址当然不同
*/
}
}
构造方法 :
public String()
public String(String str)
public String(byte[] bytes)
public String(char[] value)
获取功能 :
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)
转换功能 :
byte[] getBytes()
char[] toCharArray()
static String valueOf(char[] chs)
String toLowerCase()
String toUpperCase()
String concat(String str) Stirng[] split(分割符);
public class StringDemo2 {
public static void main(String[] args) {
//1
/**
* equalsIgnoreCase(String anotherString)
* 返回boolean
* 将此 String与其他 String比较,忽略案例注意事项
* 那种验证图片的时候会用到,忽略大小写
*/
String a ="abcDEF";
System.out.println(a.equalsIgnoreCase("ABCdef"));//true,忽略大小写
//2
/**
* contains(CharSequence s) 判断是否含指定字符串
* 当且仅当此字符串包含指定的char值序列时才返回true。
*/
String a1 ="adcdef";
System.out.println(a1.contains("a"));//true
System.out.println("两个"+a1.contains("ac"));//false, ac是一个整体
//3
/**
* isEmpty() 判断字符串是否为空
* 返回 true如果,且仅当 length()为 0
*/
String a2 ="abc";
String a21 ="";
System.out.println(a2.isEmpty());//false
System.out.println(a21.isEmpty());//true
//3
/**
* startsWith(String prefix, int toffset) 开头
* 测试在指定索引处开始的此字符串的子字符串是否以指定的前缀开头。
* 如果是,返回true,否则false;
*
* endsWith(String prefix, int toffset) 结尾
* 测试在指定索引处开始的此字符串的子字符串是否以指定的前缀结尾。
* 如果是,返回true,否则false;
*/
String a3 = "abcdef";
System.out.println(a3.startsWith("a"));//true
System.out.println(a3.endsWith("f"));//true
//4
/**
* compareTo(String anotherString)
* 按字典顺序比较两个字符串。
* 返回值是int类型,如果前值大于后面,则返回负数,后面比前面大,则返回整数;
* 按字典顺序比较 abcd...
*/
System.out.println("a".compareTo("b"));//-1
System.out.println("c".compareTo("a"));//2
//5
/**
* length()
* 返回此字符串的长度。
*/
String a5 = "abcde";
System.out.println(a.length());//5
//6
/**
* charAt(int index)
* 返回 char指定索引处的值。
* 返回值是char类型
*/
String a6 = "adc";
System.out.println(a6.charAt(0));//第0个索引,'a'
//7
/**
* indexOf(String str) 返回指定字符第一次出现的字符串内的索引。(返回指定字符首次出现的位置)
* indexOf(String str,int fromIndex) 返回指定字符第一次出现的字符串内的索引,以指定的索引开始搜索。
* 返回值都是int类型
*/
String a7 ="abcabde";
System.out.println(a7.indexOf("a"));//0,不在字符串内的数组返回-1
System.out.println(a7.indexOf("e",3));//3,从指定位置开始找,找不到就返回-1
//8
/**
* lastIndexOf(String str) 返回指定子字符串最后一次出现的字符串中的索引。
* lastIndexOf(String str, int fromIndex) 返回指定子字符串的最后一次出现的字符串中的索引,从指定索引开始向后搜索。
* 返回值都是int类型
* 返回的索引是其中的最大值k :
*
* k <= fromIndex && this.startsWith(str, k)
* 如果k的值不存在,则返回-1 。
*/
String a8 ="abccabde";
System.out.println(a8.lastIndexOf("a"));//3
System.out.println(a8.lastIndexOf("a",5));//从第五个位置开始找 4
System.out.println(a8.lastIndexOf("a",3));//从第三个位置开始找 0
//9
/**
* substring(int beginIndex)
* 从第beginIndex处开始索引到结尾,将这些索引的值赋给新的数组
* 截取的值为开区间,不取其值
*/
String a9 = "abcdefg";
String a91 = a9.substring(2);
System.out.println(a91);//“cdefg”
//10
/**
* toLowerCase() 将所有在此字符 String使用默认语言环境的规则,以小写
* toUpperCase(Locale locale) 将所有在此字符 String使用给定的规则,大写 Locale
*/
String a10 = "abcDEF";
System.out.println(a10.toLowerCase());//abcdef
System.out.println(a10.toUpperCase());//ABCDEF
//11
/**
* String concat(String str)
* 将指定的字符串连接到该字符串的末尾。
*/
String a11 = "abc";
System.out.println(a11.concat("def"));//abcdef
//12
/**
* Stirng[] split(分割符); 吧指定字符串拆封成数组***重点***
* 通过将该字符串围绕给定的正则表达式的匹配来计算的字符串数组
* 分隔符要取好 比如 " : "
*/
String a12 = "ab:cd:ef:gh";
String[] a13;
a13 =a12.split(":");
System.out.println(Arrays.toString(a13));//[ab,cd,ef,gh]
spilt()方法中药以 “.”之类的关键字为分割点,要加入“\\. ”将其转义为普通字符,才能用
String a14 = "ab.cd.ef.gh";
String []a15;
a15 =a14.split("\\.");
System.out.println(Arrays.toString(a15));//[ab,cd,ef,gh]