目录
1.控制流程
1.1 条件语句
一个 if 语句包含一个布尔表达式和一条或多条语句。
语法1:
if(布尔表达式) {
//如果布尔表达式为true将执行的语句
}
如果布尔表达式的值为 true,则执行 if 语句中的代码块,否则执行 if 语句块后面的代码。
语法2:
if(布尔表达式){
//如果布尔表达式的值为true
}else{
//如果布尔表达式的值为false
}
语法3:
if(布尔表达式 1){
//如果布尔表达式 1的值为true执行代码
}else if(布尔表达式 2){
//如果布尔表达式 2的值为true执行代码
}else if(布尔表达式 3){
//如果布尔表达式 3的值为true执行代码
}else {
//如果以上布尔表达式都不为true执行代码
}
if 语句后面可以跟 elseif…else 语句,这种语句可以检测到多种可能的情况。
使用 if,else if,else 语句的时候,需要注意下面几点:
- if 语句至多有 1 个 else 语句,else 语句在所有的 elseif 语句之后。
- if 语句可以有若干个 elseif 语句,它们必须在 else 语句之前。
- 一旦其中一个 else if 语句检测为 true,其他的 else if 以及 else 语句都将跳过执行。
语法4
if(布尔表达式 1){
//如果布尔表达式 1的值为true执行代码
if(布尔表达式 2){
如果布尔表达式 2的值为true执行代码
}
}
1.2 switch case 语句
语法:
switch(expression){
case value :
//语句
break; //可选
case value :
//语句
break; //可选
//你可以有任意数量的case语句
default : //可选
//语句
}
switch case 语句有如下规则:
-
switch 语句中的变量类型可以是: byte、short、int 或者 char。从 Java SE 7 开始,switch 支持字符串 String 类型了,同时 case 标签必须为字符串常量或字面量。
-
switch 语句可以拥有多个 case 语句。每个 case 后面跟一个要比较的值和冒号。
-
case 语句中的值的数据类型必须与变量的数据类型相同,而且只能是常量或者字面常量。
-
当变量的值与 case 语句的值相等时,那么 case 语句之后的语句开始执行,直到 break 语句出现才会跳出 switch 语句。
-
当遇到 break 语句时,switch 语句终止。程序跳转到 switch 语句后面的语句执行。case 语句不必须要包含 break 语句。如果没有 break 语句出现,程序会继续执行下一条 case 语句,直到出现 break 语句。
-
switch 语句可以包含一个 default 分支,该分支一般是 switch 语句的最后一个分支(可以在任何位置,但建议在最后一个)。default 在没有 case 语句的值和变量值相等的时候执行。default 分支不需要 break 语句。
switch case 执行时,一定会先进行匹配,匹配成功返回当前 case 的值,再根据是否有 break,判断是否继续输出,或是跳出判断。
1.3 for 循环结构
for循环执行的次数是在执行前就确定的。
语法格式如下:
for(初始化; 布尔表达式; 更新) {
//代码语句
}
关于 for 循环有以下几点说明:
- 最先执行初始化步骤。可以声明一种类型,但可初始化一个或多个循环控制变量,也可以是空语句。
- 然后,检测布尔表达式的值。如果为 true,循环体被执行。如果为false,循环终止,开始执行循环体后面的语句。
- 执行一次循环后,更新循环控制变量。
- 再次检测布尔表达式。循环执行上面的过程。
1.4 增强 for 循环
声明语句:声明新的局部变量,该变量的类型必须和数组元素的类型匹配。其作用域限定在循环语句块,其值与此时数组元素的值相等。
表达式:表达式是要访问的数组名,或者是返回值为数组的方法。
语法格式
for(声明语句 : 表达式)
{
//代码句子
}
1.5 while 循环结构
只要布尔表达式为 true,循环就会一直执行下去。
语法
while( 布尔表达式 ) {
//循环内容
}
1.6 do...while循环结构
对于 while 语句而言,如果不满足条件,则不能进入循环。但有时候我们需要即使不满足条件,也至少执行一次。
do…while 循环和 while 循环相似,不同的是,do…while 循环至少会执行一次。注意:布尔表达式在循环体的后面,所以语句块在检测布尔表达式之前已经执行了。 如果布尔表达式的值为 true,则语句块一直执行,直到布尔表达式的值为 false。
语法
do {
//代码语句
}while(布尔表达式);
1.7 break 关键字
break 主要用在循环语句或者 switch 语句中,用来跳出整个语句块。
break 跳出最里层的循环,并且继续执行该循环下面的语句。
语法
break 的用法很简单,就是循环结构中的一条语句:
break;
//或者
while(布尔表达式){
if(布尔表达式) break;
}
//或者
flag1:
for(){
for(){
if(布尔表达式) break flag1;
}
}
//break flag1 之后执行这里
if(布尔表达式){
//code
}
1.8 continue 关键字
continue 适用于任何循环控制结构中。作用是让程序立刻跳转到下一次循环的迭代。
在 for 循环中,continue 语句使程序立即跳转到更新语句。
在 while 或者 do…while 循环中,程序立即跳转到布尔表达式的判断语句。
语法
continue 就是循环体中一条简单的语句:
continue;
//或者
while(布尔表达式){
if(布尔表达式) continue;
}
//或者
flag1:
for(){
for(){
if(布尔表达式) continue flag1;
}
}
//continue flag1 之后不会执行这里,而是继续从外层for继续执行
if(布尔表达式){
//code
}
2.数组
2.1 声明数组变量
dataType[] arrayRefVar; // 首选的方法
//或
dataType arrayRefVar[]; // 效果相同,但不是首选方法
2.2 创建数组
arrayRefVar = new dataType[arraySize];
上面的语法语句做了两件事:
- 一、使用 dataType[arraySize] 创建了一个数组。
- 二、把新创建的数组的引用赋值给变量 arrayRefVar。
数组变量的声明,和创建数组可以用一条语句完成,如下所示:
dataType[] arrayRefVar = new dataType[arraySize];
//或
dataType[] arrayRefVar = {value0, value1, ..., valuek};
2.3 处理数组
数组的元素类型和数组的大小都是确定的,所以当处理数组元素时候,我们通常使用基本循环或者 foreach 循环。
数组可以作为入参和出参。
2.4 多维数组的动态初始
type arrayName = new type[arraylenght1][arraylenght2];
//或者
String s[][] = new String[2][];
s[0] = new String[2];
s[1] = new String[3];
s[0][0] = new String("Good");
s[0][1] = new String("Luck");
s[1][0] = new String("to");
s[1][1] = new String("you");
s[1][2] = new String("!");
2.5 java.util.Arrays
-
该类包含用于操作数组的各种方法(如排序和搜索)。 该类还包含一个静态工厂,可以将数组视为列表。
Modifier and Type | Method and Description |
---|---|
static <T> List<T> | asList(T... a) 返回由指定数组支持的固定大小的列表。 |
static int | binarySearch(byte[] a, byte key) 使用二进制搜索算法搜索指定值的指定字节数组。 |
static int | binarySearch(byte[] a, int fromIndex, int toIndex, byte key) 使用二进制搜索算法搜索指定值的指定字节数组的范围。 |
static int | binarySearch(char[] a, char key) 使用二进制搜索算法搜索指定数组的指定值。 |
static int | binarySearch(char[] a, int fromIndex, int toIndex, char key) 使用二分搜索算法搜索指定值的指定数组的范围。 |
static int | binarySearch(double[] a, double key) 使用二进制搜索算法搜索指定值的指定数组的双精度值。 |
static int | binarySearch(double[] a, int fromIndex, int toIndex, double key) 使用二分搜索算法搜索指定值的指定数组的双精度范围。 |
static int | binarySearch(float[] a, float key) 使用二叉搜索算法搜索指定数组的浮点数。 |
static int | binarySearch(float[] a, int fromIndex, int toIndex, float key) 使用二分搜索算法搜索指定数组的浮点数范围。 |
static int | binarySearch(int[] a, int key) 使用二叉搜索算法搜索指定的int数组的指定值。 |
static int | binarySearch(int[] a, int fromIndex, int toIndex, int key) 使用二叉搜索算法搜索指定值的指定数组的范围。 |
static int | binarySearch(long[] a, int fromIndex, int toIndex, long key) 使用二分搜索算法搜索指定值的指定数组的范围。 |
static int | binarySearch(long[] a, long key) 使用二进制搜索算法搜索指定数组的指定数组。 |
static int | binarySearch(Object[] a, int fromIndex, int toIndex, Object key) 使用二进制搜索算法搜索指定对象的指定数组的范围。 |
static int | binarySearch(Object[] a, Object key) 使用二叉搜索算法搜索指定对象的指定数组。 |
static int | binarySearch(short[] a, int fromIndex, int toIndex, short key) 使用二进制搜索算法搜索指定值的指定数组的短整型范围。 |
static int | binarySearch(short[] a, short key) 使用二进制搜索算法搜索指定值的指定数组的指定值。 |
static <T> int | binarySearch(T[] a, int fromIndex, int toIndex, T key, Comparator<? super T> c) 使用二进制搜索算法搜索指定对象的指定数组的范围。 |
static <T> int | binarySearch(T[] a, T key, Comparator<? super T> c) 使用二叉搜索算法搜索指定对象的指定数组。 |
static boolean[] | copyOf(boolean[] original, int newLength) 使用 false (如有必要)复制指定的数组,截断或填充,以使副本具有指定的长度。 |
static byte[] | copyOf(byte[] original, int newLength) 复制指定的数组,用零截取或填充(如有必要),以便复制具有指定的长度。 |
static char[] | copyOf(char[] original, int newLength) 复制指定的数组,截断或填充空字符(如有必要),以便复制具有指定的长度。 |
static double[] | copyOf(double[] original, int newLength) 复制指定的数组,用零截取或填充(如有必要),以便复制具有指定的长度。 |
static float[] | copyOf(float[] original, int newLength) 复制指定的数组,用零截取或填充(如有必要),以便复制具有指定的长度。 |
static int[] | copyOf(int[] original, int newLength) 复制指定的数组,用零截取或填充(如有必要),以便复制具有指定的长度。 |
static long[] | copyOf(long[] original, int newLength) 复制指定的数组,用零截取或填充(如有必要),以便复制具有指定的长度。 |
static short[] | copyOf(short[] original, int newLength) 复制指定的数组,用零截取或填充(如有必要),以便复制具有指定的长度。 |
static <T> T[] | copyOf(T[] original, int newLength) 复制指定的数组,用空值截断或填充(如有必要),以便复制具有指定的长度。 |
static <T,U> T[] | copyOf(U[] original, int newLength, 类<? extends T[]> newType) 复制指定的数组,用空值截断或填充(如有必要),以便复制具有指定的长度。 |
static boolean[] | copyOfRange(boolean[] original, int from, int to) 将指定数组的指定范围复制到新数组中。 |
static byte[] | copyOfRange(byte[] original, int from, int to) 将指定数组的指定范围复制到新数组中。 |
static char[] | copyOfRange(char[] original, int from, int to) 将指定数组的指定范围复制到新数组中。 |
static double[] | copyOfRange(double[] original, int from, int to) 将指定数组的指定范围复制到新数组中。 |
static float[] | copyOfRange(float[] original, int from, int to) 将指定数组的指定范围复制到新数组中。 |
static int[] | copyOfRange(int[] original, int from, int to) 将指定数组的指定范围复制到新数组中。 |
static long[] | copyOfRange(long[] original, int from, int to) 将指定数组的指定范围复制到新数组中。 |
static short[] | copyOfRange(short[] original, int from, int to) 将指定数组的指定范围复制到新数组中。 |
static <T> T[] | copyOfRange(T[] original, int from, int to) 将指定数组的指定范围复制到新数组中。 |
static <T,U> T[] | copyOfRange(U[] original, int from, int to, 类<? extends T[]> newType) 将指定数组的指定范围复制到新数组中。 |
static boolean | deepEquals(Object[] a1, Object[] a2) 如果两个指定的数组彼此 深度相等 ,则返回 true 。 |
static int | deepHashCode(Object[] a) 根据指定数组的“深度内容”返回哈希码。 |
static String | deepToString(Object[] a) 返回指定数组的“深度内容”的字符串表示形式。 |
static boolean | equals(boolean[] a, boolean[] a2) 如果两个指定的布尔数组彼此 相等 ,则返回 true 。 |
static boolean | equals(byte[] a, byte[] a2) 如果两个指定的字节数组彼此 相等 ,则返回 true 。 |
static boolean | equals(char[] a, char[] a2) 如果两个指定的字符数组彼此 相等 ,则返回 true 。 |
static boolean | equals(double[] a, double[] a2) 如果两个指定的双精度数组彼此 相等 ,则返回 true 。 |
static boolean | equals(float[] a, float[] a2) 如果两个指定的浮动数组彼此 相等 ,则返回 true 。 |
static boolean | equals(int[] a, int[] a2) 如果两个指定的int数组彼此 相等 ,则返回 true 。 |
static boolean | equals(long[] a, long[] a2) 如果两个指定的longs数组彼此 相等 ,则返回 true 。 |
static boolean | equals(Object[] a, Object[] a2) 如果两个指定的对象数组彼此 相等 ,则返回 true 。 |
static boolean | equals(short[] a, short[] a2) 如果两个指定的短裤阵列彼此 相等 ,则返回 true 。 |
static void | fill(boolean[] a, boolean val) 将指定的布尔值分配给指定的布尔数组的每个元素。 |
static void | fill(boolean[] a, int fromIndex, int toIndex, boolean val) 将指定的布尔值分配给指定数组布尔值的指定范围的每个元素。 |
static void | fill(byte[] a, byte val) 将指定的字节值分配给指定字节数组的每个元素。 |
static void | fill(byte[] a, int fromIndex, int toIndex, byte val) 将指定的字节值分配给指定字节数组的指定范围的每个元素。 |
static void | fill(char[] a, char val) 将指定的char值分配给指定的char数组的每个元素。 |
static void | fill(char[] a, int fromIndex, int toIndex, char val) 将指定的char值分配给指定的char数组的指定范围的每个元素。 |
static void | fill(double[] a, double val) 将指定的double值分配给指定的双精度数组的每个元素。 |
static void | fill(double[] a, int fromIndex, int toIndex, double val) 将指定的double值分配给指定的双精度数组范围的每个元素。 |
static void | fill(float[] a, float val) 将指定的float值分配给指定的浮点数组的每个元素。 |
static void | fill(float[] a, int fromIndex, int toIndex, float val) 将指定的浮点值分配给指定的浮点数组的指定范围的每个元素。 |
static void | fill(int[] a, int val) 将指定的int值分配给指定的int数组的每个元素。 |
static void | fill(int[] a, int fromIndex, int toIndex, int val) 将指定的int值分配给指定的int数组的指定范围的每个元素。 |
static void | fill(long[] a, int fromIndex, int toIndex, long val) 将指定的long值分配给指定的longs数组的指定范围的每个元素。 |
static void | fill(long[] a, long val) 将指定的long值分配给指定的longs数组的每个元素。 |
static void | fill(Object[] a, int fromIndex, int toIndex, Object val) 将指定的对象引用分配给指定的对象数组的指定范围的每个元素。 |
static void | fill(Object[] a, Object val) 将指定的对象引用分配给指定的对象数组的每个元素。 |
static void | fill(short[] a, int fromIndex, int toIndex, short val) 将指定的短值分配给指定的短裤数组的指定范围的每个元素。 |
static void | fill(short[] a, short val) 将指定的短值分配给指定的短裤数组的每个元素。 |
static int | hashCode(boolean[] a) 根据指定数组的内容返回哈希码。 |
static int | hashCode(byte[] a) 根据指定数组的内容返回哈希码。 |
static int | hashCode(char[] a) 根据指定数组的内容返回哈希码。 |
static int | hashCode(double[] a) 根据指定数组的内容返回哈希码。 |
static int | hashCode(float[] a) 根据指定数组的内容返回哈希码。 |
static int | hashCode(int[] a) 根据指定数组的内容返回哈希码。 |
static int | hashCode(long[] a) 根据指定数组的内容返回哈希码。 |
static int | hashCode(Object[] a) 根据指定数组的内容返回哈希码。 |
static int | hashCode(short[] a) 根据指定数组的内容返回哈希码。 |
static void | parallelPrefix(double[] array, DoubleBinaryOperator op) 使用提供的功能,并行地计算给定阵列的每个元素。 |
static void | parallelPrefix(double[] array, int fromIndex, int toIndex, DoubleBinaryOperator op) 对于数组的给定子范围执行 |
static void | parallelPrefix(int[] array, IntBinaryOperator op) 使用提供的功能,并行地计算给定阵列的每个元素。 |
static void | parallelPrefix(int[] array, int fromIndex, int toIndex, IntBinaryOperator op) 对于数组的给定子范围执行 |
static void | parallelPrefix(long[] array, int fromIndex, int toIndex, LongBinaryOperator op) 对于数组的给定子范围执行 |
static void | parallelPrefix(long[] array, LongBinaryOperator op) 使用提供的功能,并行地计算给定阵列的每个元素。 |
static <T> void | parallelPrefix(T[] array, BinaryOperator<T> op) 使用提供的功能,并行地计算给定阵列的每个元素。 |
static <T> void | parallelPrefix(T[] array, int fromIndex, int toIndex, BinaryOperator<T> op) 对于数组的给定子范围执行 |
static void | parallelSetAll(double[] array, IntToDoubleFunction generator) 使用提供的生成函数来并行设置指定数组的所有元素来计算每个元素。 |
static void | parallelSetAll(int[] array, IntUnaryOperator generator) 使用提供的生成函数来并行设置指定数组的所有元素来计算每个元素。 |
static void | parallelSetAll(long[] array, IntToLongFunction generator) 使用提供的生成函数来并行设置指定数组的所有元素来计算每个元素。 |
static <T> void | parallelSetAll(T[] array, IntFunction<? extends T> generator) 使用提供的生成函数来并行设置指定数组的所有元素来计算每个元素。 |
static void | parallelSort(byte[] a) 按照数字顺序排列指定的数组。 |
static void | parallelSort(byte[] a, int fromIndex, int toIndex) 按照数字顺序排列数组的指定范围。 |
static void | parallelSort(char[] a) 按照数字顺序排列指定的数组。 |
static void | parallelSort(char[] a, int fromIndex, int toIndex) 按照数字顺序排列数组的指定范围。 |
static void | parallelSort(double[] a) 按照数字顺序排列指定的数组。 |
static void | parallelSort(double[] a, int fromIndex, int toIndex) 按照数字顺序排列数组的指定范围。 |
static void | parallelSort(float[] a) 按照数字顺序排列指定的数组。 |
static void | parallelSort(float[] a, int fromIndex, int toIndex) 按照数字顺序排列数组的指定范围。 |
static void | parallelSort(int[] a) 按照数字顺序排列指定的数组。 |
static void | parallelSort(int[] a, int fromIndex, int toIndex) 按照数字顺序排列数组的指定范围。 |
static void | parallelSort(long[] a) 按照数字顺序排列指定的数组。 |
static void | parallelSort(long[] a, int fromIndex, int toIndex) 按照数字顺序排列数组的指定范围。 |
static void | parallelSort(short[] a) 按照数字顺序排列指定的数组。 |
static void | parallelSort(short[] a, int fromIndex, int toIndex) 按照数字顺序排列数组的指定范围。 |
static <T extends Comparable<? super T>> | parallelSort(T[] a) 对指定对象升序排列的阵列,根据natural ordering的元素。 |
static <T> void | parallelSort(T[] a, Comparator<? super T> cmp) 根据指定的比较器引发的顺序对指定的对象数组进行排序。 |
static <T extends Comparable<? super T>> | parallelSort(T[] a, int fromIndex, int toIndex) 对指定对象升序排列的数组的指定范围内,根据natural ordering的元素。 |
static <T> void | parallelSort(T[] a, int fromIndex, int toIndex, Comparator<? super T> cmp) 根据指定的比较器引发的顺序对指定的对象数组的指定范围进行排序。 |
static void | setAll(double[] array, IntToDoubleFunction generator) 使用提供的生成函数来计算每个元素,设置指定数组的所有元素。 |
static void | setAll(int[] array, IntUnaryOperator generator) 使用提供的生成函数来计算每个元素,设置指定数组的所有元素。 |
static void | setAll(long[] array, IntToLongFunction generator) 使用提供的生成函数来计算每个元素,设置指定数组的所有元素。 |
static <T> void | setAll(T[] array, IntFunction<? extends T> generator) 使用提供的生成函数来计算每个元素,设置指定数组的所有元素。 |
static void | sort(byte[] a) 按照数字顺序排列指定的数组。 |
static void | sort(byte[] a, int fromIndex, int toIndex) 按升序排列数组的指定范围。 |
static void | sort(char[] a) 按照数字顺序排列指定的数组。 |
static void | sort(char[] a, int fromIndex, int toIndex) 按升序排列数组的指定范围。 |
static void | sort(double[] a) 按照数字顺序排列指定的数组。 |
static void | sort(double[] a, int fromIndex, int toIndex) 按升序排列数组的指定范围。 |
static void | sort(float[] a) 按照数字顺序排列指定的数组。 |
static void | sort(float[] a, int fromIndex, int toIndex) 按升序排列数组的指定范围。 |
static void | sort(int[] a) 按照数字顺序排列指定的数组。 |
static void | sort(int[] a, int fromIndex, int toIndex) 按升序排列数组的指定范围。 |
static void | sort(long[] a) 按照数字顺序排列指定的数组。 |
static void | sort(long[] a, int fromIndex, int toIndex) 按升序排列数组的指定范围。 |
static void | sort(Object[] a) 对指定对象升序排列的阵列,根据natural ordering的元素。 |
static void | sort(Object[] a, int fromIndex, int toIndex) 对指定对象升序排列的数组的指定范围内,根据natural ordering的元素。 |
static void | sort(short[] a) 按照数字顺序排列指定的数组。 |
static void | sort(short[] a, int fromIndex, int toIndex) 按升序排列数组的指定范围。 |
static <T> void | sort(T[] a, Comparator<? super T> c) 根据指定的比较器引发的顺序对指定的对象数组进行排序。 |
static <T> void | sort(T[] a, int fromIndex, int toIndex, Comparator<? super T> c) 根据指定的比较器引发的顺序对指定的对象数组的指定范围进行排序。 |
static Spliterator.OfDouble | spliterator(double[] array) 返回 |
static Spliterator.OfDouble | spliterator(double[] array, int startInclusive, int endExclusive) 返回 |
static Spliterator.OfInt | spliterator(int[] array) 返回 |
static Spliterator.OfInt | spliterator(int[] array, int startInclusive, int endExclusive) 返回 |
static Spliterator.OfLong | spliterator(long[] array) 返回 |
static Spliterator.OfLong | spliterator(long[] array, int startInclusive, int endExclusive) 返回 |
static <T> Spliterator<T> | spliterator(T[] array) 返回 |
static <T> Spliterator<T> | spliterator(T[] array, int startInclusive, int endExclusive) 返回 |
static DoubleStream | stream(double[] array) 返回顺序 |
static DoubleStream | stream(double[] array, int startInclusive, int endExclusive) 返回顺序 |
static IntStream | stream(int[] array) 返回顺序 |
static IntStream | stream(int[] array, int startInclusive, int endExclusive) 返回顺序 |
static LongStream | stream(long[] array) 返回顺序 |
static LongStream | stream(long[] array, int startInclusive, int endExclusive) 返回顺序 |
static <T> Stream<T> | stream(T[] array) 返回顺序 |
static <T> Stream<T> | stream(T[] array, int startInclusive, int endExclusive) 返回顺序 |
static String | toString(boolean[] a) 返回指定数组的内容的字符串表示形式。 |
static String | toString(byte[] a) 返回指定数组的内容的字符串表示形式。 |
static String | toString(char[] a) 返回指定数组的内容的字符串表示形式。 |
static String | toString(double[] a) 返回指定数组的内容的字符串表示形式。 |
static String | toString(float[] a) 返回指定数组的内容的字符串表示形式。 |
static String | toString(int[] a) 返回指定数组的内容的字符串表示形式。 |
static String | toString(long[] a) 返回指定数组的内容的字符串表示形式。 |
static String | toString(Object[] a) 返回指定数组的内容的字符串表示形式。 |
static String | toString(short[] a) 返回指定数组的内容的字符串表示形式。 |
3.大数值
3.1 java.math.BigDecimal
- 不变的,任意精度的带符号的十进制数字。 A
BigDecimal
由任意精度整数未缩放值和32位整数级别组成 。 如果为零或正数,则刻度是小数点右侧的位数。 如果是负数,则数字的非标定值乘以10,以达到等级的否定的幂。 因此,BigDecimal所代表的BigDecimal
值为(unscaledValue × 10-scale) 。BigDecimal
类提供了算术,缩放操作,舍入,比较,散列和格式转换的操作。toString()
方法提供了一个BigDecimal的规范BigDecimal
。BigDecimal
类使用户完全控制舍入行为。 如果未指定舍入模式,并且无法表示确切的结果,则抛出异常; 否则,可以通过向操作提供适当的MathContext
对象来进行计算,以选择精度和舍入模式。 在这两种情况下,都提供了八种舍入方式来控制舍入。 使用此类中的整数字段(如ROUND_HALF_UP
)表示舍入模式已经过时了; 应该使用RoundingMode
枚举
(如RoundingMode.HALF_UP
)的枚举值。
域
Modifier and Type | Field and Description |
---|---|
static BigDecimal | ONE 值1,标度为0。 |
static int | ROUND_CEILING 圆形模式向正无穷大转弯。 |
static int | ROUND_DOWN 舍入模式向零舍入。 |
static int | ROUND_FLOOR 舍入模式向负无穷大转弯。 |
static int | ROUND_HALF_DOWN 四舍五入模式向“最近邻居”转弯,除非这两个邻居都是等距离的,在这种情况下,这是倒圆的。 |
static int | ROUND_HALF_EVEN 四舍五入模式向“最近邻居”转弯,除非两个邻居都是等距离的,在这种情况下,向着邻居方向转移。 |
static int | ROUND_HALF_UP 四舍五入模式向“最近邻居”转弯,除非两个邻居都是等距的,在这种情况下是圆括弧的。 |
static int | ROUND_UNNECESSARY 舍入模式来确定所请求的操作具有精确的结果,因此不需要舍入。 |
static int | ROUND_UP 舍入模式从零开始。 |
static BigDecimal | TEN 值为10,标度为0。 |
static BigDecimal | ZERO 值为0,标度为0。 |
构造方法
Constructor and Description |
---|
BigDecimal(BigInteger val) 将 |
BigDecimal(BigInteger unscaledVal, int scale) 将BigInteger的 |
BigDecimal(BigInteger unscaledVal, int scale, MathContext mc) 将 |
BigDecimal(BigInteger val, MathContext mc) 根据上下文设置将 |
BigDecimal(char[] in) 一个转换的字符数组表示 |
BigDecimal(char[] in, int offset, int len) 一个转换的字符数组表示 |
BigDecimal(char[] in, int offset, int len, MathContext mc) 一个转换的字符数组表示 |
BigDecimal(char[] in, MathContext mc) 一个转换的字符数组表示 |
BigDecimal(double val) 将 |
BigDecimal(double val, MathContext mc) 将 |
BigDecimal(int val) 将 |
BigDecimal(int val, MathContext mc) 将 |
BigDecimal(long val) 将 |
BigDecimal(long val, MathContext mc) 将 |
BigDecimal(String val) 将BigDecimal的字符串表示 |
BigDecimal(String val, MathContext mc) 一个转换的字符串表示 |
方法
Modifier and Type | Method and Description |
---|---|
BigDecimal | abs() 返回一个 |
BigDecimal | abs(MathContext mc) 返回一个 |
BigDecimal | add(BigDecimal augend) 返回 |
BigDecimal | add(BigDecimal augend, MathContext mc) 返回 |
byte | byteValueExact() 将此 |
int | compareTo(BigDecimal val) 将此 |
BigDecimal | divide(BigDecimal divisor) 返回 |
BigDecimal | divide(BigDecimal divisor, int roundingMode) 返回 |
BigDecimal | divide(BigDecimal divisor, int scale, int roundingMode) 返回一个 |
BigDecimal | divide(BigDecimal divisor, int scale, RoundingMode roundingMode) 返回一个 |
BigDecimal | divide(BigDecimal divisor, MathContext mc) 返回 |
BigDecimal | divide(BigDecimal divisor, RoundingMode roundingMode) 返回 |
BigDecimal[] | divideAndRemainder(BigDecimal divisor) 返回一个两元件 |
BigDecimal[] | divideAndRemainder(BigDecimal divisor, MathContext mc) 返回一个两元件 |
BigDecimal | divideToIntegralValue(BigDecimal divisor) 返回一个 |
BigDecimal | divideToIntegralValue(BigDecimal divisor, MathContext mc) 返回值为 |
double | doubleValue() 将此 |
boolean | equals(Object x) 将此 |
float | floatValue() 将此 |
int | hashCode() 返回此 |
int | intValue() 将此 |
int | intValueExact() 将此 |
long | longValue() 将此 |
long | longValueExact() 将此 |
BigDecimal | max(BigDecimal val) 返回此 |
BigDecimal | min(BigDecimal val) 返回此 |
BigDecimal | movePointLeft(int n) 返回一个 |
BigDecimal | movePointRight(int n) 返回一个 |
BigDecimal | multiply(BigDecimal multiplicand) 返回 |
BigDecimal | multiply(BigDecimal multiplicand, MathContext mc) 返回 |
BigDecimal | negate() 返回 |
BigDecimal | negate(MathContext mc) 返回 |
BigDecimal | plus() 返回 |
BigDecimal | plus(MathContext mc) 返回 |
BigDecimal | pow(int n) 返回 |
BigDecimal | pow(int n, MathContext mc) 返回 |
int | precision() 返回此 BigDecimal的 |
BigDecimal | remainder(BigDecimal divisor) 返回 |
BigDecimal | remainder(BigDecimal divisor, MathContext mc) 返回 |
BigDecimal | round(MathContext mc) 返回 |
int | scale() 返回此 规模 |
BigDecimal | scaleByPowerOfTen(int n) 返回一个BigDecimal,其数值等于( |
BigDecimal | setScale(int newScale) 返回一个 |
BigDecimal | setScale(int newScale, int roundingMode) 返回一个 |
BigDecimal | setScale(int newScale, RoundingMode roundingMode) 返回一个 |
short | shortValueExact() 将此 |
int | signum() 返回这个 |
BigDecimal | stripTrailingZeros() 返回一个 |
BigDecimal | subtract(BigDecimal subtrahend) 返回 |
BigDecimal | subtract(BigDecimal subtrahend, MathContext mc) 返回 |
BigInteger | toBigInteger() 将此 |
BigInteger | toBigIntegerExact() 将此 |
String | toEngineeringString() 如果需要指数,则使用工程符号返回此 |
String | toPlainString() 返回没有指数字段的此 |
String | toString() 返回此 |
BigDecimal | ulp() 返回此 |
BigInteger | unscaledValue() 返回一个 |
static BigDecimal | valueOf(double val) 转换一个 |
static BigDecimal | valueOf(long val) 将 |
static BigDecimal | valueOf(long unscaledVal, int scale) 将 |
3.2 java.math.BigInteger
-
不可变的任意精度整数。 所有操作的行为就好像BigIntegers以二进制补码表示(如Java的原始整数类型)表示。 BigInteger提供了所有Java的原始整数运算符和java.lang.Math中所有相关方法的类比。 此外,BigInteger还提供了模数运算,GCD计算,原始测试,初级生成,位操作以及其他一些其他操作的操作。
域
Modifier and Type | Field and Description |
---|---|
static BigInteger | ONE BigInteger常数。 |
static BigInteger | TEN BigInteger常数十。 |
static BigInteger | ZERO BigInteger常数为零。 |
构造方法
Constructor and Description |
---|
BigInteger(byte[] val) 将包含BigInteger的二进制补码二进制表达式的字节数组转换为BigInteger。 |
BigInteger(int signum, byte[] magnitude) 将BigInteger的符号大小表示形式转换为BigInteger。 |
BigInteger(int bitLength, int certainty, Random rnd) 构造一个随机生成的正BigInteger,它可能是素数,具有指定的bitLength。 |
BigInteger(int numBits, Random rnd) 构造一个随机生成的BigInteger,均匀分布在0到(2 |
BigInteger(String val) 将BigInteger的十进制字符串表示形式转换为BigInteger。 |
BigInteger(String val, int radix) 将指定基数中的BigInteger的String表示形式转换为BigInteger。 |
方法
Modifier and Type | Method and Description |
---|---|
BigInteger | abs() 返回一个BigInteger,它的值是此BigInteger的绝对值。 |
BigInteger | add(BigInteger val) 返回值为 |
BigInteger | and(BigInteger val) 返回值为 |
BigInteger | andNot(BigInteger val) 返回值为 |
int | bitCount() 返回与其符号位不同的BigInteger的二进制补码表示中的位数。 |
int | bitLength() 返回此BigInteger的最小二进制补码表示中的位数, 不包括符号位。 |
byte | byteValueExact() 将此 |
BigInteger | clearBit(int n) 返回一个BigInteger,其值等于此BigInteger,指定的位被清零。 |
int | compareTo(BigInteger val) 将此BigInteger与指定的BigInteger进行比较。 |
BigInteger | divide(BigInteger val) 返回值为 |
BigInteger[] | divideAndRemainder(BigInteger val) 返回两个BigInteger的数组,其中包含 |
double | doubleValue() 将此BigInteger转换为 |
boolean | equals(Object x) 将此BigInteger与指定的对象进行比较以实现相等。 |
BigInteger | flipBit(int n) 返回一个BigInteger,其值等于此BigInteger,指定的位被翻转。 |
float | floatValue() 将此BigInteger转换为 |
BigInteger | gcd(BigInteger val) 返回一个BigInteger,其值是 |
int | getLowestSetBit() 返回此BigInteger中最右(最低位)一位的索引(最右边一位右侧的零位数)。 |
int | hashCode() 返回此BigInteger的哈希码。 |
int | intValue() 将此BigInteger转换为 |
int | intValueExact() 将此 |
boolean | isProbablePrime(int certainty) 返回 |
long | longValue() 将此BigInteger转换为 |
long | longValueExact() 将此 |
BigInteger | max(BigInteger val) 返回此BigInteger和 |
BigInteger | min(BigInteger val) 返回此BigInteger和 |
BigInteger | mod(BigInteger m) 返回值为 |
BigInteger | modInverse(BigInteger m) 返回值为 |
BigInteger | modPow(BigInteger exponent, BigInteger m) 返回值为 (thisexponent mod m)的BigInteger 。 |
BigInteger | multiply(BigInteger val) 返回值为 |
BigInteger | negate() 返回值为 |
BigInteger | nextProbablePrime() 返回大于这个 |
BigInteger | not() 返回值为 |
BigInteger | or(BigInteger val) 返回值为 |
BigInteger | pow(int exponent) 返回值为 (thisexponent)的BigInteger 。 |
static BigInteger | probablePrime(int bitLength, Random rnd) 返回一个正的BigInteger,它可能是素数,具有指定的位长度。 |
BigInteger | remainder(BigInteger val) 返回值为 |
BigInteger | setBit(int n) 返回一个BigInteger,其值等于具有指定位集合的BigInteger。 |
BigInteger | shiftLeft(int n) 返回值为 |
BigInteger | shiftRight(int n) 返回值为 |
short | shortValueExact() 将此 |
int | signum() 返回此BigInteger的signum函数。 |
BigInteger | subtract(BigInteger val) 返回值为 |
boolean | testBit(int n) 返回 |
byte[] | toByteArray() 返回一个包含此BigInteger的二进制补码表示的字节数组。 |
String | toString() 返回此BigInteger的十进制字符串表示形式。 |
String | toString(int radix) 返回给定基数中BigInteger的String表示形式。 |
static BigInteger | valueOf(long val) 返回一个BigInteger,其值等于指定的 |
BigInteger | xor(BigInteger val) 返回值为 |