文章目录
String
String创建
import org.junit.Test;
/**
* @Author: mei_ming
* @DateTime: 2022/6/11 20:55
* @Description: String类详解
*/
public class StringTest {
/**
* String : 字符串,使用一对""引起来表示
* 1. String 声明位final,不可被继承
* 2. String 实现了Serializable接口:表示字符串支持序列化
* 实现了Comparable接口:表示String可以比较大小
* 3. String 内部定义了final char[] value 用于存储字符串数据
* 4. String代表不可变的字符序列,简称 不可变性。
* 体现 1. 当对字符重新赋值时,需要重写指定内存区域赋值,不能使用原有的value进行赋值
* 2. 当对字符进行连接操作时,也需要重新指定内存地址
* 3. 当调用replace方法时,也需要重新指定内存地址
* 5. 通过字面量的方式赋值,声明在字符串常量池中
* 6. 字符串常量池中不会存储相同的内容的字符串
*/
/**
* String 实例化
* 方式1: 通过字面量定义
* 方式2: 通过new 构造器的方式
*/
@Test
public void test() {
//字面量方式,s1,s2的数据存储在方法区的字符串常量池中
String s1 = "abc";
String s2 = "abc";
//new 方式: s3,s4的数据存储在堆中
String s3 = new String("abc");
String s4 = new String("abc");
System.out.println(s1 == s2); //true
System.out.println(s1 == s3); //false
System.out.println(s1 == s4); //false
System.out.println(s3 == s4); //false
}
/**
* String 拼接
* 1. 常量与常量的拼接结果在常量池,且常量池中不会存在相同内容的常量
* 2. 只要其中有一个是变量,结果就在堆中
* 3. 如果拼接的结果调用intern方法,返回值也就在常量池中
*/
@Test
public void test2() {
String s1 = "hello";
String s2 = "java";
String s3 = "hellojava";
String s4 = "hello" + "java";
String s5 = s1 + "java";
String s6 = "hello" + s2;
String s7 = s1 + s2;
String s8 = s7.intern();
final String s9 = "hello"; //被final修饰为常量 存在字符串池中
String s10 = s9 + "java";
System.out.println(s3 == s4); //true
System.out.println(s3 == s5); //false
System.out.println(s3 == s6); //false
System.out.println(s3 == s7); //false
System.out.println(s5 == s6); //false
System.out.println(s6 == s7); //false
System.out.println(s5 == s7); //false
System.out.println(s3 == s8); //true
System.out.println(s3 == s10); //true
}
}
String中常用方法
import org.junit.Test;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
/**
* @Author: mei_ming
* @DateTime: 2022/6/12 13:51
* @Description: String类常用方法
*/
public class StringMethod {
/**
* int length() : 返回字符串的长度 return value.length
* char charAt(int index) : 返回某索引处的字符 return value[index]
* boolean isEmpty() : 判断是否为空字符串 return value.length == 0
* String toLowerCase() : 使用默认语言环境,将String中的所有字符转换为小写
* String toUpperCase() : 使用默认语言环境,将String中的所有字符转换为大写
* String trim() : 去掉前后空格,返回字符串的副本
* boolean equals(Object obj) : 比较字符串的内容是否相同
* boolean equalsIgnoreCase(Object obj) : 比较字符串的内容是否相同,忽略大小写
* String concat(String str) : 将指定字符串连接到此字符串的结尾。等价于 +
* int compareTo(String anotherString) : 比较两个字符串的大小
* String substring(int beginIndex) : 返回一个新的字符串,从字符串beginIndex开始截取
* String substring(int beginIndex,int endIndex) : 字符串截取 ,[beginIndex,endIndex)
*/
@Test
public void test() {
String s1 = "Hello";
String s2 = "hello";
System.out.println(s1.equals(s2)); //false
System.out.println(s1.equalsIgnoreCase(s2)); //true
String s3 = "abc";
String s4 = s3.concat("def");
System.out.println(s4); //"abcdef"
String s5 = new String("ade");
System.out.println(s3.compareTo(s5)); //-2
String s6 = s4.substring(0);
String s7 = s4.substring(0, 3);
System.out.println(s6); //abcdef
System.out.println(s7); //abc
}
/**
* boolean endsWith(String suffix) : 判断字符串是否以suffix结尾
* boolean startsWith(String prefix) : 判断字符串是否以prefix开头
* boolean startsWith(String prefix,int toffset) : 判断字符串是否以prefix开头,(从索引为toffset的位置开始)
* boolean contains(CharSequence s) : 当且仅当此字符串包含指定的char值,返回true
* int indexOf(String str) : 返回指定子字符串在此字符串中第一次出现的位置
* int indexOf(String str,int fromIndex) : 返回指定子字符串在此字符串中第一次出现的位置,(从索引为fromIndex的位置开始)
* int lastIndexOf(String str) : 返回指定子字符串在此字符串中最后出现的位置
* int lastIndexOf(String str,int fromIndex) : 返回指定子字符串在此字符串中最后出现的位置,(从索引为fromIndex的位置开始)
*/
@Test
public void test2() {
String s1 = "hi,ming";
System.out.println(s1.endsWith("ing")); //true
System.out.println(s1.startsWith("hi")); //true
System.out.println(s1.startsWith("hi", 2)); //false
System.out.println(s1.startsWith("mi", 3)); //true
System.out.println(s1.contains("mi")); //true
System.out.println(s1.indexOf("mim")); //-1
System.out.println(s1.indexOf("i", 3)); //4
System.out.println(s1.lastIndexOf("mim")); //-1
System.out.println(s1.lastIndexOf("i", 3)); //1
}
/**
* String replace(char oldChar,char newChar) : 使用newChar替换后,返回字符串 一个字符
* String replace(CharSequence target,CharSequence replacement) : 使用指定字面量替换后,返回字符串 多个字符
* String replaceAll(String regex,String replacement) : 替换全部,返回字符串
* String replaceFirst(String regex,String replacement) : 替换第一个,返回字符串
* boolean matches(String regex) : 判断字符串是否匹配给定的正则表达式
* String[] split(String regex) : 根据给定的正则表达式匹配此字符串
* String[] split(String regex,int limit) : 根据给定的正则表达式匹配此字符串,最多不超过limit个
*/
@Test
public void test3() {
String s1 = "我爱你,i love you";
String s2 = s1.replace('i', 'u');
String s3 = s1.replace("love", "like");
System.out.println(s2); //我爱你,u love you
System.out.println(s3); //我爱你,i like you
s1="i love you";
boolean b = s1.matches("[a-z ]+");
System.out.println(b); //true
String[] split = s1.split("\\s");
for (int i = 0; i <split.length ; i++) {
System.out.println(split[i]); //i love you
}
}
/**
* 字符串 --> 基本数据类型 parseXxx(String s)
* 基本数据类型 --> 字符串 valueOf(Xxx)
*/
@Test
public void test4() {
String s= "123";
int num = Integer.parseInt(s);
System.out.println("数字:"+num);
String s1 = String.valueOf(num);
System.out.println("字符串:"+s1);
}
/**
* String与char[]之间的转换
* String --> char[] : toCharArray()
* char[] --> String : String构造器
*/
@Test
public void test5() {
String s= "abc123";
char[] charArray = s.toCharArray();
for (int i = 0; i <charArray.length ; i++) {
System.out.println(charArray[i]); //a b c 1 2 3
}
String s2 = new String(charArray);
System.out.println(s2);
}
/**
*
* String与byte[]之间的转换
* String --> byte[] : getBytes() : 编码的过程 默认使用utf-8
* byte[] --> String : String构造器 : 解码的过程
*
* utf-8 : 中文占3个字节
* gbk : 中文占2个字节
*/
@Test
public void test6() throws UnsupportedEncodingException {
String s= "abc123,我爱你";
byte[] bytes = s.getBytes();
byte[] gbks = s.getBytes("gbk");
System.out.println(Arrays.toString(bytes));
System.out.println(Arrays.toString(gbks));
String s2 = new String(bytes);
String s3 = new String(gbks,"gbk");
System.out.println(s2);
System.out.println(s3);
}
}
StringBuffer、StringBuilder
StringBuffer、StringBuilder、String的异同
import org.junit.Test;
/**
* @Author: mei_ming
* @DateTime: 2022/6/12 15:08
* @Description: StringBuffer与StringBuilder
*/
public class StringBufferBuilderTest {
/**
* String、StringBuffer、StringBuilder的异同
* String :不可变的字符序列,底层使用char[]存储
* StringBuffer: 可变的字符序列,线程安全的,效率低,底层使用char[]存储
* StringBuilder:可变的字符序列,线程不安全的,效率高,底层使用char[]存储
*
* 源码分析:
* String str = new String(); //char[] value = new char[0];
* String str1 = new String("abc"); //char[] value = new char[]{'a','b','c'};
*
* StringBuffer sb1 = new StringBuffer(); //char[] value = new char[16];底层创建了一个长度为16的char数组
* System.out.println(sb1.length()); //0
*
* StringBuffer sb2 = new StringBuffer("abc"); //char[] value = new char["abc".length()+16];
* System.out.println(sb2.length()); //3
*
* 扩容问题:如果要添加的数据底层数组盛不下了,那就需要扩容底层数组,新长度 = 原长度*2+2,接着复制数组
*
* 建议使用new StringBuffer(int capacity)
*/
@Test
public void test(){
StringBuffer stringBuffer = new StringBuffer("abc");
stringBuffer.setCharAt(0,'m');
System.out.println(stringBuffer); //mbc
}
/**
* 对比String、StringBuffer、StringBuilder的效率
*
* 运行结果
* Buffer:4
* Builder:2
* String:3424
*
* builder> buffer>String
*/
@Test
public void test2(){
long start = 0L;
long end = 0L;
String text= "";
StringBuffer stringBuffer = new StringBuffer();
StringBuilder stringBuilder = new StringBuilder();
start = System.currentTimeMillis();
for(int i=0;i<20000;i++){
stringBuffer.append(String.valueOf(i));
}
end = System.currentTimeMillis();
System.out.println("Buffer:"+(end-start));
start = System.currentTimeMillis();
for(int i=0;i<20000;i++){
stringBuilder.append(String.valueOf(i));
}
end = System.currentTimeMillis();
System.out.println("Builder:"+(end-start));
start = System.currentTimeMillis();
for(int i=0;i<20000;i++){
text +=i;
}
end = System.currentTimeMillis();
System.out.println("String:"+(end-start));
}
}
StringBuffer中常用方法
import org.junit.Test;
/**
* @Author: mei_ming
* @DateTime: 2022/6/12 15:25
* @Description: StringBuffer中的方法
*/
public class StringBufferBuilderMethod {
/**
* StringBuffer的常用方法
* StringBuffer append(xxx): 字符串拼接
* StringBuffer delete(int start,int end): 删除指定位置的内容
* StringBuffer replace(int start,int end,String str): 把[start,end)位置替换为str
* StringBuffer insert(int offset,xxx): 在指定位置插入xxx
* StringBuffer reverse(): 把当前字符序列逆转
* public int indexOf(String str)
* public String substring(int start,int end)
* public int length()
* public char charAt(int n)
* public void setCharAt(int n, char ch)
*/
@Test
public void test(){
StringBuffer s1 =new StringBuffer("abc");
s1.append(1);
s1.append("2");
System.out.println(s1); //abc12
s1.delete(2,4);
System.out.println(s1); //ab2
s1.replace(2,4,"hello");
System.out.println(s1); //abhello
s1.insert(2,"insert");
System.out.println(s1); //abinserthello
String substring = s1.substring(2, 8);
System.out.println(substring); //insert
System.out.println(s1.length()); //13
}
}
JDK1.8之前的时间日期类
Date、SimpleDateFormat、Calendar
package ming2.Common;
import org.junit.Test;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
* @Author: mei_ming
* @DateTime: 2022/6/12 15:54
* @Description: JDK 8 之前的时间API测试
* Date 、 SimpleDateFormat、 Calendar
*/
public class DatesTest {
/**
* System类提供currentTimeMillis()
* 用来返回当前时间与1970年1月1日之间以毫秒为单位的时间差
*/
@Test
public void test() {
long timeMillis = System.currentTimeMillis();
System.out.println(timeMillis);
}
/**
* Date类
* java.util.Date/java.sql.Date
*/
@Test
public void test2() {
//java.util.Date
//方式1:
Date date = new Date();
System.out.println(date.toString()); //Sun Jun 12 16:02:47 CST 2022
System.out.println(date.getTime()); //1655020967662
//方式2:
Date date1 = new Date(1655020777513L);
System.out.println(date1.toString()); //Sun Jun 12 15:59:37 CST 2022
//java.sql.Date
java.sql.Date date2 = new java.sql.Date(1655020777513L);
System.out.println(date2.toString()); //2022-06-12
}
//SimpleDateFormat的使用:SimpleDateFormat对日期Date类的格式化和解析
//1. 格式化: 日期--->字符串
//2. 解析: 字符串--->日期
@Test
public void test3() throws ParseException {
//实例化SimpleDateFormat
SimpleDateFormat sdf = new SimpleDateFormat();
//格式化
Date date = new Date();
System.out.println(date); //Mon Jun 13 19:30:14 CST 2022
String format = sdf.format(date);
System.out.println(format); //22-6-13 下午7:30
//解析
//String str = "2022-06-13"; //Unparseable date: "2022-06-13"
String str2 = "22-6-13 下午7:25";
Date date1 = sdf.parse(str2);
System.out.println(date1); //Mon Jun 13 19:25:00 CST 2022
System.out.println("*********");
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
//格式化
String format1 = sdf2.format(date);
System.out.println(format1); //2022-06-13 07:30:14
//解析
Date date2 = sdf2.parse("2022-06-13 19:26:33");
System.out.println(date2); //Mon Jun 13 19:26:33 CST 2022
}
/**
* 将 字符串 “2022-06-13” 转换为 java.sql.Date
*/
@Test
public void test4() throws ParseException {
String birth = "2022-06-13";
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date date = format.parse(birth);
java.sql.Date birthDate = new java.sql.Date(date.getTime());
System.out.println(birthDate);
}
/**
* Calendar: (抽象类)不能直接实例化 日历类
* 获取月份时:1月是 0,2月是 1,,,,,,,12月是 11.
* 获取星期时:周日是 1, 周一是 2,周六是 7.
*/
@Test
public void test5(){
//1. 实例化
//方式1:创建其子类(GregorianCalendar)的对象
//方式2:调用其静态方法getInstance()
Calendar calendar = Calendar.getInstance();
//2. 常用方法
//get()
System.out.println(calendar.get(Calendar.DAY_OF_MONTH)); //返回一个月的第几天
System.out.println(calendar.get(Calendar.DAY_OF_YEAR));
//set()
calendar.set(Calendar.DAY_OF_MONTH,10);
System.out.println(calendar.get(Calendar.DAY_OF_MONTH)); //设置这个月的第10天
//add()
calendar.add(Calendar.DAY_OF_MONTH,-3);
System.out.println(calendar.get(Calendar.DAY_OF_MONTH)); //返回 这个月的第几天 10+3
//getTime() Calendar --> Date
Date date = calendar.getTime();
System.out.println(date); //Tue Jun 07 19:58:02 CST 2022
//setTime() Date --> Calendar
Date date1 = new Date();
calendar.setTime(date1);
System.out.println(calendar.get(Calendar.DAY_OF_MONTH)); //13
}
}
JDK 1.8时间日期类
import org.junit.Test;
import java.time.*;
import java.util.Date;
/**
* @Author: mei_ming
* @DateTime: 2022/6/13 20:01
* @Description: JDK8中的时间API
* LocalDate、LocalTime、LocalDateTime、Instant
* 其对象实例是不可变的,分别表示使用ISO-8601日历系统的日期、时间、日期和时间
*/
public class NewDatesTest {
/**
* LocalDate、LocalTime、LocalDateTime的使用
*/
@Test
public void test(){
//实例化
//1. now()
LocalDate localDate = LocalDate.now();
LocalTime localTime = LocalTime.now();
LocalDateTime localDateTime = LocalDateTime.now();
System.out.println("localDate:"+localDate); //2022-06-13
System.out.println("localTime:"+localTime); //20:21:01.353
System.out.println("localDateTime:"+localDateTime); //2022-06-13T20:21:01.353
//2.of() : LocalDateTime 无偏移量
LocalDateTime localDateTime1 = LocalDateTime.of(2022, 6, 13, 20, 18, 30);
Date date = new Date(2022 - 1900, 7 - 1, 13, 20, 18, 30); //已过时
System.out.println("localDateTime1:"+localDateTime1); //2022-06-13T20:18:30
System.out.println("date:"+date); //Wed Jul 13 20:18:30 CST 2022
//getXXX() 获取相关属性
System.out.println("------get-----");
System.out.println(localDateTime.getDayOfMonth()); //13
System.out.println(localDateTime.getDayOfWeek()); //MONDAY
System.out.println(localDateTime.getMonth()); //JUNE
System.out.println(localDateTime.getMonthValue()); //6
System.out.println(localDateTime.getMinute()); //25
//withXXX() 设置相关属性 不可变性
System.out.println("------with------");
LocalDateTime localDateTime2 = localDateTime.withDayOfMonth(22);
System.out.println(localDateTime); //2022-06-13T20:27:33.925
System.out.println(localDateTime2); //2022-06-22T20:27:33.925
//plusXXX()/minusXXX() 加上/减去日期 不可变性
System.out.println("------plusXXX()/minusXXX()------");
LocalDateTime localDateTime3 = localDateTime.plusDays(3);
System.out.println(localDateTime); //2022-06-13T20:31:06.503
System.out.println(localDateTime3); //2022-06-16T20:31:06.503
LocalDateTime localDateTime4 = localDateTime.minusDays(3);
System.out.println(localDateTime4); //2022-06-10T20:31:06.503
}
/**
* Instant的使用
*/
@Test
public void test2(){
//now(): 获取本初子午线的时间
Instant instant = Instant.now();
System.out.println(instant); //2022-06-13T12:39:26.055Z
//添加时间偏移量
OffsetDateTime offsetDateTime = instant.atOffset(ZoneOffset.ofHours(8));
System.out.println(offsetDateTime); //2022-06-13T20:39:26.055+08:00
}
}
比较器类
实现方式: Comparable接口、创建Comparator对象
package ming2.Common;
import org.junit.Test;
import java.util.Arrays;
import java.util.Comparator;
/**
* @Author: mei_ming
* @DateTime: 2022/6/13 21:52
* @Description: 自然排序、定制排序
*/
public class CompareTest {
/**
* Comparable接口的使用举例 自然排序 java.lang.Comparable
* 1. 像String,包装类等实现了Comparable接口,重写了compareTo(obj) 方法,给出了比较两个对象的比较方式
* 进行从小到大排序
* 2. 重写compareTo(obj) 的规则:
* 如果当前对象this大于形参obj,返回正数
* 小于形参obj,返回负数
* 等于形参obj,返回0
* 3. 对于自定义类来说,如果需要排序,我们可以让自定义类实现Comparable接口,重写compareTo(obj)
* 在compareTo(obj)中指明如何排序
*/
@Test
public void test() {
String[] arr = new String[]{"AA", "CC", "MM", "BB"};
Arrays.sort(arr);
System.out.println(Arrays.toString(arr)); //[AA, BB, CC, MM]
}
@Test
public void test2() {
Goods[] arr = new Goods[5];
arr[0] = new Goods("lenovoMouse", 34);
arr[1] = new Goods("dellMouse", 46);
arr[2] = new Goods("xiaomiMouse", 12);
arr[3] = new Goods("huaweiMouse", 65);
arr[4] = new Goods("zz", 12);
Arrays.sort(arr);
System.out.println(Arrays.toString(arr)); //[Goods{name='zz', price=12.0}, Goods{name='xiaomiMouse', price=12.0}, Goods{name='lenovoMouse', price=34.0}, Goods{name='dellMouse', price=46.0}, Goods{name='huaweiMouse', price=65.0}]
}
/**
* Comparator : java.util.Comparator
* 1.当我们已经实现Comparable接口,但不想要修改源码,实现另一种排序,我们就可以用Comparator
* 2.重写 compare(obj1,obj2)方法,比较两个的大小
* 返回 正数 表示 obj1大于obj2
* 返回 负数 表示 obj1小于obj2
* 返回 0 表示 obj1=obj2
*/
@Test
public void test3() {
String[] arr = new String[]{"AA", "CC", "MM", "BB"};
Arrays.sort(arr, new Comparator() {
//按照字符串从大到小排序
@Override
public int compare(Object o1, Object o2) {
if (o1 instanceof String && o2 instanceof String) {
String s1 = (String) o1;
String s2 = (String) o2;
return -s1.compareTo(s2);
}
throw new RuntimeException("传入的数据类型不一致!");
}
});
System.out.println(Arrays.toString(arr)); //[MM, CC, BB, AA]
}
@Test
public void test4() {
Goods[] arr = new Goods[6];
arr[0] = new Goods("lenovoMouse", 34);
arr[1] = new Goods("dellMouse", 46);
arr[2] = new Goods("xiaomiMouse", 12);
arr[3] = new Goods("huaweiMouse", 65);
arr[4] = new Goods("huaweiMouse", 112);
arr[5] = new Goods("zz", 12);
Arrays.sort(arr, new Comparator() {
//指明产品 先按名称从低到高,再按照价格从高到低
@Override
public int compare(Object o1, Object o2) {
if (o1 instanceof Goods && o2 instanceof Goods) {
Goods g1 = (Goods) o1;
Goods g2 = (Goods) o2;
if (g1.getName().equals(g2.getName())){
return -Float.compare(g1.getPrice(),g2.getPrice());
}else{
return g1.getName().compareTo(g2.getName());
}
}
throw new RuntimeException("传入的数据类型不一致!");
}
});
System.out.println(Arrays.toString(arr)); //[Goods{name='dellMouse', price=46.0}, Goods{name='huaweiMouse', price=112.0}, Goods{name='huaweiMouse', price=65.0}, Goods{name='lenovoMouse', price=34.0}, Goods{name='xiaomiMouse', price=12.0}, Goods{name='zz', price=12.0}]
}
}
System类、Math类、大数类
System类
System类代表系统,系统级的许多属性和控制方法都放在System类,该类位于java.lang包
由于该类的构造器是private的,所以无法创建该类的对象,也就是无法实例化该类。其内部的成员变量和成员方法都是static的,所以也可以很方便的进行调用。
成员变量:in、out、err分别表示标准输入流,标准输出流、标准错误输出流。
成员方法:
- native long currentTimeMillis(): 该方法的作用是返回当前的计算机时间,时间的表达格式为当前计算机时间和GMT时间 1970年1月1号0时0分0秒所差的毫秒数
- void exit(int status):该方法的作用是退出程序,其中status的值为0代表正常退出,非0代表异常退出
- void gc(): 该方法的作用时请求系统进行垃圾回收。至于系统是否立刻回收,则取决于系统中垃圾回收算法的实现以及系统执行时的情况
- String getProperty(String key): 该方法的作用时获取系统中的属性名为key的属性对应的值。
Math类
java.lang.Math提供一系列静态方法用于科学计算。其方法的参数和返回值类型一般为double型。
- abs 绝对值
- acos,asin,atan,cos,sin,tan 三角函数
- sqrt 平方根
- pow(double a,double b) a的b次幂
BIgInteger类
java.math包的BigInteger可以表示不可变的任何精度的整数。
构造器:
BigInteger(String val): 根据字符串构建BigInteger对象
BigDecimal类
BigDecimal要求数字精度更高,支持不可变的、任意精度的有符号十进制定点数。
构造器:
public BigDecimal(double val)
public BigDecimal(String val)
常用方法:
public BigDecimal add(BigDecimal augend)
public BigDecimal subtract(BigDecimal subtrahend)
public BigDecimal multiply(BigDecimal multiplicand)
public BigDecimal divide(BigDecimal divisor,int scale,int roundingMode)
import org.junit.Test;
import java.math.BigDecimal;
import java.math.BigInteger;
/**
* @Author: mei_ming
* @DateTime: 2022/6/14 7:24
* @Description: 其他类,System、Math、BigInteger、BigDecimal
*/
public class OtherClassTest {
@Test
public void test() {
String javaVersion = System.getProperty("java.version");
System.out.println(javaVersion);
System.out.println(System.getProperty("java.home"));
System.out.println(System.getProperty("os.name"));
System.out.println(System.getProperty("os.version"));
System.out.println(System.getProperty("user.name"));
System.out.println(System.getProperty("user.home"));
System.out.println(System.getProperty("user.dir"));
}
@Test
public void test2(){
BigInteger integer = new BigInteger("12345678987654321");
BigDecimal decimal = new BigDecimal("12345.351");
BigDecimal decimal2 = new BigDecimal("12345");
System.out.println(integer);
//System.out.println(decimal.divide(decimal2)); //Non-terminating decimal expansion; no exact representable decimal result.
System.out.println(decimal.divide(decimal2,BigDecimal.ROUND_HALF_UP)); //1.000
System.out.println(decimal.divide(decimal2,15,BigDecimal.ROUND_HALF_UP)); //1.000028432563791
}
}
自学笔记,欢迎纠错