1 Object类
类层次结构的根类,所有类都直接或者间接的继承自该类。
== :
基本类型:比较的就是值是否相同
引用类型:比较的就是地址值是否相同
equals :
引用类型: 默认情况下比较的是地址值。重写时一般比较对象的成员变量值是否相同。
Object类equals方法源码:
public boolean equals(Object obj) {
return (this == obj);
}
子类重写Object类equals 方法
public class Student implements Cloneable {
private int age;
private String name;
private int getAge() {
return age;
}
private void setAge(int age) {
this.age = age;
}
private String getName() {
return name;
}
private void setName(String name) {
this.name = name;
}
public Student(int age, String name) {
super();
this.age = age;
this.name = name;
}
@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;
}
@Override
protected Object clone() throws CloneNotSupportedException {
// TODO Auto-generated method stub
return super.clone();
}
}
2 Scanner 类
JDK5以后用于获取用户的键盘输入 。hasNextXxx() 判断是否还有下一个输入项,其中Xxx可以是Int,Double等,如果需要判断是否包含下一个字符串,则可以省略Xxx。nextXxx() 获取下一个输入项。
3 String类
字符串是由多个字符组成的一串数据(字符序列),可以看成是字符数组。字符串是常量,它的值在创建之后不能更改。字符串缓冲区支持可变的字符串。
1 字符串拼接内存图解
2 String s=new String("hello")和String s="hello" 内存图解
String 类使用代码示例
public class StringTsst {
public static void main(String[] args) {
String s1=new String();
System.out.println(s1);
System.out.println(s1.length());//0
byte[] bys= {97,98,99,100,101};
String s2=new String(bys);
System.out.println(s2);//abcde
System.out.println(s2.length());//5
char[] chs= {'a','b','c','d','e','我','是'};
String s3=new String(chs);
System.out.println(s3);//abcde我是
System.out.println(s3.length());//7
String s4="hello";
s4 += "world";
System.out.println(s4);//helloworld
String s5=new String("hello");
String s6="hello";
// ==:比较引用类型比较的就是地址值是否相同
System.out.println(s5==s6);//false
// equals:比较引用类型默认比较的是地址值是否相同,但是String类重写了equals方法,
// 比较的是内容是否相同
System.out.println(s5.equals(s6));//true
String s7="world";
String s8="helloworld";
System.out.println(s8 == s6+s7);//false
System.out.println(s8.equals(s6+s7));//true
System.out.println(s8 == "hello"+"world");//true
System.out.println(s8.equals("hello"+"world"));//true
String s9="Hello123world";
int big=0,small=0,num=0;
for (int i = 0; i < s9.length(); i++) {
char ch=s9.charAt(i);
if('A'<= ch && ch<='Z') {
big++;
}else if('a'<= ch && ch<='z') {
small++;
}else if('0'<= ch && ch<='9'){
num++;
}
}
System.out.println("big:"+big+",small:"+small+",num:"+num);//big:1,small:9,num:3
String s10="woaijavawozhenaijavawozhendeaijavawozhendehenaijavaxinbuxinwoaijavagun";
String s11="java";
System.out.println(getCount(s10, s11));//5
}
/**
* 查找较长字符串中包含的较短字符串个数
* @param maxStr 较长字符串
* @param minStr 较短字符串
* @return 个数
*/
public static int getCount(String maxStr,String minStr) {
int count=0;
int index;
while((index=maxStr.indexOf(minStr)) != -1){
count++;
maxStr=maxStr.substring(index+minStr.length());
}
return count;
}
}
4 StringBuffer 类
同步的,线程安全(多线程时)的可变字符序列。
public class StringBufferTest {
public static void main(String[] args) {
//无参构造
StringBuffer sb=new StringBuffer();
System.out.println(sb.capacity());//16
System.out.println(sb.length());//0
// public StringBuffer(int capacity) 指定容量的字符串缓冲区对象
StringBuffer sb2=new StringBuffer(50);
System.out.println(sb2.capacity());//50
System.out.println(sb2.length());//0
// public StringBuffer(String str) 指定字符串内容的字符串缓冲区对象
StringBuffer sb3=new StringBuffer("hello");
System.out.println(sb3.capacity());//21
System.out.println(sb3.length());//5
StringBuffer sb4=new StringBuffer();
sb4.append("hello").append("world").append("java");
sb4.deleteCharAt(1);
System.out.println(sb4);//hlloworldjava
sb4.delete(4, 9);
System.out.println(sb4);//hllojava
sb4.delete(0, sb4.length());
System.out.println(sb4);
sb4.insert(0, "helloworld");
System.out.println(sb4);//helloworld
sb4.replace(2, 5, "hhh");
System.out.println(sb4);//hehhhworld
sb4.reverse();
System.out.println(sb4);//dlrowhhheh
String s=sb4.substring(2, 5);
System.out.println(sb4);//dlrowhhheh
System.out.println(s);//row
String s1="hello";
String s2="world";
System.out.println(s1+"---"+s2);//hello---world
chage(s1,s2);
System.out.println(s1+"---"+s2);//hello---world
StringBuffer sb5=new StringBuffer("hello");
StringBuffer sb6=new StringBuffer("world");
System.out.println(sb5+"---"+sb6);//hello---world
chage(sb5,sb6);
System.out.println(sb5+"---"+sb6);//hello---worldworld
}
private static void chage(StringBuffer sb1, StringBuffer sb2) {
sb1=sb2;
sb2.append(sb1);
}
private static void chage(String s1, String s2) {
s1=s2;
s2=s1+s2;
}
}
5 StringBuilder 类
不同步的,线程不安全(多线程时)的可变字符序列。StringBuffer的一个简易替换,用在字符串缓冲区被单个线程使用时。在大多数实现中,比StringBuffer要快。
6 数组相关算法
1 冒泡排序
相邻元素两两比较,大的往后放,第一次完毕,最大值出现在了最大索引处。
/**
* @param arr 需要排序的数组
* 冒泡排序
*/
private static void bubbleSort(int[] arr) {
for (int i = 0; i < arr.length-1; i++) {
for (int j = 0; j < arr.length-1-i; j++) {
if(arr[j]>arr[j+1]) {
int temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
}
2 选择排序
从0索引开始,依次和后面元素比较,小的往前放,第一次完毕,最小值出现在了最小索引处。
/**
* @param arr 需要排序的数组
* 选择排序
*/
private static void selectSort(int[] arr) {
for (int i = 0; i < arr.length-1; i++) {
for (int j = i+1; j < arr.length; j++) {
if(arr[j]
3 二分查找(数组元素有序)
步骤:
A:定义最大索引,最小索引 :min=0 max=arr.length-1
B:计算出中间索引: mid=(min+max)/2
C:拿中间索引的值和要查找的值进行比较
相等:就返回当前的中间索引
不相等:
大 左边找
小 右边找
D:重新计算出中间索引
大 左边找
max = mid - 1;
小 右边找
min = mid + 1;
E:回到B
/**
* @param arr 排序后的数组
* @param value 需要查找的数值
* 二分查找
*/
private static int binarySearch(int[] arr, int value) {
int min=0,max=arr.length-1;
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;
}
7 包装类
为了对基本数据类型进行更多的操作,更方便的操作,Java就针对每一种基本数据类型提供了对应的类类型。包装类类型。
byte Byte
short Short
int Integer
long Long
float Float
double Double
char Character
boolean Boolean
public class IntegerTest {
public static void main(String[] args) {
System.out.println(Integer.MAX_VALUE);//2147483647
System.out.println(Integer.MIN_VALUE);//-2147483648
// 二进制
System.out.println(Integer.toBinaryString(100));//1100100
// 八进制
System.out.println(Integer.toOctalString(100));//144
// 十六进制
System.out.println(Integer.toHexString(100));//64
// 十进制到其他进制(进制范围2-36)
System.out.println(Integer.toString(100, 10));//100
System.out.println(Integer.toString(100, 2));//1100100
System.out.println(Integer.toString(100, 8));//144
System.out.println(Integer.toString(100, 16));//64
System.out.println(Integer.toString(100, 5));//400
System.out.println(Integer.toString(100, 7));//202
System.out.println(Integer.toString(100, -7));//100
//其他进制到十进制
System.out.println(Integer.parseInt("100", 10));//100
System.out.println(Integer.parseInt("100", 2));//4
System.out.println(Integer.parseInt("100", 8));//64
System.out.println(Integer.parseInt("100", 16));//256
System.out.println(Integer.parseInt("100", 23));//529
//NumberFormatException 二进制只有0和1
//System.out.println(Integer.parseInt("123", 2));
int i=100; String s="100",s1="abc";
Integer it=new Integer(i);
System.out.println(it);//100
Integer its=new Integer(s);
System.out.println(its);//100
//Exception in thread "main" java.lang.NumberFormatException: For input string: "abc"
//Integer its1=new Integer(s1);
//System.out.println(its1);
int i1=Integer.parseInt(s);
System.out.println(i1);//100
Integer i2 = 100;
i2 += 200;
System.out.println("i2:" + i2);
// 通过反编译后的代码
// Integer i2 = Integer.valueOf(100); //自动装箱(把基本类型转换为包装类类型)
// i2 = Integer.valueOf(i2.intValue() + 200); //自动拆箱(把包装类类型转换为基本类型),再自动装箱
// System.out.println((new StringBuilder("i2:")).append(i2).toString());
//Exception in thread "main" java.lang.NullPointerException
//Integer i3 = null;
//i3 += 200;
//System.out.println("i3:" + i3);
Integer i11 = new Integer(127);
Integer i21 = new Integer(127);
System.out.println(i11 == i21);//false
System.out.println(i11.equals(i21));//true
System.out.println("-----------");
Integer i31 = new Integer(128);
Integer i41 = new Integer(128);
System.out.println(i31 == i41);
System.out.println(i31.equals(i41));//false
System.out.println("-----------");//true
Integer i51 = 128;
Integer i61 = 128;
System.out.println(i51 == i61);//false
System.out.println(i51.equals(i61));//true
System.out.println("-----------");
Integer i71 = 127;
Integer i81 = 127;
System.out.println(i71 == i81);//true
System.out.println(i71.equals(i81));//true
// 对于-128到127之间的数据,有一个数据缓冲池,如果数据是该范围内的,每次并不创建新的空间
/*public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}*/
String s2="Hello123world";
int big=0,small=0,num=0;
//Character 类在对象中包装一个基本类型char的值,Character类型的对象包含类型为char的单个字段。
for (int j = 0; j
8 正则表达式
规则字符在java.util.regex Pattern类中
正则表达式的构造摘要
构造 | 匹配 |
---|---|
字符 | |
x | 字符 x |
\\ | 反斜线字符 |
\0n | 带有八进制值 0 的字符 n (0 <= n <= 7) |
\0nn | 带有八进制值 0 的字符 nn (0 <= n <= 7) |
\0mnn | 带有八进制值 0 的字符 mnn(0 <= m <= 3、0 <= n <= 7) |
\xhh | 带有十六进制值 0x 的字符 hh |
\uhhhh | 带有十六进制值 0x 的字符 hhhh |
\t | 制表符 ('\u0009') |
\n | 新行(换行)符 ('\u000A') |
\r | 回车符 ('\u000D') |
\f | 换页符 ('\u000C') |
\a | 报警 (bell) 符 ('\u0007') |
\e | 转义符 ('\u001B') |
\cx | 对应于 x 的控制符 |
字符类 | |
[abc] | a、b 或 c(简单类) |
[^abc] | 任何字符,除了 a、b 或 c(否定) |
[a-zA-Z] | a 到 z 或 A 到 Z,两头的字母包括在内(范围) |
[a-d[m-p]] | a 到 d 或 m 到 p:[a-dm-p](并集) |
[a-z&&[def]] | d、e 或 f(交集) |
[a-z&&[^bc]] | a 到 z,除了 b 和 c:[ad-z](减去) |
[a-z&&[^m-p]] | a 到 z,而非 m 到 p:[a-lq-z](减去) |
预定义字符类 | |
. | 任何字符(与行结束符可能匹配也可能不匹配) |
\d | 数字:[0-9] |
\D | 非数字: [^0-9] |
\s | 空白字符:[ \t\n\x0B\f\r] |
\S | 非空白字符:[^\s] |
\w | 单词字符:[a-zA-Z_0-9] |
\W | 非单词字符:[^\w] |
POSIX 字符类(仅 US-ASCII) | |
\p{Lower} | 小写字母字符:[a-z] |
\p{Upper} | 大写字母字符:[A-Z] |
\p{ASCII} | 所有 ASCII:[\x00-\x7F] |
\p{Alpha} | 字母字符:[\p{Lower}\p{Upper}] |
\p{Digit} | 十进制数字:[0-9] |
\p{Alnum} | 字母数字字符:[\p{Alpha}\p{Digit}] |
\p{Punct} | 标点符号:!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~ |
\p{Graph} | 可见字符:[\p{Alnum}\p{Punct}] |
\p{Print} | 可打印字符:[\p{Graph}\x20] |
\p{Blank} | 空格或制表符:[ \t] |
\p{Cntrl} | 控制字符:[\x00-\x1F\x7F] |
\p{XDigit} | 十六进制数字:[0-9a-fA-F] |
\p{Space} | 空白字符:[ \t\n\x0B\f\r] |
java.lang.Character 类(简单的 java 字符类型) | |
\p{javaLowerCase} | 等效于 java.lang.Character.isLowerCase() |
\p{javaUpperCase} | 等效于 java.lang.Character.isUpperCase() |
\p{javaWhitespace} | 等效于 java.lang.Character.isWhitespace() |
\p{javaMirrored} | 等效于 java.lang.Character.isMirrored() |
Unicode 块和类别的类 | |
\p{InGreek} | Greek 块(简单块)中的字符 |
\p{Lu} | 大写字母(简单类别) |
\p{Sc} | 货币符号 |
\P{InGreek} | 所有字符,Greek 块中的除外(否定) |
[\p{L}&&[^\p{Lu}]] | 所有字母,大写字母除外(减去) |
边界匹配器 | |
^ | 行的开头 |
$ | 行的结尾 |
\b | 单词边界 |
\B | 非单词边界 |
\A | 输入的开头 |
\G | 上一个匹配的结尾 |
\Z | 输入的结尾,仅用于最后的结束符(如果有的话) |
\z | 输入的结尾 |
Greedy 数量词 | |
X? | X,一次或一次也没有 |
X* | X,零次或多次 |
X+ | X,一次或多次 |
X{n} | X,恰好 n 次 |
X{n,} | X,至少 n 次 |
X{n,m} | X,至少 n 次,但是不超过 m 次 |
Reluctant 数量词 | |
X?? | X,一次或一次也没有 |
X*? | X,零次或多次 |
X+? | X,一次或多次 |
X{n}? | X,恰好 n 次 |
X{n,}? | X,至少 n 次 |
X{n,m}? | X,至少 n 次,但是不超过 m 次 |
Possessive 数量词 | |
X?+ | X,一次或一次也没有 |
X*+ | X,零次或多次 |
X++ | X,一次或多次 |
X{n}+ | X,恰好 n 次 |
X{n,}+ | X,至少 n 次 |
X{n,m}+ | X,至少 n 次,但是不超过 m 次 |
Logical 运算符 | |
XY | X 后跟 Y |
X|Y | X 或 Y |
(X) | X,作为捕获组 |
Back 引用 | |
\n | 任何匹配的 nth 捕获组 |
引用 | |
\ | Nothing,但是引用以下字符 |
\Q | Nothing,但是引用所有字符,直到 \E |
\E | Nothing,但是结束从 \Q 开始的引用 |
特殊构造(非捕获) | |
(?:X) | X,作为非捕获组 |
(?idmsux-idmsux) | Nothing,但是将匹配标志i d m s u x on - off |
(?idmsux-idmsux:X) | X,作为带有给定标志 i d m s u x on - off |
的非捕获组 | |
(?=X) | X,通过零宽度的正 lookahead |
(?!X) | X,通过零宽度的负 lookahead |
(?<=X) | X,通过零宽度的正 lookbehind |
(?X) | X,通过零宽度的负 lookbehind |
(?>X) | X,作为独立的非捕获组 |
代码示例
public class PatternTest {
public static void main(String[] args) {
//邮箱规则
//String rules="[a-zA-Z_0-9]+@[a-zA-Z_0-9]{2,6}(\\.[a-zA-Z_0-9]{2,3})+";
String ruleSimple="\\w+@\\w{2,6}(\\.\\w{2,3})+";
//要校验的字符串
String checks="[email protected]";
System.out.println(mailboxCheck(checks,ruleSimple));//true
String spiltest="91 27 46 38 50";
System.out.println(spiltTest(spiltest)); //27 38 46 50 91
String replaces="124ewr342jdgherht445365";
String translations="*";
//String rules="\\d+";
String rules="\\d";
System.out.println(replaceTest(replaces, translations, rules));//***ewr***jdgherht******
//模式和匹配器的典型调用顺序
//把正则表达式编译成模式对象
Pattern p = Pattern.compile("a*b");
//通过模式对象得到匹配器对象,这个时候需要的是被匹配的字符串
Matcher m = p.matcher("aaaaab");
//调用匹配器对象的功能
boolean b = m.matches();
boolean b1 = Pattern.matches("a*b", "aaaaab");
//获取功能:
//获取下面这个字符串中由三个字符组成的单词
//da jia ting wo shuo,jin tian yao xia yu,bu shang wan zi xi,gao xing bu?
//定义字符串
String s = "da jia ting wo shuo,jin tian yao xia yu,bu shang wan zi xi,gao xing bu?";
//规则
String regex = "\\b\\w{3}\\b";
//把规则编译成模式对象
Pattern p1 = Pattern.compile(regex);
// 通过模式对象得到匹配器对象
Matcher m1 = p1.matcher(s);
while(m1.find()) {
System.out.print(m1.group()+" ");//jia jin yao xia wan gao
}
}
/**
* @param check 要校验的字符串
* @param rule 校验规则
* @return 返回值(true/false)
*/
private static boolean mailboxCheck( String check,String rule) {
return check.matches(rule);
}
/**
* @param s 要分割的字符串
* @return 返回结果字符串
* @features:输入字符串:"91 27 46 38 50",实现输出字符串:"27 38 46 50 91"
*
*/
private static String spiltTest( String s) {
String[] splits=s.split(" ");
int[] arr=new int[splits.length];
for (int i = 0; i < arr.length; i++) {
arr[i]=Integer.parseInt(splits[i]);
}
Arrays.sort(arr);
StringBuilder sb=new StringBuilder();
for (int i = 0; i < arr.length; i++) {
sb.append(arr[i]).append(" ");
}
return sb.toString().trim();
}
/**
* @param replaces 要替换的字符串
* @param translations 替换的字符串
* @param rules 替换规则
* @return 返回值
*/
private static String replaceTest( String replaces,String translations,String rules) {
return replaces.replaceAll(rules, translations) ;
}
}
9 Math 类
Math类包含用于执行基本数学运算的方法,如初等指数、对数、平方根和三角函数。
/*
*
* 成员变量:
* public static final double PI
* public static final double 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):最大值 (min自学)
* public static double pow(double a,double b):a的b次幂
* public static double random():随机数 [0.0,1.0)
* public static int round(float a) 四舍五入(参数为double的自学)
* public static double sqrt(double a):正平方根
*/
public class MathTest {
public static void main(String[] args) {
// public static final double PI
System.out.println("PI:" + Math.PI);//PI:3.141592653589793
// public static final double E
System.out.println("E:" + Math.E);//E:2.718281828459045
// public static int abs(int a):绝对值
System.out.println("abs:" + Math.abs(10));//abs:10
System.out.println("abs:" + Math.abs(-10));//abs:10
// public static double ceil(double a):向上取整
System.out.println("ceil:" + Math.ceil(12.34));//ceil:13.0
System.out.println("ceil:" + Math.ceil(12.56));//ceil:13.0
// public static double floor(double a):向下取整
System.out.println("floor:" + Math.floor(12.34));//floor:12.0
System.out.println("floor:" + Math.floor(12.56));//floor:12.0
// public static int max(int a,int b):最大值
System.out.println("max:" + Math.max(12, 23));//max:23
// 需求:我要获取三个数据中的最大值
// 方法的嵌套调用
System.out.println("max:" + Math.max(Math.max(12, 23), 18));//max:23
// 需求:我要获取四个数据中的最大值
System.out.println("max:"
+ Math.max(Math.max(12, 78), Math.max(34, 56)));//max:78
// public static double pow(double a,double b):a的b次幂
System.out.println("pow:" + Math.pow(2, 3));//pow:8.0
// public static double random():随机数 [0.0,1.0)
System.out.println("random:" + Math.random());//random:0.14638098279005374
// 获取一个1-100之间的随机数
System.out.println("random:" + ((int) (Math.random() * 100) + 1));//random:14
// public static int round(float a) 四舍五入
System.out.println("round:" + Math.round(12.34f));//round:12
System.out.println("round:" + Math.round(12.56f));//round:13
//public static double sqrt(double a):正平方根
System.out.println("sqrt:"+Math.sqrt(4));//sqrt:2.0
System.out.println(getRandom(100,200));
}
/**
* @param start 初始值
* @param end 结束值
* @return 两者之间的随机数值
*/
private static int getRandom(int start, int end) {
return (int)(Math.random()*(end-start+1))+start;
}
}
10 Random 类
此类的实例用于生成伪随机数流。
构造方法摘要 | |
---|---|
Random() 创建一个新的随机数生成器。默认是当前时间的毫秒值。 |
|
Random(long seed) 使用单个 long 种子创建一个新的随机数生成器。给定种子后,每次得到的随机数是相同的。 |
方法摘要 | |
---|---|
int |
nextInt() 返回下一个伪随机数,它是此随机数生成器的序列中均匀分布的 int 值。 |
int |
nextInt(int n) 返回一个伪随机数,它是取自此随机数生成器序列的、在 0(包括)和指定值(不包括)之间均匀分布的 int 值。 |
11 System 类
System
类包含一些有用的类字段和方法。它不能被实例化。
字段摘要 | |
---|---|
static PrintStream |
err “标准”错误输出流。 |
static InputStream |
in “标准”输入流。 |
static PrintStream |
out “标准”输出流。 |
方法摘要 | |
---|---|
static void |
|
static long |
currentTimeMillis() 返回以毫秒为单位的当前时间。 |
static void |
exit(int status) 终止当前正在运行的 Java 虚拟机。参数用作状态码;根据惯例,非 0 的状态码表示异常终止。 |
static void |
gc() 运行垃圾回收器。 |
System.gc()可用于垃圾回收。当使用System.gc()回收某个对象所占用的内存之前,通过要求程序调用适当的方法来清理资源。在没有明确指定资源清理的情况下,Java提高了默认机制来清理该对象的资源,就是调用Object类的finalize()方法。finalize()方法的作用是释放一个对象占用的内存空间时,会被JVM调用。而子类重写该方法,就可以清理对象占用的资源,该方法有没有链式调用,所以必须手动实现。
从程序的运行结果可以发现,执行System.gc()前,系统会自动调用finalize()方法清除对象占有的资源,通过super.finalize()方式可以实现从下到上的finalize()方法的调用,即先释放自己的资源,再去释放父类的资源。
12 BigInteger类
可以让超过Integer范围内的数据进行运算
构造方法摘要 | |
---|---|
BigInteger(byte[] val) 将包含 BigInteger 的二进制补码表示形式的 byte 数组转换为 BigInteger。 |
|
BigInteger(int signum, byte[] magnitude) 将 BigInteger 的符号-数量表示形式转换为 BigInteger。 |
|
BigInteger(int bitLength, int certainty, Random rnd) 构造一个随机生成的正 BigInteger,它可能是一个具有指定 bitLength 的素数。 |
|
BigInteger(int numBits, Random rnd) 构造一个随机生成的 BigInteger,它是在 0 到 (2numBits - 1)(包括)范围内均匀分布的值。 |
|
BigInteger(String val) 将 BigInteger 的十进制字符串表示形式转换为 BigInteger。 |
|
BigInteger(String val, int radix) 将指定基数的 BigInteger 的字符串表示形式转换为 BigInteger。 |
方法摘要 | |
---|---|
BigInteger |
add(BigInteger val) 返回其值为 (this + val) 的 BigInteger。 |
BigInteger |
divide(BigInteger val) 返回其值为 (this / val) 的 BigInteger。 |
BigInteger[] |
divideAndRemainder(BigInteger val) 返回包含 (this / val) 后跟 (this % val) 的两个 BigInteger 的数组。 |
BigInteger |
multiply(BigInteger val) 返回其值为 (this * val) 的 BigInteger。 |
BigInteger |
subtract(BigInteger val) 返回其值为 (this - val) 的 BigInteger。 |
13 BigDecimal 类
不可变的、任意精度的有符号十进制数。
构造方法摘要 | |
---|---|
BigDecimal(BigInteger val) 将 BigInteger 转换为 BigDecimal。 |
|
BigDecimal(BigInteger unscaledVal, int scale) 将 BigInteger 非标度值和 int 标度转换为 BigDecimal。 |
|
BigDecimal(BigInteger unscaledVal, int scale, MathContext mc) 将 BigInteger 非标度值和 int 标度转换为 BigDecimal(根据上下文设置进行舍入)。 |
|
BigDecimal(BigInteger val, MathContext mc) 将 BigInteger 转换为 BigDecimal(根据上下文设置进行舍入)。 |
|
BigDecimal(char[] in) 将 BigDecimal 的字符数组表示形式转换为 BigDecimal,接受与 BigDecimal(String) 构造方法相同的字符序列。 |
|
BigDecimal(char[] in, int offset, int len) 将 BigDecimal 的字符数组表示形式转换为 BigDecimal,接受与 BigDecimal(String) 构造方法相同的字符序列,同时允许指定子数组。 |
|
BigDecimal(char[] in, int offset, int len, MathContext mc) 将 BigDecimal 的字符数组表示形式转换为 BigDecimal,接受与 BigDecimal(String) 构造方法相同的字符序列,同时允许指定子数组,并根据上下文设置进行舍入。 |
|
BigDecimal(char[] in, MathContext mc) 将 BigDecimal 的字符数组表示形式转换为 BigDecimal,接受与 BigDecimal(String) 构造方法相同的字符序列(根据上下文设置进行舍入)。 |
|
BigDecimal(double val) 将 double 转换为 BigDecimal,后者是 double 的二进制浮点值准确的十进制表示形式。 |
|
BigDecimal(double val, MathContext mc) 将 double 转换为 BigDecimal(根据上下文设置进行舍入)。 |
|
BigDecimal(int val) 将 int 转换为 BigDecimal。 |
|
BigDecimal(int val, MathContext mc) 将 int 转换为 BigDecimal(根据上下文设置进行舍入)。 |
|
BigDecimal(long val) 将 long 转换为 BigDecimal。 |
|
BigDecimal(long val, MathContext mc) 将 long 转换为 BigDecimal(根据上下文设置进行舍入)。 |
|
BigDecimal(String val) 将 BigDecimal 的字符串表示形式转换为 BigDecimal。 |
|
BigDecimal(String val, MathContext mc) 将 BigDecimal 的字符串表示形式转换为 BigDecimal,接受与 BigDecimal(String) 构造方法相同的字符串(按照上下文设置进行舍入)。 |
方法摘要 | |
---|---|
BigDecimal |
add(BigDecimal augend) 返回一个 BigDecimal,其值为 (this + augend),其标度为 max(this.scale(), augend.scale())。 |
BigDecimal |
add(BigDecimal augend, MathContext mc) 返回其值为 (this + augend) 的 BigDecimal(根据上下文设置进行舍入)。 |
BigDecimal |
divide(BigDecimal divisor) 返回一个 BigDecimal,其值为 (this / divisor),其首选标度为 (this.scale() - divisor.scale());如果无法表示准确的商值(因为它有无穷的十进制扩展),则抛出 ArithmeticException。 |
BigDecimal |
divide(BigDecimal divisor, int roundingMode) 返回一个 BigDecimal,其值为 (this / divisor),其标度为 this.scale()。 |
BigDecimal |
divide(BigDecimal divisor, int scale, int roundingMode) 返回一个 BigDecimal,其值为 (this / divisor),其标度为指定标度。 |
BigDecimal |
divide(BigDecimal divisor, int scale, RoundingMode roundingMode) 返回一个 BigDecimal,其值为 (this / divisor),其标度为指定标度。 |
BigDecimal |
divide(BigDecimal divisor, MathContext mc) 返回其值为 (this / divisor) 的 BigDecimal(根据上下文设置进行舍入)。 |
BigDecimal |
divide(BigDecimal divisor, RoundingMode roundingMode) 返回一个 BigDecimal,其值为 (this / divisor),其标度为 this.scale()。 |
BigDecimal[] |
divideAndRemainder(BigDecimal divisor) 返回由两个元素组成的 BigDecimal 数组,该数组包含 divideToIntegralValue 的结果,后跟对两个操作数计算所得到的 remainder。 |
BigDecimal[] |
divideAndRemainder(BigDecimal divisor, MathContext mc) 返回由两个元素组成的 BigDecimal 数组,该数组包含 divideToIntegralValue 的结果,后跟根据上下文设置对两个操作数进行舍入计算所得到的 remainder 的结果。 |
BigDecimal |
multiply(BigDecimal multiplicand) 返回一个 BigDecimal,其值为 (this × multiplicand),其标度为 (this.scale() + multiplicand.scale())。 |
BigDecimal |
multiply(BigDecimal multiplicand, MathContext mc) 返回其值为 (this × multiplicand) 的 BigDecimal(根据上下文设置进行舍入)。 |
BigDecimal |
subtract(BigDecimal subtrahend) 返回一个 BigDecimal,其值为 (this - subtrahend),其标度为 max(this.scale(), subtrahend.scale())。 |
BigDecimal |
subtract(BigDecimal subtrahend, MathContext mc) 返回其值为 (this - subtrahend) 的 BigDecimal(根据上下文设置进行舍入)。 |
14 Date 类
类 Date
表示特定的瞬间,精确到毫秒。
在类 Date
所有可以接受或返回年、月、日期、小时、分钟和秒值的方法中,将使用下面的表示形式:
- 1900
表示。public class DateTest {
public static void main(String[] args) throws ParseException {
// 创建对象
Date d = new Date();
long time=d.getTime();
System.out.println("time:" + time);//time:1547696057867
System.out.println("time:" +System.currentTimeMillis());//time:1547696057867
d.setTime(1000);
System.out.println("d:" + d);//d:Thu Jan 01 08:00:01 GMT+08:00 1970
// 创建对象
// long time1 = System.currentTimeMillis();
long time1 = 1000 * 60 * 60; // 1小时
Date d2 = new Date(time1);
System.out.println("d2:" + d2);//d2:Thu Jan 01 09:00:00 GMT+08:00 1970
Date d3 = new Date();
SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
String s=sdf.format(d3);
System.out.println(s);
//String -- Date
String str = "2008-08-08 12:12:12";
//在把一个字符串解析为日期的时候,请注意格式必须和给定的字符串格式匹配
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date dd = sdf2.parse(str);
System.out.println(dd);//Fri Aug 08 12:12:12 GMT+08:00 2008
}
}
14 Calendar 类
Calendar
类是一个抽象类,它为特定瞬间与一组诸如 YEAR
、MONTH
、DAY_OF_MONTH
、HOUR
等 日历字段之间的转换提供了一些方法,并为操作日历字段提供了一些方法。
public class CalendarTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
Calendar nowTime=Calendar.getInstance();
int year=nowTime.get(Calendar.YEAR);
int month=nowTime.get(Calendar.MONTH);
int date=nowTime.get(Calendar.DATE);
System.out.println(year + "年" + (month + 1) + "月" + date + "日");
//三年前
nowTime.add(Calendar.YEAR, -3);
// 5年后的10天前
nowTime.add(Calendar.YEAR, 5);
nowTime.add(Calendar.DATE, -10);
nowTime.set(2011, 11, 11);
// 获取年
year = nowTime.get(Calendar.YEAR);
// 获取月
month = nowTime.get(Calendar.MONTH);
// 获取日
date = nowTime.get(Calendar.DATE);
System.out.println(year + "年" + (month + 1) + "月" + date + "日");
System.out.println(getDays(2007,2));;
}
/**
* @param year 任意的年份
* @param month 任意的月份
* @return 获取任意一年的某一月份的天数
*/
private static int getDays(int year,int month) {
Calendar c=Calendar.getInstance();
c.set(year,month,1);
c.add(Calendar.DATE, -1);
return c.get(Calendar.DATE);
}
}