Java中的System类、BigInteger类和BigDecimal类详解
作者:Neo丶
这篇文章主要介绍了Java中的System类、BigInteger类和BigDecimal类详解,arraycopy()方法,复制数组元素,比较适合底层调用,一般使用Arrays.copyOf()完成复制数组,需要的朋友可以参考下
System类常见方法
1)exit()方法,退出当前程序;
2)arraycopy()方法,复制数组元素,比较适合底层调用,一般使用Arrays.copyOf()完成复制数组;
3)currentTimeMillis()方法,返回当前时间距离1970-1-1的毫秒数
4)gc()方法,运行垃圾回收机制System.gc();
package com.pero.system_;
import java.util.Arrays;
/**
* @author Pero
* @version 1.0
*/
public class System_ {
public static void main(String[] args) {
//exit()方法,退出当前程序;
System.out.println("程序1");
//exit(0),表示程序正常退出
//0表示一个正常的状态
//System.exit(0);
System.out.println("程序2");
//2)arraycopy()方法,复制数组元素,比较适合底层调用,
// 一般使用Arrays.copyOf()完成复制数组;
int[] src = {1,2,3};
int[] dest = new int[3]; //dest当前是{0,0,0}
System.arraycopy(src,0,dest,0,3);
// src – the source array. 源数组(被拷贝的数组)
// srcPos – starting position in the source array. 从源数组的指定索引位置开始拷贝(复制)信息
// dest – the destination array. 目标数组(将源数组数据拷贝到该数组中)
// destPos – starting position in the destination data. 从目标数组的指定索引位置开始拷贝(粘贴)信息
// length – the number of array elements to be copied. 从源数组拷贝数据的个数(长度)
System.out.println("dest="+ Arrays.toString(dest));
//3)currentTimeMillis()方法,返回当前时间距离1970-1-1的毫秒数
System.out.println(System.currentTimeMillis());
}
}BigInteger类和BigDecimal类应用场景
1)BigInteger适合保存比较大的整型;
2)BigDecimal适合保存精度更高的浮点型(小数)。
BigInteger常用方法
package com.pero.bignum_;
import java.math.BigInteger;
/**
* @author Pero
* @version 1.0
*/
public class BigInteger_ {
public static void main(String[] args) {
long longNumber = 1000000000000000001l;
System.out.println(longNumber);
//要以字符串形式传进BigInteger的对象中
BigInteger bigInteger = new BigInteger("99999999999999999999999");
System.out.println(bigInteger);
//1.在对BigInteger进行加减乘除的时候,必须使用对应的方法,不能直接使用运算符(+、-、*、/)
//加法add()
BigInteger bigInteger1 = BigInteger.valueOf(longNumber);
System.out.println(bigInteger1);
BigInteger bigInteger2 = bigInteger.add(bigInteger1);
System.out.println(bigInteger2);
//减法subtract()
BigInteger subtract = bigInteger2.subtract(bigInteger);
System.out.println(subtract);
//乘法multiply()
BigInteger multiply = bigInteger1.multiply(bigInteger2);
System.out.println(multiply);
//除法divide()
BigInteger divide = multiply.divide(bigInteger2);
System.out.println(divide);
}
}BigDecimal常用方法
package com.pero.bignum_;
import java.math.BigDecimal;
/**
* @author Pero
* @version 1.0
*/
public class BigDecimal_ {
public static void main(String[] args) {
double d = 19999999.199999999999999d;
System.out.println(d);
BigDecimal bigDecimal = new BigDecimal("19999999.19999999999999999999");
System.out.println(bigDecimal);
//1.如果对BigDecimal进行运算,不能够直接使用运算符(+、-、*、/)进行运算,
// 需要使用对应方法
//加法add()
BigDecimal bigDecimal1 = new BigDecimal("0.00000000000000000001");
System.out.println(bigDecimal.add(bigDecimal1));
//减法subtract()
BigDecimal bigDecimal2 = new BigDecimal("19999999.09999999999999999999");
System.out.println(bigDecimal.subtract(bigDecimal2));
//乘法multiply()
System.out.println(bigDecimal.multiply(bigDecimal2));
//除法divide()
//System.out.println(bigDecimal.divide(bigDecimal2));
//注意:可能存在无法除尽的情况,抛出ArithmeticException异常
//在调用divide()方法时,指定精度即可,BigDecimal.ROUND_CEILING
//如果有无限循环小数,就会保留分子的精度
System.out.println(bigDecimal.divide(bigDecimal2,BigDecimal.ROUND_CEILING));
}
}到此这篇关于Java中的System类、BigInteger类和BigDecimal类详解的文章就介绍到这了,更多相关System类、BigInteger类和BigDecimal类详解内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
您可能感兴趣的文章:
- Java BigInteger类,BigDecimal类,Date类,DateFormat类及Calendar类用法示例
- JAVA基本类型包装类 BigDecimal BigInteger 的使用
- Java你不了解的大数型BigInteger与BigDecimal类
- Java Big Number操作BigInteger及BigDecimal类详解
- JAVA biginteger类bigdecimal类的使用示例学习
- Java中BigInteger与BigDecimal类用法总结
- java数学类Math BigInteger BigDecimal使用介绍
- JavaAPI中BigInteger、BigDecimal的使用方法及应用
