目录
2.2 String类/StringBuffer类/StringBuilder类
2.2.12 String类和StringBuffer类的转换
2.4 基本类型包装类(Integer,Character)
2.8 Date类/DateFormat类/Calendar类
1. API概述
API(Application Programming Interface) ,全称应用程序编程接口。就是JDK提供给我们的一些能提高编程效率各种功能的java类。
2. 常用类
2.1 Object类/Scanner类
2.1.1 Object类概述及构造方法
概述:类层次结构的根类,所有类都直接或者间接的继承自该类。
构造方法:
public Object();
2.1.2 Object类的成员方法
- public int hashCode() ——返回该对象的哈希码值
哈希码值是根据哈希算法将该对象的实际地址值计算转换的得到的值
package cn.itcast_01;
//测试类
public class StudentTest {
public static void main(String[] args) {
//创建对象
Student s1 = new Student();
//调用Object类中的hashCode方法
System.out.println(s1.hashCode());//366712642
Student s2 = new Student();
System.out.println(s2.hashCode());//1829164700
Student s3 = s1;
System.out.println(s3.hashCode());//366712642
}
}
- public final Class getClass() ——返回此Obeject类的运行时类,即Class类型的对象,可以通过Class类中的方法获取该类相关信息
- public String getName()——以 String 的形式返回此 Class 对象所表示的实体(类、接口、数组类、基本类型或 void)名称
package cn.itcast_01;
public class StudentTest {
public static void main(String[] args) {
//public int hashCode()
Student s1 = new Student();
System.out.println(s1.hashCode());//366712642
Student s2 = new Student();
System.out.println(s2.hashCode());//1829164700
Student s3 = s1;
System.out.println(s3.hashCode());//366712642
//public final Class getClass()
Student s = new Student();
//调用Object类中的getClass方法,返回的是Class类型的对象
Class c = s.getClass();
//调用Class中的getName()方法,返回的是此Class对象所表示的实体名称
String str = c.getName();
System.out.println(c);//class cn.itcast_01.Student
System.out.println(str);//cn.itcast_01.Student
//链式编程
System.out.println(s.getClass().getName());//cn.itcast_01.Student
}
}
- public String toString() ——返回该对象的字符串表示
要想输出的信息有意义,建议所有子类都重写该方法
重写原则:将该类的所有成员变量值组成返回
自动生成操作:右键→source→toString
直接输出一个对象的名称实际上就是调用该对象的toString()方法
package cn.itcast_02;
//学生类
public class Student {
//成员变量
private String name;
private int age;
public Student() {
super();
}
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
//return super.toString();
//默认返回父类Object的toString方法,要想输出的信息有意义需要重写
return "姓名:"+ name + ",年龄:"+age;//手动重写
}
}
//测试类
package cn.itcast_02;
public class StudentDemo {
public static void main(String[] args) {
//创建对象
Student s = new Student();
System.out.println(s.hashCode());//366712642
System.out.println(s.getClass().getName());//cn.itcast_02.Student
//public String toString()
System.out.println(s.toString());//姓名:null,年龄:0
//不重写方法时等价于
System.out.println(s.getClass().getName()+'@'+
Integer.toHexString(s.hashCode()));//cn.itcast_02.Student@15db9742
//方法重写后
System.out.println(s.toString());//姓名:null,年龄:0
System.out.println(s);//姓名:null,年龄:0
}
}
- public boolean equals(Object obj) ——指示其他某个对象是否与此对象相等,子类方法重写的时候一般比较对象的值是否相等
只能比较引用类型(案例中都是比较的对象),默认情况下比较的是地址值
子类方法重写的时候,根据要比较的成员变量来决定返回值是true还是false(使用条件语句,返回值为boolean型)
看成员变量的类型,如果为基本类型,使用==比较;如果为引用类型,看该类型中有没有提供相应的比较方法
String的equals()方法重写自Object类,比较的是字符串的内容而不是地址值
package cn.itcast_03;
// 学生类
public class Student {
// 成员变量
private String name;
private int age;
public Student() {
super();
}
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public boolean equals(Object obj) {
// 要使用学生类的特有成员变量需要向下转型
Student s = (Student) obj;
if (this.name.equals(s.name) && this.age == s.age) {
return true;
} else {
return false;
}
}
}
// 测试类
public class StudentDemo {
public static void main(String[] args) {
// 创建对象
Student s1 = new Student("守鹤", 1);
Student s2 = new Student("守鹤", 1);
Student s3 = s1;
Student s4 = new Student("九尾", 9);
// ==两边为引用型时,比较的是对象的地址值
System.out.println(s1 == s2);// false
System.out.println(s1 == s3);// true
System.out.println("---------------");
System.out.println(s1.equals(s1));// true
// 这里相当于obj=s2,子类型赋给父类型,多态
System.out.println(s1.equals(s2));// true
System.out.println(s1.equals(s3));// true
System.out.println(s1.equals(s4));// false
}
}
代码优化:
- 地址值相同的对象一定是true,不需要向下转型并比较,提高效率;
- 向下转型之前需要判断obj是否是Student的对象(Object类能接收任意类的对象),判断以提高代码的安全性
- 是就运行
- 不是就返回false,否则会报错
- 判断对象是否是某个类的对象的格式:对象名 instanceof 类名
- if语句可以用三元改进
最终版代码:右键→source→hashCode() and equals()
// 优化部分代码
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if(!(obj instanceof Student)) {
return false;
}
// 要使用学生类的特有成员变量需要向下转型
Student s = (Student) obj;
return this.name.equals(s.name) && this.age == s.age;
}
}
- protected void finalize() ——当垃圾回收器确定不存在对该对象的更多引用时,由对象的垃圾回收器调用此方法,用于回收垃圾(不确定何时)
- protected Object clone()——创建并返回此对象的一个副本
protected修饰的方法clone()方法在Student类(Object的一个子类中)中可以直接访问
但是不能在测试类(Object另一个子类)中通过对象去访问Student类中的clone()方法,需要在Student类中方法重写
Cloneable是一个标记接口,没有方法,实现该接口的类就可以实现对象的复制了
两个引用指向同一个对象(对象和引用的区别),改动对结果有影响
克隆的改动与原本的改动无关
package cn.itcast_04;
//学生类,实现接口Cloneable
public class Student implements Cloneable{
// 成员变量
private String name;
private int age;
public Student() {
super();
}
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
//测试类
public class StudentDemo {
public static void main(String[] args) throws CloneNotSupportedException {
// 创建学生对象
Student s = new Student();
s.setName("九尾");
s.setAge(9);
// 克隆学生对象
Object obj = s.clone();
Student s2 = (Student) obj;
//引用s3与s指向同一个对象
Student s3=s;
System.out.println("姓名:"+s.getName()+";年龄:"+s.getAge());
System.out.println("姓名:"+s2.getName()+";年龄:"+s2.getAge());
System.out.println("姓名:"+s3.getName()+";年龄:"+s3.getAge());
System.out.println("--------------------");
//区别
s3.setName("守鹤");
s3.setAge(1);
System.out.println("姓名:"+s.getName()+";年龄:"+s.getAge());
System.out.println("姓名:"+s2.getName()+";年龄:"+s2.getAge());
System.out.println("姓名:"+s3.getName()+";年龄:"+s3.getAge());
}
}
输出:
姓名:九尾;年龄:9
姓名:九尾;年龄:9
姓名:九尾;年龄:9
--------------------
姓名:守鹤;年龄:1
姓名:九尾;年龄:9
姓名:守鹤;年龄:1
2.1.3 Scanner类概述及其构造方法
概述:JDK5以后用于获取用户的键盘输入
构造方法:
public Scanner(InputStream source)
2.1.4 Scanner类的成员方法
基本格式
public boolean hasNextXxx();//判断是否是Xxx类型的元素
- 判断是否还有下一个输入项,其中Xxx可以是Int,Double等
- 如果需要判断是否包含下一个字符串,则可以省略Xxx
public Xxx nextXxx();//获取该元素
- 获取下一个输入项,Xxx的含义和上个方法中的Xxx相同
默认情况下,Scanner使用空格,回车等作为分隔符
//两个方法的配套使用
package cn.itcast_01;
import java.util.Scanner;
public class ScannerDemo {
public static void main(String[] args) {
//创建键盘录入对象,然后ctrl+shift+o导包
Scanner sc = new Scanner(System.in);
//提示语句
System.out.println("请输入一个整数:");
//获取数据
if(sc.hasNextInt()) {
int x = sc.nextInt();
System.out.println("x:"+x);
}else {
System.out.println("您输入的数据有误");
}
//关闭输入流,释放内存空间
sc.close();
}
}
常用方法
- public int nextInt() ——获取一个int类型的值
- public String nextLine()——获取一个String类型的值
- 注意事项:先获取数值后获取字符串会出现问题
- 原因:将换行符号当成字符串赋值
- 解决方案:A:重新创建对象 B:都以字符串形式接收,然后把字符串转成所需要的类型(推荐)
2.2 String类/StringBuffer类/StringBuilder类
2.2.1 String类概述及其构造方法
概述:字符串是由多个字符组成的一串数据(字符序列) ,也可以看成是字符数组
- 字符串字面值(eg:"abc")也可以看成是一个字符串对象
- 字符串是常量,一旦被赋值就不能更改
构造方法:
- public String();——空构造
- public String(byte[] bytes);——把字节数组转成字符串
- public String(byte[] bytes,int index,int length);——把字节数组的一部分转成字符串
- public String(char[] value);——把字符数组转成字符串
- public String(char[] value,int index,int count);——把字符数组的一部分转成字符串
- public String(String original);——把字符串常量值转成字符串
String的方法:
- public int length();——返回次字符串的长度
package cn.itcast_01;
public class StringDemo {
public static void main(String[] args) {
// 空构造
String s1 = new String();
System.out.println("s1:" + s1);
System.out.println("s1.length:" + s1.length());
// public String(byte[] bytes);
byte[] bys = { 97, 98, 99, 100, 101 };
String s2 = new String(bys);
System.out.println("s2:" + s2);
System.out.println("s2.length:" + s2.length());
// public String(byte[] bytes,int index,int length);
String s3 = new String(bys, 2, 2);
System.out.println("s3:" + s3);
System.out.println("s3.length:" + s3.length());
// public String(char[] value);
char[] chs = { 'a', 'b', 'c', 'd', 'e', '叶', '英' };
String s4 = new String(chs);
System.out.println("s4:" + s4);
System.out.println("s4.length:" + s4.length());
// public String(char[] value,int index,int count);
String s5 = new String(chs, 4, 3);
System.out.println("s5:" + s5);
System.out.println("s5.length:" + s5.length());
// public String(String original);
String s6 = new String("abcde");
System.out.println("s6:" + s6);
System.out.println("s6.length:" + s6.length());
String s7 = "abcde";// 这里将字符串字面值看成是一个字符串对象
System.out.println("s7:" + s7);
System.out.println("s7.length:" + s7.length());
}
}
2.2.2 String类的特点
特点:
- 字符串直接赋值的方式是,先到字符串常量池(在方法区中)找有没有对应字符串,有就返回;没有就开辟空间创建一个再返回
- 一旦被赋值就不能被改变指的是方法区中的值不能改变,而不是引用的变量(引用和值的区别、引用和对象的区别)
package cn.itcast_02;
public class StringDemo2 {
public static void main(String[] args) {
String s1 = "hello";
String s2 = "world";
String s3 = "helloworld";
System.out.println(s3 == s1 + s2);
System.out.println(s3.equals((s1 + s2)));
System.out.println(s3 == "hello" + "world");
System.out.println(s3.equals("hello"+"world"));
}
}
输出:
false
true
true
true
注意:
- 字符串如果是变量相加,先开空间再拼接;
- 字符串如果是常量相加,是先加,然后在常量池里找,有就返回,没有就创建
2.2.3 String类的判断功能
- boolean equals(Object obj) ——比较字符串的内容是否相同,区分大小写
- boolean equalsIgnoreCase(String str)——比较字符串的内容是否相同,不区分大小写
- boolean contains(String str) ——判断大字符串中是否包含小字符串
- boolean startsWith(String str)——判断字符串是否以某个指定字符串开头
- boolean endsWith(String str)——判断字符串是否以某个字符串结尾
- boolean isEmpty()——判断字符串是否为空
注意:字符串内容为空和字符串对象为空的区别
- String s = " "; // 有对象,内容为空
- String s = null; //连对象都没有
2.2.4 String类的获取功能
- int length() ——获取字符串长度
- char charAt(int index)——获取指定索引位置的字符
- int indexOf(int ch)——返回指定字符在此字符串第一次出现的索引
- int indexOf(String str)——返回指定字符串在此字符串中第一次出现的索引
- int indexOf(int ch,int fromIndex)——返回指定字符在此字符串从指定位置后第一次出现的索引
- int indexOf(String str,int fromIndex)——返回指定字符串在此字符串从指定位置后第一次出现的索引
- String substring(int start) ——从指定位置开始截取字符串,默认到末尾结束,包括start索引
- String substring(int start,int end)——从指定位置开时,到指定位置结束截取字符串,包括start索引不包括end
- 字符串的遍历(int length()和char charAt()方法)
2.2.5 String类的转换功能
- byte[] getBytes() ——把字符串转换为字节数组
- char[] toCharArray() ——把字符串转换为字符数组
- static String valueOf(char[] chs) ——把字符数组转换为字符串
- static String valueOf(int i) ——把int类型的数据转为字符串
- String toLowerCase() ——把字符串转成小写,是产生一个新的小写字符串,原本的字符串不变
- String toUpperCase() ——把字符串转成大写,……
- String concat(String str)——把字符串拼接
2.2.6 String类的其他功能
- 替换功能
- String replace(char old,char new)
- String replace(String old,String new)
- 去除字符串两空格(两端)
- String trim()
- 按字典顺序比较两个字符串
- int compareTo(String str)——区分大小写
- int compareToIgnoreCase(String str)——不区分大小写
2.2.7 StringBuffer类的概述和构造方法
概述:对字符串进行拼接操作,每次拼接,都会构建一个新的String对象,既耗时,又浪费空间。而StringBuffer就可以解决这个问题
是线程安全的可变字符串,长度和内容可变,做字符串拼接时不会浪费太多资源;String不可变
构造方法:
- public StringBuffer()——无参构造,初始默认容量为16
- public StringBuffer(int capacity) ——指定容量的字符串缓冲区对象
- public StringBuffer(String str)——指定字符串内容的缓冲区对象
StringBuffer的方法:
- public int capacity()——返回当前容量(理论长度)
- public int length()——返回字符长度(实际长度)
2.2.8 StringBuffer类的添加功能
- public StringBuffer append(String str)
- ——创建对象时,创建的是字符串缓冲区,可以把任意类型数据添加到字符串缓冲区里,并返回字符串缓冲区本身
- public StringBuffer insert(int offset,String str)——在指定位置,把任意类型数据插入到字符缓冲区,并返回缓冲区本身
2.2.9 StringBuffer类的删除功能
- public StringBuffer deleteCharAt(int index) ——删除指定位置的字符,并返回本身
- public StringBuffer delete(int start,int end)——删除指定位置开始指定位置结束的内容,并返回本身(包括start索引,不包括end)
2.2.10 StringBuffer类的替换功能
- public StringBuffer replace(int start,int end,String str)——从start开始到end结束的内容由str替换(包左不包右)
2.2.11 StringBuffer类的反转功能
- public StringBuffer reverse()——字符串反转
2.2.11 StringBuffer类的截取功能
- public String substring(int start)——从指定位置开始到结尾截取字符串
- public String substring(int start,int end)——从指定位置开始到指定位置结束截取字符串(包左不包右)
截取功能和前面几个功能的不同 :返回值类型是String类型,本身没有发生改变
2.2.12 String类和StringBuffer类的转换
String→StringBuffer
不能将字符串直接赋值给StringBuffer
方法1:通过构造方法
StringBuffer sb = new StringBuffer(str);
方法2:通过append()方法
StringBuffer sb2 = new StringBuffer();
sb2.append(str);
StringBuffer→String
方法1:通过构造方法
String str = new String(buffer);
方法2:通过toString()方法
String str2 = buffer.toString();
- String,StringBuffer,StringBuilder的区别
- String是内容不可变的,另外两个可变;
- StringBuffer是同步的,数据安全但效率低,StringBuilder是不同步了,效率高但数据不安全
- StringBuffer和数组的区别
- 都可以看成是容器,装其他类型的数据;
- 但是StringBuffer里存放的数据最终是一个字符串数据
- 而数组可以存放多种类型的数据,只是必须是同一种数据类型
- String作为形式参数传递效果和基本类型作为参数传递是一样的
2.3 数组高级和Arrays类
2.3.1 数组高级(排序和查找)
排序
- 冒泡排序——相邻元素两两比较,大的往后放,第一轮完毕后最大值出现在最大索引处。
- 第一轮比较全部元素,之后每一轮比较的元素比上一轮少1
- 总共需要比较数组长度-1次
//冒泡排序代码
public static void bubbleSort(int[] arr){
for (int x = 0; x < arr.length - 1; x++) {
for (int y = 0; y < arr.length - 1 - x; y++) {
if (arr[y] > arr[y + 1]) {
int temp = arr[y];
arr[y] = arr[y + 1];
arr[y + 1] = temp;
}
}
}
}
- 选择排序——从0索引开始,依次和后面元素比较,小的往前放,第一轮比较完毕,最小值出现在了最小索引处
- 第一次是0索引上的元素与其他索引元素进行比较
- 第二轮是1索引上的元素与其他元素进行比较
- 最后一轮是数组长度-2的元素与数组元素-1的元素进行比较
//选择排序
public static void selectSort(int[] arr){
for(int x=0; x<arr.length-1; x++){
for(int y=x+1; y<arr.length; y++){
if(arr[y] <arr[x]){
int temp = arr[x];
arr[x] = arr[y];
arr[y] = temp;
}
}
}
}
查找
- 基本查找——适用于数组元素无序时,从头找到尾
- 二分查找——适用于数组元素有序时,折半查找即每次都猜中间的那个元素,比较大小,就能淘汰一半元素
//二分查找
public static int getIndex(int[] arr,int value){
//定义最大索引,最小索引
int max = arr.length -1;
int min = 0;
//计算出中间索引
int mid = (max +min)/2;
//拿中间索引的值和要查找的值进行比较
while(arr[mid] != value){
if(arr[mid]>value){
max = mid - 1;
}else if(arr[mid]<value){
min = mid + 1;
}
//加入判断
if(min > max){
return -1;
}
mid = (max +min)/2;
}
return mid;
}
2.3.2 Arrays类的概述及其常用方法
Arrays类概述:针对数组进行操作的工具类, 提供了排序,查找等功能
成员方法 :
- public static String toString(int[] a) ——把数组转成字符串
- public static void sort(int[] a) ——对数组进行排序
- public static int binarySearch(int[] a,int key)——二分查找
2.4 基本类型包装类(Integer,Character)
2.4.1 基本类型包装类的概述
- 为了对基本数据类型进行更多、更方便的操作,Java针对每一种基本类型数据提供了对应的类类型(是引用类型),叫包装类类型
- 基本类型和包装类的对应 Byte,Short,Integer,Long,Float,Double Character,Boolean
- 将基本数据类型封装成对象的好处在于可以在对象中定义更多的功能方法操作该数据。
- 常用的操作之一:用于基本数据类型与字符串之间的转换
2.4.2 Integer类的概述及其构造方法
Integer类概述:
Integer 类在对象中包装了一个基本类型 int 的值,该类提供了多个方法,能在 int 类型和 String 类型之间互相转换,还提供了处理 int 类型时非常有用的其他一些常量和方法
构造方法:
- public Integer(int value) ——将int类型转为integer
- public Integer(String s)——将String类型转为integer,字符串必须由数组字符组成
2.4.3 Integer类的成员方法
int类型和String类型的相互转换
- public static String toString(int i) ——将int类型转换为String类型
- public int intValue() ——以int类型返回integer类型的值
- public static int parseInt(String s) ——将字符串转成int类型
- public static Integer valueOf(int i)——将int类型变为integer类型
- public static Integer valueOf(String s)——将String类型变为integer类型
//int类型和String类型的相互转换
public class IntegerDemo {
public static void main(String[] args) {
// int类型 → String类型
int number = 100;
// 方式1
String s1 = "" + number;
System.out.println("s1:" + s1);
// 方式2
String s2 = String.valueOf(number);
System.out.println("s2:" + s2);
// 方式3
// int -- Integer -- String
Integer i = new Integer(number);
String s3 = i.toString();
System.out.println("s3:" + s3);
// 方式4
// public static String toString(int i)
String s4 = Integer.toString(number);
System.out.println("s4:" + s4);
System.out.println("-----------------");
// String类型 → int类型
String s = "100";
// 方式1
// String -- Integer -- int
Integer ii = new Integer(s);
// public int intValue()
int x = ii.intValue();
System.out.println("x:" + x);
//方式2
//public static int parseInt(String s)
int y = Integer.parseInt(s);
System.out.println("y:"+y);
}
}
常用的基本进制转换
- public static String toBinaryString(int i)——将十进制转为二进制
- public static String toOctalString(int i) ——将十进制转为八进制
- public static String toHexString(int i) ——将十进制转为十六进制
十进制到其他进制
- public static String toString(int i,int radix) ——将十进制转为其他进制(进制的范围:2到36,因为0到9,a到z一共36个)
其他进制到十进制
- public static int parseInt(String s,int radix)——将其他进制转为十进制
2.4.4 Character类的概述及其构造方法
Character类概述:Character 类在对象中包装一个基本类型 char 的值
构造方法:
- public Character(char value)
2.4.5 Character类的成员方法
- public static boolean isUpperCase(char ch) ——判断给定的字符是否是大写字符
- public static boolean isLowerCase(char ch) ——判断给定的字符是否是小写字符
- public static boolean isDigit(char ch) ——判断给定的字符是否是数字字符
- public static char toUpperCase(char ch)——将给定的字符转换为大写字符
- public static char toLowerCase(char ch)——将给定的字符转换为小写字符
2.5 正则表达式(Pattern,Matcher)
2.5.1 正则表达式的概述及基本使用
正则表达式:是指一个用来描述或者匹配一系列符合某个句法规则的字符串的单个字符串。其实就是一种规则。
2.5.2 正则表达式的组成规则
规则字符在java.util.regex Pattern类中
常见组成规则
- 字符 ——任意一个字符代表它本身;\\代表\;\r回车;\n换行
- 字符类——[abc]代表a或b或c;[^abc]除了abc的任何字符;[a-zA-Z]a到z或A到Z;[0-9]0到9
- 预定义字符类——.表示任意字符;\.表示.本身;\d代表数字;\w代表单词字符[a-zA-Z_0-9]
- 边界匹配器—— ^代表行的开头;$代表行的结尾;\b代表单词边界
- 数量词
2.5.3 正则表达式的应用
判断功能
- public boolean matches(String regex)——判断此字符串是否匹配给定的正则表达式
分割功能
- public String[] split(String regex) ——根据给定正则表达式的匹配,拆分字符串
替换功能
- public String replaceAll(String regex,String replacement)
- ——使用给定的replacement替换此字符串所有匹配给定的正则表达式的子字符串
获取功能
- Pattern和Matcher类的使用
2.6 Math类/Random类/System类
2.6.1 Math类的概述及其成员方法
Math类概述:Math 类包含用于执行基本数学运算的方法,如初等指数、对数、平方根和三角函数等
成员变量:
- public static final double PI——圆周率
- public static final double E——自然对数的底数e
成员方法
- public static int abs(int a) ——绝对值
- public static double ceil(double a) ——向上取整
- public static double floor(double a) ——向下取整
- public static int max(int a,int b)——最大值
- public static int min(int a,int b)——最小值
- public static double pow(double a,double b)——返回a的b次幂
- public static double random() ——[0.0,1.0)范围的随机数
- public static int round(float a)——四舍五入
- public static double sqrt(double a)——正平方根
2.6.2 Random类的概述及其构造方法
Random类概述:此类用于产生随机数
如果用相同的种子创建两个 Random 实例(对象),则对每个实例进行相同的方法调用序列,它们将生成并返回相同的数字序列
构造方法
- public Random() ——创建一个新的随机数生成器,使用的默认种子(当前时间的毫秒值,种子不一样)
- public Random(long seed)——使用单格long种子创建一个新的随机数生成器(给出指定种子)
给定种子后,每次得到的随机数是相同的
2.6.3 Random类的成员方法
- public int nextInt() ——返回的是int范围内的随机数
- public int nextInt(int n)——返回的是0到n(包左不包右)之间的随机数
2.6.4 System类的概述及其成员方法
System类概述:System 类包含一些有用的类字段和方法,不能被实例化。
成员方法
- public static void gc() ——运行垃圾回收器
- public static void exit(int status) ——终止当前正在运行的java虚拟机,参数用作状态码,非0的状态码表示异常终止
- public static long currentTimeMillis() ——返回以毫秒为单位的当前时间
- public static void arraycopy(Object src,int srcPos,Object dest,int destPos,int length)
- ——从指定源数组中复制一个数组,复制从指定位置开始,到目标数组的指定位置结束
2.7 BigInteger类/BigDecimal类
2.7.1 BigInteger类的概述及其构造方法
BigInteger类概述:可以让超过Integer范围内的数据进行运算
构造方法
- public BigInteger(String val)
2.7.2 BigInteger类的成员方法
- public BigInteger add(BigInteger val)——加
- public BigInteger subtract(BigInteger val)——减
- public BigInteger multiply(BigInteger val) ——乘
- public BigInteger divide(BigInteger val)——除
- public BigInteger[] divideAndRemainder(BigInteger val)——返回商和余数的数组
2.7.3 BigDecimal类的概述及其构造方法
在运算的时候,float类型和double很容易丢失精度,所以,为了能精确的表示、计算浮点数,Java提供了BigDecimal类
BigDecimal类概述:不可变的、任意精度的有符号十进制数,可以解决精度丢失问题
构造方法
- public BigDecimal(String val)
2.7.4 BigDecimal类的成员方法
- public BigDecimal add(BigDecimal augend) ——加
- public BigDecimal subtract(BigDecimal subtrahend)——减
- public BigDecimal multiply(BigDecimal multiplicand)——乘
- public BigDecimal divide(BigDecimal divisor)——除
- public BigDecimal divide(BigDecimal divisor,int scale,int roundingMode)——除(商,小数位数,如何取舍)
2.8 Date类/DateFormat类/Calendar类
2.8.1 Date类的概述及其方法
Date类概述:类 Date 表示特定的瞬间,精确到毫秒。
构造方法
- public Date() ——根据当前的默认毫秒值创建日期对象
- public Date(long date) ——根据给定的毫秒值创建日期对象
成员方法
- public long getTime() ——获取时间,以毫秒为单位
- public void setTime(long time)——设置时间
2.8.2 DateFormat类的概述及其方法
DateFormat类概述:DateFormat 是日期/时间格式化子类的抽象类,它以与语言无关的方式格式化并解析日期或时间
是抽象类,所以使用其子类SimpleDateFormat
Date→String 格式化
String→Date 解析
SimpleDateFormat的构造方法
- public SimpleDateFormat() ——用默认的模式和默认语言环境的日期格式符号构造SimpleDateFormat
- public SimpleDateFormat(String pattern) ——用给定的模式……
成员方法
- public final String format(Date date)
- public Date parse(String source)
2.8.3 Calendar类的概述及其方法
Calendar类概述:
Calendar 类是一个抽象类,它为特定瞬间与一组诸如 YEAR、MONTH、DAY_OF_MONTH、HOUR 等 日历字段之间的转换提供了一些方法,并为操作日历字段提供了一些方法
成员方法
- public static Calendar getInstance()
- public int get(int field) ——返回给定日历字段的值,日历类中每个日历字段都是静态的成员变量,且是int类型
- public void add(int field,int amount) ——根据日历的规则,为给定的日历字段添加或减去指定的时间量
- public final void set(int year,int month,int date)——设置当前日历的年月日