目录
一、Java API概述(Application Programming Interface--应用程序编程接口)
常用类
String Arrays object Integer 日期Date
一、Java API概述(Application Programming Interface--应用程序编程接口)
API:JAVA语言中的实际提供的类和接口
API文档:对java中提供的类和接口中的功能进行说明的文档
二、Object
Object是java中所有类的基类(超类 祖宗类)
1、String toString()
返回对象的字符串表示形式
native修饰的方法成为本地方法(有些java是不实现的,直接调用本地操作系统的中的方法)
输出一个对象时,默认会调用类中的toString();我们的类中如果没有定义toString();就会找父类中的toString();
Object类中的toString()
public String toString() { return getClass().getName() + "@" + Integer.toHexString(hashCode()); //转成16进制的字符串 }
public class Car {
String name;
int price;
@Override
//将对象以字符串的形式输出
public String toString() {
/*return "Car{" +
"name='" + name + '\'' +
", price=" + price +
'}';*/
return name+":"+price;
}
}
public class Test {
public static void main(String[] args) {
Car car1 = new Car();
car1.name = "宝马";
car1.price = 30000;
System.out.println(car1.toString()); //没有重写toString()的时候会输出day1.Car@1b6d3586
//重写了toString(),就会输出/*Car{name='宝马', price=30000}*/宝马:30000
}
}
2、equals(Object obj)
一般默认为equals()方法比较的是对象中的内容是否相等
public class Car {
String name;
int price;
}
public class Test {
public static void main(String[] args) {
Car car1 = new Car();
car1.name="宝马";
car1.price=300000;
Car car2 = new Car();
car2.name="宝马";
car2.price=300000;
/*car1.equals(car2)由于car类中没有重写equals()这个方法,所以调用的是Object类中的equals()(object中的所有方法都是最基础的方法,可以说是第一个,没有重写)
public boolean equals(Object obj) {
return (this == obj);
}
== 用于基本类型比较,比较的值是否相等
== 用于引用类型比较,比较的是引用地址(对象地址)是否相等
*/
System.out.println(car1.equals(car2));//false
/*
Car car3 = car2;
System.out.println(car2.equals(car3)); //true
*/
String s1 = new String();
String s2 = new String();
/*字符串类中重写了Object类中的equals()方法,实现的是比较两个字符串中的每一个字母是否相等*/
System.out.println(s1.equals(s2));//true
}
}
三、Arrays类
1、equals()
int [] a = {1,2,3,4,5};
int [] b = {1,2,3,4,5};
System.out.println(Arrays.equals(a,b))//true
//Arrays.equals(a,b)比较数组中的内容是否相等
2、sort()
int [] a = {5,3,4,1,2};
Arrays.sort(a); //0-length-1 底层使用的是快速排序
System.out.println(Arrays.toString(a)); //[1,2,3,4,5]
int [] a = {5,3,4,1,2};
Arrays.sort(a,0,3); //对某个区间排序,开始位置(包含),结束位置(不包含)
System.out.println(Arrays.toString(a)); //[3,4,5,2,1]
引用类型的排序(比较)需要实现Comparable<类名>这个接口,并重写它的方法
import java.util.Objects;
public class User implements Comparable<User> {
int id;
String account;
int password;
public User(int id, String account, int password) {
this.id = id;
this.account = account;
this.password = password;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getAccount() {
return account;
}
public void setAccount(String account) {
this.account = account;
}
public int getPassword() {
return password;
}
public void setPassword(int password) {
this.password = password;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", account='" + account + '\'' +
", password=" + password +
'}';
}
@Override
public int compareTo(User o) {
return this.id-o.id; //表示按id排序 结果用来判断大小,大于0,小于0,等于0
}
}
import java.util.Arrays;
public class Test {
public static void main(String[] args) {
User user1 = new User(1,"1111111",666666);
User user2 = new User(2,"2222222",666666);
User user3 = new User(3,"3333333",666666);
User user4 = new User(4,"4444444",666666);
User user5 = new User(5,"5555555",666666);
User [] users = {user3,user1,user5,user2,user4};
Arrays.sort(users);
System.out.println(Arrays.toString(users));
}
}
3、binarySearch()
二分查找法(折半查找法) 前提:有序的
使用前先排序
import java.util.Arrays;
public class search {
public static void main(String[] args) {
int [] a = {5,3,4,1,2,9};
Arrays.sort(a);
System.out.println(Arrays.binarySearch(a,3));//2
System.out.println(Arrays.binarySearch(a,6));//-6
//返回的是负数,表示没有找到这个数,此时的值表示应该在排序好的数组中应该插入的数
}
}
4、copyof()
数组拷贝/数组复制 传入原数组 ,新数组长度
返回一个新数组,并将原数组值复制到新数组中
import java.util.Arrays;
public class copy {
public static void main(String[] args) {
int [] a ={1,2,3,4,5};
int [] b = Arrays.copyOf(a,10);
System.out.println(Arrays.toString(b));
}
}
四、String类
由多个字符组成的一串数据,值一旦被创建,就不能改变了,值一改变就会创建一个新的对象
底层:private final char value[];
1、String类的两种创建形式
String s = "abc"; 简化的创建方式
String s1 = new String("abc"); new + 构造方法
区别:
先去字符串常量池中查找有没有abc,如果没有就在字符串常量池中创建一个对象(abc),如果字符串常量池中有abc,那么直接指向已有的对象即可 String s1 = "abc"; String s2 = "abc"; System.out.println(s1==s2); //true
凡是new出来的,在内存空间中就是独一无二的 String s3 = new String("abc"); String s4 = new String("abc"); System.out.println(s3==s4);
2.构造方法
public String(){}
底层:
public String() {
this.value = "".value;
}
public String("字符串"){}
3、判断功能方法
equals()
比较字符串的内容是否相等
public class StringDemo {
public static void main(String[] args) {
String s1 = "abc";
String s2 = new String("abc");
System.out.println(s1.equals(s2)); //true
}
}
equalsIgnoreCase()
比较字符串的内容是否相等,忽略大小写
public class StringDemo {
public static void main(String[] args) {
String s1 = "abc";
String s2 = new String("abc");
System.out.println(s1.equalsIgnoreCase(s2));//true
}
}
contains()
是否包含指定的字符串(必须是相邻的)
public class StringDemo {
public static void main(String[] args) {
String s1 = "abc";
System.out.println(s1.contains("ab"));//true
System.out.println(s1.contains("ac"));//false
}
}
isEmpty()
判断是否为“ ”
public class StringDemo {
public static void main(String[] args) {
String s1 = "abc";
System.out.println(s1.isEmpty());//false
}
}
startsWith()
判断是否以指定子串开头
public class StringDemo {
public static void main(String[] args) {
String s1 = "abc";
System.out.println(s1.startsWith("a"));//true
}
}
endsWith()
判断是否以指定子串结尾
public class StringDemo {
public static void main(String[] args) {
String s1 = "abc";
System.out.println(s1.endsWith("c"));//true
}
}
compareTo()
字符串比大小
public class StringDemo {
public static void main(String[] args) {
String s1 = "a";
String s2 = "b";
System.out.println(s1.compareTo(s2));//97-98=-1
String s3 = "abc";
String s4 = "efg";
System.out.println(s3.compareTo(s4));//-4
}
}
4、获取功能
length()
获取字符串的长度
public class requreString {
public static void main(String[] args) {
String s = "abcdefgd";
System.out.println(s.length());//8
}
}
charAt()
获取指定位置(索引)的字符
public class requreString {
public static void main(String[] args) {
String s = "abcdefgd";
System.out.println(s.charAt(2));//c
}
}
indexOf()
从前向后找,找到首次出现指定字符串的索引
public class requreString {
public static void main(String[] args) {
String s = "abcdefgd";
System.out.println(s.indexOf("d"));//3
/*从前向后找,从第一个出现指定字符串的位置开始找下次出现指定字符串的索引*/
System.out.println(s.indexOf("d",s.indexOf("d")+1));
}
}
lastIndexOf()
从后向前找,首次出现指定字符串的位置
public class requreString {
public static void main(String[] args) {
String s = "abcdefgd";
System.out.println(s.lastIndexOf("d"));//7
}
}
substring()
从指定位置开始截取字符串,直接到最后一位,最终返回一个新的字符串
public class requreString {
public static void main(String[] args) {
String s = "abcdefgd";
System.out.println(s.substring(3));//defgd
/*截取指定区间,包含开始位置,不包含结束位置*/
System.out.println(s.substring(2,5));//cde
}
}
5、转换功能
getBytes();
默认是UTF-8编码,在UTF-8编码中,一个汉字占三个字节
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
public class shift {
public static void main(String[] args) throws UnsupportedEncodingException {
String s = "你好";
byte [] b = s.getBytes("UTF-8");//编码
System.out.println(Arrays.toString(b));//[-28, -67, -96, -27, -91, -67]
String s1 = new String(b,"UTF-8");//解码
System.out.println(s1);//你好
/*将指定的区间转码*/
String s2 = new String(b,0,3,"UTF-8");
System.out.println(s2);//你
}
}
charArray()
将字符串转为char类型的数组
public class shift {
public static void main(String[] args) throws UnsupportedEncodingException {
String s = "abcd";
char [] chars = s.toCharArray();
System.out.println(Arrays.toString(chars));//[a, b, c, d]
/*/将char数组转为字符串*/
String s1 = new String(chars);
System.out.println(s1);//abcd
String s2 = String.valueOf(chars);
System.out.println(s2);//abcd
}
}
toUpperCase()
将小写字母转为大写字母
public class shift {
public static void main(String[] args) throws UnsupportedEncodingException {
String s = "abcdEFG";
System.out.println(s.toUpperCase());//ABCDEFG
}
}
toLowerCase()
将大写字母转为小写字母
public class shift {
public static void main(String[] args) throws UnsupportedEncodingException {
String s = "abcdEFG";
System.out.println(s.toLowerCase());//abcdefg
}
}
concat()
连接,可以连接其他类型
public class shift {
public static void main(String[] args) throws UnsupportedEncodingException {
String s = "abcdEFG";
String s1 = "zgh";
System.out.println(s.concat(s1)); //abcdEFGzgh
System.out.println(s.concat("4"));//abcdEFG4
System.out.println(s.concat("中国"));//abcdEFG中国
}
}
split()
按照分隔符,将字符串拆分为数组
public class shift {
public static void main(String[] args) throws UnsupportedEncodingException {
String s = "abc:def:gh";
String [] strings = s.split(":");
System.out.println(Arrays.toString(strings));//[abc, def, gh]
}
}
6、正则表达式(规则表达式)
使用一些特定的符号来制定一个规则,使用此规则与一个字符串进行模式匹配,匹配成功后返回true,否则返回false
底层:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Regular {
public static void main(String[] args) {
Pattern p = Pattern.compile("a");//定义一个规则
Matcher m = p.matcher("b");//输入内容
boolean b = m.matches();//匹配
System.out.println(b);//false
}
}
简化:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Regular {
public static void main(String[] args) {
String s = "abc"; //输入内容
boolean b = s.matches("abc");//定义的规则
System.out.println(b);
}
}
\d: [0-9] 判断是否为数字
public class Regular {
public static void main(String[] args) {
String s = "1";
System.out.println(s.matches("\\d"));//true
System.out.println(s.matches("[0-9]"));//true
/*如果想要判断是否为1——9内*/
System.out.println(s.matches("[1-9]"));//true
}
}
X? : X,一次或一次也没有
X* : X,多次或一次也没有
X+:X,一次或多次
X{n}:X,恰好n次
X{n,}:X,至少n次
X{n,m}:X,至少n次,最大m次
[abc] : a或b或c
[a-zA-Z](A-z) : a~z或A~Z
\w : 单词字符:[a-z A-Z _ 0-9]
\D : 非数字 :[ ^0-9 ]
\W:非单词字符 [ ^\w ]
. : 表示任何字符 \ \ . 进行转义 |或
public class reg {
public static void main(String[] args) {
/*
手机号格式:11位 只能是数字 1开头 第二位是3或5或7 或8 或9
*/
String s1 = "13586556890";
System.out.println(s1.matches("1[35789]\\d{9}"));
/*
QQ号格式:6-12位 只能是数字 不能是0开头
*/
String s2 = "3278047324";
System.out.println(s2.matches("[1-9]\\d{5,11}"));
/*
邮箱格式:6-18位 3278047324@qq.com
*/
String s3 = "qr3285623442@qq.com";
System.out.println(s3.matches("\\w{6,18}@\\w{2,5}\\.(com|com\\.cn)"));
}
}
7、replace() 替换
public class Replace {
public static void main(String[] args) {
String s = "abcdefg";
String s1 = s.replace("c","p");
System.out.println(s1);//abpdefg
}
}
8、replaceAll()
与replace()的区别:replaceAll()可以使用正则表达式的符号,replace()不可以
public class Replace {
public static void main(String[] args) {
String s1 = s.replace("cd","zl");
String s2 = s.replaceAll("cd","zl");
String s3 = s.replaceAll("\\d","t");
System.out.println(s1);//abzl8ghzl8
System.out.println(s2);//abzl8ghzl8
System.out.println(s3);//abcdtghcdt
}
}
9、replaceFirst()
替换第一个出现的指定字符串
public class Replace {
public static void main(String[] args) {
String s = " sbcd ";
System.out.println(s.length());//6
System.out.println(s.trim().length());//4
}
}
10、trim()
去除空格,只能去除两边的空格,不能去除中间的空格
public class Replace {
public static void main(String[] args) {
String s = "abcdefgckc";
String s1 = s.replaceFirst("c","r");
System.out.println(s1);// abrdefgckc
}
}
五、StringBuffer类
线程安全,可变字符串,底层方法被synchronized修饰,底层默认是char数组,长度为16,如果给其添加内容,则其总长度为内容长度+16
当向StringBuffer中添加内容时,是将内容添加到底层的数组中,因为数组没有被final修饰,可以改变其内容,当数组装满时,会创建一个新的数组,将新数组的地址传给底层数组,StringBuffer对象是不会改变的
append()
向末尾添加内容
public class StringbufferDemo {
public static void main(String[] args) {
StringBuffer stringBuffer = new StringBuffer("abcd");
stringBuffer.append("efg");
System.out.println(stringBuffer);//abcdefg
/*返回的是底层数组中实际装入字符的长度*/
System.out.println(stringBuffer.length())//7
}
}
insert()
向指定位置添加内容
public class StringbufferDemo {
public static void main(String[] args) {
StringBuffer stringBuffer = new StringBuffer("abcd");
stringBuffer.insert(2,"y");
System.out.println(stringBuffer);//abycd
}
}
deleteCharAt()
删除指定位置的内容
public class StringbufferDemo {
public static void main(String[] args) {
StringBuffer stringBuffer = new StringBuffer("abcd");
stringBuffer.deleteCharAt(1);
System.out.println(stringBuffer);//acd
}
}
delete()
删除指定区间的内容,包含开始,不包含结束
public class StringbufferDemo {
public static void main(String[] args) {
StringBuffer stringBuffer = new StringBuffer("abcd");
stringBuffer.delete(0,3);
System.out.println(stringBuffer);//d
}
}
replace()
替换指定区间的内容,包含开始,不包含结束
public class StringbufferDemo {
public static void main(String[] args) {
StringBuffer stringBuffer = new StringBuffer("abcd");
stringBuffer.replace(0,3,"xxx");
System.out.println(stringBuffer);//xxxd
}
}
reverse()
逆序字符串
public class StringbufferDemo {
public static void main(String[] args) {
StringBuffer stringBuffer = new StringBuffer("abcd");
stringBuffer.reverse();
System.out.println(stringBuffer);//dcba
}
}
subString()
public class StringbufferDemo {
public static void main(String[] args) {
StringBuffer stringBuffer = new StringBuffer("abcd");
/*从指定位置开始截取字符串到末尾*/
System.out.println(stringBuffer.substring(2));//cd
/*截取指定区间的字符串,包含开始,不包含结束*/
System.out.println(stringBuffer.substring(0,2));//ab
}
}
六、StringBuilder类
多线程不安全,可变字符串
方法与StringBuffer一样
底层方法没有被synchronized修饰,而StringBuffer底层方法被synchronized修饰,synchronized是同步锁,所以StringBuffer是多线程安全的
Sting,StringBuffer和StringBuilder三者之间的区别:
String:底层都是数组实现,被final修饰,值是不能改变的,改变后会创建一个新的对象
StringBuffer:线程安全的,可变字符串
StringBuilder:线程不安全的(对于多线程访问同一个资源),可变字符串
七、基本数据类型包装类
(1)基本数据类型
java中是用关键字直接声明,用法简单
基本类型没有面向对象的使用方式,所以java中每种基本类型定义了一个类,来表示基本类型数据,这个类成为包装类
以int类型为例:
System.out.println(Integer.MAX_VALUE);//2147483647
System.out.println(Integer.MIN_VALUE);//-2147483648
System.out.println(Integer.BYTES);//4
System.out.println(Integer.SIZE);//32
/*构造方法*/
Integer s = new Integer(10);
Integer s1 = new Integer("10");
Integer中的转换方法:
toBinaryString() 转二进制
System.out.println(Integer.toBinaryString(3));//11
toHexString() 转十六进制
System.out.println(Integer.toHexString(17));//11
toOctalString() 转八进制
System.out.println(Integer.toOctalString(9));//11
intValue()
把对象中的基本类型取出来
Integer i1 = new Integer(10);//把基本类型包装到对象中
int i2 = i1.intValue();//把对象中的基本类型取出来
parseInt()
将Sting类型转为int类型
int i3 = Integer.parseInt("20");
toString()
将int包装类型转为String类型
Integer i1 = new Integer(10);
System.out.println(i1.toString());
valueOf()转型
Integer i4 = Integer.valueOf(10);//将基本类型转为引用类型
Integer i5 = Integer.valueOf("10");//将String类型转为引用类型
(2)装箱和拆箱(Auto-boxing/unboxing)
自动装箱
自动的将基本类型转为引用类型,底层默认调用valueOf(int a)
public class box {
public static void main(String[] args) {
int a = 10;
Integer a1 = a;
Integer x1 = 127;
Integer x2 = 127;
System.out.println(x1==x2);//true
System.out.println(x1.equals(x2));//true
/*
public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}
*/
Integer x3 = 128;
Integer x4 = 128;
System.out.println(x3==x4);//false
System.out.println(x1.equals(x2));//true
}
}
自动拆箱
底层默认调用intValue()
public class box {
public static void main(String[] args) {
int a = 10;
Integer a1 = a;
int a2 = a1;
}
}
八、Math类
包含了一些常用的数学计算方法
import java.sql.SQLOutput;
public class MathDemo {
public static void main(String[] args) {
System.out.println(Math.PI);//3.141592653589793
/*绝对值*/
System.out.println(Math.abs(-1));//1
/*平方根*/
System.out.println(Math.sqrt(9));//3.0
/*随机数*/
System.out.println(Math.random());//0.9053814848484472 0~1之间的随机数,可以取到0,但是取不到1
/*向下取整*/
System.out.println(Math.floor(9.9));//9.0
/*向上取整*/
System.out.println(Math.ceil(9.1));//10.0
/*四舍五入*/
System.out.println(Math.round(9.6));//10
/*2的3幂*/
System.out.println(Math.pow(2,3));//8.0
}
}
九、random类
import java.util.Random;
public class RandomDemo {
public static void main(String[] args) {
Random r = new Random();
/*在int的范围内随机取出*/
System.out.println(r.nextInt());
/*在0~10之间随机取出,取到0,不能取到10*/
System.out.println(r.nextInt(10));
byte [] b = new byte[5]; //将数组的引用地址传进去
r.nextBytes(b);//随机取出数组.length个随机数
System.out.println(Arrays.toString(b));//[-21, 103, -60, 50, 22]
}
}
十、system类
public class LongDemo {
public static void main(String[] args) {
Long s = System.currentTimeMillis();
System.out.println(s);//1637513078999 是自1970.1.1 0:0:0到程序运行时的那一刻的时间差(以毫秒为单位)
/*System.exit(0);//退出虚拟机
System.gc();//垃圾回收,程序一般不使用*/
int a[] = {1,2,3,4,5};
int b[] = new int[10];
System.arraycopy(a,0,b,0,5);//系统中提供的最底层的数组内容复制方法System.arraycopy(原数组,原数组得位置,目标数组,目标数组的位置,长度)
System.out.println(Arrays.toString(b));//[1, 2, 3, 4, 5, 0, 0, 0, 0, 0]
}
}
十一、Date类
import java.util.Date;
public class DateDemo {
public static void main(String[] args) {
/*创建一个日期对象,里面包含了程序运行时的那一刻的时间*/
Date date = new Date();
System.out.println(date.getTime());//1637514109171
/*此类方法称为过期方法,不建议使用,有新方法代替*/
System.out.println(date.getYear()+1900);//2021
System.out.println(date.getMonth()+1);//11
Date date1= new Date(1637514109171l);
System.out.println(date1);//Mon Nov 22 01:01:49 CST 2021
}
}
十二、Calender类
Calender是一个抽象类,抽象类不能创建对象
Calendar c = Calendar.getInstance();
System.out.println(c);//获得的是一个子类对象
/*GregorianCalendar 公历*/
Calendar c = new GregorianCalendar();
System.out.println(c);
int date = c.get(Calendar.DAY_OF_MONTH);
System.out.println(date); //25
System.out.println(c.get(Calendar.DAY_OF_YEAR));//329
System.out.println(c.get(Calendar.DAY_OF_WEEK_IN_MONTH)+1);//5
System.out.println(c.get(Calendar.HOUR_OF_DAY));//19
十三、SimpleDateFormal类
将字符串日期转为Date(对象) 在网页中输入生日(字符串)提交给java转为Date(对象)
public class SlimpleDateFormalDemo {
public static void main(String[] args) {
String s1 = "1999-11-02";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
Date date = sdf.parse(s1);
System.out.println(date);
} catch (ParseException e) {
e.printStackTrace();
}
}
}
将Date(对象)转为字符串日期 Java中日期对象—网页—字符串
public class SlimpleDateFormalDemo {
public static void main(String[] args) {
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
String s = sdf.format(date);
System.out.println(s);
}
}
十四、BigIntegerDemo类
大整数,可以存放基本数据类型int中放不下的数据
public class BigIntegerDemo {
public static void main(String[] args) {
System.out.println(Long.MAX_VALUE);//基本数据类型的最大长度:9223372036854775807
BigInteger b = new BigInteger("156489498947613203213164");
BigInteger b1 = new BigInteger("222222222222222222222222222");
System.out.println(b.add(b1));//222378711721169835425435386
System.out.println(b.multiply(b1));//34775444210580711825147555520780111344974843730408
}
}
十五、BigDecimal类
大浮点数
public class BigDecimalDemo {
public static void main(String[] args) {
Double a = new Double("12");
Double b = new Double("11.9");
System.out.println(a-b);//0.09999999999999964
System.out.println((a-b)==0.1);//false
BigDecimal c = new BigDecimal("12");
BigDecimal d = new BigDecimal("11.9");
System.out.println(c.subtract(d));//0.1
BigDecimal g = new BigDecimal("10");
BigDecimal h = new BigDecimal("3");
/*因为输出是无限循环,所以报错,然后调用 public BigDecimal divide(BigDecimal divisor, int scale, int roundingMode){}这个方法4:精度(几位小数),BigDecimal.ROUND_CEILING:向上取整*/
System.out.println(g.divide(h,4,BigDecimal.ROUND_CEILING));//3.3334
}
}