悟已往之不谏,知来者之可追
在常用API中,只需记住类名、类的作用,会查API帮助文档
类Math:数学计算的工具类
final类,不可继承
字段:
static double E − 这是比任何其他值都更接近自然对数底 e 的双精度值。
static double PI − 这是比任何其他值都更接近 pi 的双精度值,pi 是圆的周长与其直径的比值。
原理:
private Math(){} 私有化构造方法,外部不能创建其对象。
静态方法:类名.方法名 调用
1 2 3 4 5 6 7 8 9 Math.abs(); Math.ceil(); Math.floor(); Math.round(); Math.max(); Math.min(); Math.pow(a,b); Math.sqrt(); Math.random();
类System:系统工具类
计算机的时间原点:1970.1.1 00:00:00
1 2 3 4 5 6 7 8 9 10 11 System.exit(); System.currentTimeMillis(); System.arraycopy();
类Runtime:允许应用程序与运行应用程序的环境进行交互。
原理:
使用Runtime类的实例化方法需要先获取对象,因为运行环境唯一,构造方法私有化,所以在Runtime类的内部自己创建了对象并赋值给了currentRuntime,且用final修饰,保证了地址值不会变化。再使用getRuntime()方法将对象currentRuntime进行返回,获取到了Runtime的对象,确保了在不同类中的何处运行都保证了获取到了同一个对象。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 public class Runtime { private static final Runtime currentRuntime = new Runtime (); private static Version version; public static Runtime getRuntime () { return currentRuntime; } private Runtime () {} }
1 2 3 4 5 6 7 8 9 10 Runtime.getRuntime(); Runtime.getRuntime().exit() Runtime.getRuntime().availableProcessors(); Runtime.getRuntime().maxMemory(); Runtime.getRuntime().totalMemory(); Runtime.getRuntime().freeMemory(); Runtime.getRuntime().exec("" );
类Object:顶级父类,所有类直接或间接继承与Object类
因为类Object是顶级父类,所以没有成员变量,为空参构造。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 public String toString () { return getClass().getName() + "@" + Integer.toHexString(hashCode()); } public boolean equals (Objectobj) {return (this == obj);} protected native Objectclone () throws CloneNotSupportedException;
System.out.println(obj)原理解释
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 public void println (Objectx) {String s = String.valueOf(x); synchronized (this ) {print(s); newLine(); } } public static String valueOf (Objectobj) {return (obj == null ) ? "null" : obj.toString(); }
所以println();方法先调用了String.valueOf();valueOf()的底层又调用了String包下的obj.toSting()方法
String包下的equals方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 public boolean equals (ObjectanObject) { if (this == anObject) { return true ; } if (anObjectinstanceof String) { String aString = (String)anObject; if (coder() == aString.coder()) { return isLatin1() ? StringLatin1.equals(value, aString.value) : StringUTF16.equals(value, aString.value); } } return false ; } String s1 = "Hello" ; String s2 = "Hello" ; String s3 = s1; String s4 = new String ("Hello" ); String s5 = new String ("Hello" ); s1 == s1; s1 == s2; s1 == s3; s1 == s4; s4 == s5; s1.equals(s3); s1.equals(s4); s4.equals(s5);
浅克隆、深克隆的区别
需注意克隆是指克隆对象整体以及内部的属性
浅克隆:我们这里说的浅拷贝是指我们拷贝出来的对象内部的引用类型变量和原来对象内部引用类型变量是同一引用 (指向同一对象)。但是我们拷贝出来的对象和新对象不是同一对象 。基本数据类型直接拷贝数据值(开辟新空间 ),引用数据类型会直接拷贝地址,但字符串会复用串池,有final修饰的存在 。
深克隆:全部拷贝原对象的内容 ,包括内存的引用类型也进行拷贝,基本数据类型直接拷贝数据值(开辟新空间),引用数据类型会重新创建对象并赋值,但字符串会复用串池 。
浅克隆可以使用类Object下的Clone()方法
深克隆可以使用第三方工具:
原理:Gson可以将对象序列化成JSON,也可以将JSON反序列化成对象,所以我们可以用它进行深拷贝。
导入jar包
将对象变为字符串
将字符串变为对象
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 public void gsonCopy () { Address address = new Address ("杭州" , "中国" ); User user = new User ("大山" , address); Gson gson = new Gson (); User copyUser = gson.fromJson(gson.toJson(user), User.class); user.getAddress().setCity("深圳" ); assertNotSame(user.getAddress().getCity(), copyUser.getAddress().getCity()); }
类Objects: Object的工具类,位于java.util包
静态方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 public static boolean equals (Objecta, Objectb) { return (a == b) || (a != null && a.equals(b)); } public static boolean isNull (Objectobj) {return obj == null ;} public static boolean nonNull (Objectobj) {return obj != null ;}
莫道桑榆晚,为霞尚满天
类BigInteger:不可变的任意精度整数
所有操作都表现得好像BigIntegers用二进制补码表示法表示(如Java的原始整数类型)。 BigInteger为所有Java的原始整数运算符以及java.lang.Math中的所有相关方法提供类似物。 此外,BigInteger还提供模块化算术,GCD计算,素性测试,素数生成,位操作以及一些其他杂项操作的操作。
构造方法
1 2 3 4 5 6 7 8 BigInteger(intnumBits, Random rnd) BigInteger(String val) BigInteger(String val, intradix)
1 2 3 4 5 6 7 8 9 静态方法 static BigInteger valueOf (long val)
BigInteger为什么能存这么大数?
BigInteger对象存数原理是 成员变量:final intsignum;是符号位 1:正数 ,0:符号位为0,-1:负数
final int[] mag; index=0是符号位,其他位置全用来分段存数,以二进制的补码方式存数
类BigDecimal:用于小数精确计算、表示很大的数
不变的,任意精度的带符号的十进制数字。
通过构造方法创建对象
1 2 3 4 5 6 7 8 9 10 11 BigDecimal(double val) BigDecimal(String val)
通过静态方法获取对象
1 2 3 4 5 6 static BigDecimal valueOf (double val)
细节:
如果数字不大,没有超出double的范围,建议使用静态方法
如果数字很大,超出了double的范围,建议使用构造方法
如果我们传递的是[0-10]的整数,那么方法会返回已经创建好的对象,不会重新创建
BigDecimal的使用
1 2 3 4 5 6 7 8 9 10 public BigDecimal add (BigDecimal val) public BigDecimal subtract (BigDecimal val) public BigDecimal multiply (BigDecimal val) public BigDecimal divide (BigDecimal val) public BigDecimal divide (BigDecimal val,精确几位,舍入模式)
BigDecimal底层存储方法:
将小数每一位按照ASCII码表的值,对应存入到数组中
正则表达式:校验字符串是否满足一定的规则
正则表达式的作用:
校验字符串是否满足规则
在一段文本中查找满足要求的内容
字符串校验
字符类(只匹配一个字符)
[abc]
[^abc]
[a-zA-Z]
[a-d[m-p]]
[a-z&&[def]]
[a-z&&[^bc]]
[a-z&&[^m-p]]
只能是a||b||c
除了a,b,c之外的任何一个字符
只能是a-z||A-Z
只能是a-d||m-p 括起来也表示或者的意思
a-z与[def]的交集:只能是d,e,f
a-z与[^bc]的交集:只能是a,d-z
a-z与[^m-p]的交集:是a-l,q-z
预定义字符(只匹配一个字符)
.
\d
\D
\s
\S
\w
\W
任何字符
一个数字[0-9]
非数字[^0-9]
一个空白字符[\t\n\x0B\f\r]
非空白字符[^\s]
[a-zA-Z_0-9] 字母、数字、下划线
[^\W]
数量词
X?
X*
X+
X{n}
X{n,}
X{n,m}
X,一次或0次
X,零次或多次
X,一次或多次
X,正好n次
X,至少n次
X,至少n次但是不超过m次
1 2 3 4 5 6 7 8 手机号验证的正则表达式 String regex = "1[3-9]\\d{9}" ;System.out.printlf("15148877290" .matches(regex));
座机号验证的正则表达式
1 2 3 4 5 6 String regex = "0\\d{2,3}-?[1-9]\\d{4,9}" ;System.out.println("0478-1234567" .matches(regex));
邮箱号验证的正则表达式
1 2 3 4 5 6 7 8 9 10 String regex = "\\w+@[\\w&&[^_]]{2,6}(\\.[a-zA-Z]{2,3}){1,2}" ;
用户名验证的正则表达式
1 2 String regex = "\\w{4,16}" ;
身份证号简单验证的正则表达式
1 2 3 4 String regex = "[1-9]\\d{16}[\\dXx]" ;String regex2 = "[1-9]\\d{16}(\\d|(?i)x);
正则表达式插件
在idea中,提供了正则表达式的插件,帮助我们去快速书写正则表达式—— any-rule
爬虫
在一段文本中,按照一定的字符串要求进行爬取。
爬虫举例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 String str = Java自从95 年问世以来,经历了很多版本,目前企业中用的最多是Java8和Java11,+因为这两个是长期支持版本,下一个长期支持版本是Java17,相信在不久的未来Java17也会逐渐登上历史的舞台; Pattern p = Pattern.compile("Java\\d{0,2}" );Matcher m = p.matcher(str);while (m.find()){String s = m.group();System.out.println(s);n }
带条件的爬取:
代码块中的例子,只想爬取带版本号的Java,但是只要Java,不要版本号
带条件的爬取
1 2 3 4 5 String regex = "Java(?=8|11|17);
正则表达式在字符串中的应用
实例化方法
1 2 3 4 5 6 7 8 public String[] matches(String regex);public String replaceAll (String regex,String newStr) ;public String[] split(String regex);
分组
即正则表达式中的(),每组都有组号,也就是序号
规则1:从1开始,连续不间断
规则2:从左括号为基准,最左边的是第一组,其次是第二组,以此类推。
捕获分组:后续仍会继续使用到分组
在正则内部使用分组:\组号
在正则外部使用分组:$组号
非捕获分组:后续不会继续使用到分组,仅仅只是为了分组操作
(**?: ** )
特点:不占用分组
捕获分组
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 String regex = "(.).+\\1" ;System.out.println("a13a" .matches(regex)); String regex = "(.+).+\\1" ;System.out.println("abc13abc" .matches(regex)); String regex = "((.)\\2*).+\\1" ; \\将首字母(.)看作一组,反复\\2 出现*次System.out.println("aaa13aaa" .matches(regex)); String regex = (wyxxxxxbbbbbbbccccccc);String result= str.replaceAll("(.)\\1+" ;,"$1" ); System.out.println("wyxbc" ;);
JDK7之前的时间类
Date时间类
Date时间类是一个JDK写好的javabean类,用来表示时间,精确到秒
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 利用空参构造创建的对象,表示系统当前时间 利用有参构造创建的对象,表示指定时间 Date类的构造方法与实例化方法 public Date () {} public Date (long date) {}public void setTime (long time) public long getTime () Date d1 = new Date (0L );long time = d1.getTime();time = time + 1000L * 60 * 60 * 24 * 365 ; d1.setTime(time); System.out.printlf(d1); Random r = new Random ();Date d1 = new Date (Math.abs(r.nextInt()));Date d2 = new Date (Math.abs(r.nextInt()));long time1 = d1.getTime();long time2 = d2.getTime();
SimpleDateFormat类
格式化:把时间格式按需求更改
解析:将字符串表示的时间变成Date对象
1 2 public SimpleDateFormat () public SimpleDateFormat (String pattern)
1 2 public final String format (Date date) public Date parse (String source)
格式化时间常用模式对应关系
y 年 H 时
M 月 m 分
d 日 s 秒
可查阅API文档
1 2 3 4 5 6 7 8 9 10 11 SimpleDateFormatsdf1 = new SimpleDateFormat (); Data d1 = new Date ();String str1 = sdf1.format(d1);System.out,println(str1); SimpleDateFormatsdf2 = new SimpleDateFormat ("yyyy年MM月dd日 HH:mm:ss EE" ); Date d2 = new Date ();Stirng str2 = sdf2.format(d2);System.out.println(str2);
1 2 3 4 5 6 7 String str = "2024-6-1 10:33:19" ;DateSimpleFormatdsf = new DateSimpleFormat ("yyyy-MM-dd HH:mm:ss" ); Date date = sdf.parse(data);System.out,println(date);
将一个格式的时间转换为另一个格式的时间
方法:
先将需要转换格式的时间,按照格式解析为时间对象,
再将时间对象转化为格式化为想要的格式。
Calendar类
是一个抽象类,不能直接创建对象
日历类获取到的月份是0-11,需要+1处理
在外国星期日是一周中的第一天,所以1对应的星期日
1 public static Calendar getInstance ()
Calendar 类常用方法的记录:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 Calendar cal = Calendar.getInstance(); System.out.println("年:" + cal.get(Calendar.YEAR)); System.out.println("月:" + (cal.get(Calendar.MONTH) + 1 )); System.out.println("日:" + cal.get(Calendar.DAY_OF_MONTH)); System.out.println("时:" + cal.get(Calendar.HOUR_OF_DAY)); System.out.println("分:" + cal.get(Calendar.MINUTE)); System.out.println("秒:" + cal.get(Calendar.SECOND)); Calendar cal = Calendar.getInstance(); cal.set(2018 , 1 , 15 , 23 , 59 , 59 ); cal.set(Calendar.YEAR, 2018 ); cal.set(Calendar.MONTH, Calendar.FEBRUARY); cal.set(Calendar.DAY_OF_MONTH, 15 ); cal.set(Calendar.HOUR_OF_DAY, 23 ); cal.set(Calendar.MINUTE, 59 ); cal.set(Calendar.SECOND, 59 ); System.out.println(cal.getTime());
时间计算
add方法: 比如在除夕当晚最后一秒,add 一秒:
1 2 3 4 5 Calendar cal = Calendar.getInstance();System.out.println(cal.getTime()); cal.set(2018 , 1 , 15 , 23 , 59 , 59 ); cal.add(Calendar.SECOND, 1 ); System.out.println(cal.getTime());
打印时间结果如下,日期会自动进入下一天:
1 2 Thu Feb 15 23 :59 :59 CST 2018 Fri Feb 16 00 :00 :00 CST 2018
JDK8新增的时间类
新增判断方法,计算时间间隔的方法
解决了多线程下会导致数据安全的问题
时间日期对象都是不可变的,解决了这个问题
ZoneId获取时区
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 Set<String> zoneIds = ZoneId.getAvailableZoneIds(); System.out.println(zoneIds.size()); System.out.println(zoneIds); ZoneId zoneId = ZoneId.systemDefault();System.out.println(zoneId); ZoneId zoneId1 = ZoneId.of("Asia/Pontianak" );System.out.println(zoneId1);
Instant时间戳
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 Instantnow = Instant.now(); System.out.println(now); Instantinstant1 = Instant.ofEpochMilli(0L ); System.out.println(instant1); Instantinstant2 = Instant.ofEpochSecond(1L ); System.out.println(instant2); Instantinstant3 = Instant.ofEpochSecond(1L , 1000000000L ); System.out.println(instant3); ZonedDateTime time = Instant.now().atZone(ZoneId.of("Asia/Shanghai" ));System.out.println(time); Instantinstant4=Instant.ofEpochMilli(0L ); Instantinstant5 =Instant.ofEpochMilli(1000L ); boolean result1=instant4.isBefore(instant5);System.out.println(result1); boolean result2 = instant4.isAfter(instant5);System.out.println(result2); Instantinstant6 =Instant.ofEpochMilli(3000L ); System.out.println(instant6); Instantinstant7 =instant6.minusSeconds(1 ); System.out.println(instant7);
ZoneDateTime带时区的时间
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 ZonedDateTime now = ZonedDateTime.now();System.out.println(now); ZonedDateTime time1 = ZonedDateTime.of(2023 , 10 , 1 , 11 , 12 , 12 , 0 , ZoneId.of("Asia/Shanghai" )); System.out.println(time1); Instantinstant= Instant.ofEpochMilli(0L ); ZoneId zoneId = ZoneId.of("Asia/Shanghai" );ZonedDateTime time2 = ZonedDateTime.ofInstant(instant, zoneId);System.out.println(time2); ZonedDateTime time3 = time2.withYear(2000 );System.out.println(time3); ZonedDateTime time4 = time3.minusYears(1 );System.out.println(time4); ZonedDateTime time5 = time4.plusYears(1 );System.out.println(time5);
DateTimeFormatter时间的格式化和解析
1 2 3 4 5 6 7 8 9 10 11 ZonedDateTime time = Instant.now().atZone(ZoneId.of("Asia/Shanghai" ));DateTimeFormatter dtf1=DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm;ss EE a" ); System.out.println(dtf1.format(time));
LocalDate年、月、日
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 LocalDate nowDate = LocalDate.now();LocalDate ldDate = LocalDate.of(2023 , 1 , 1 );System.out.println("指定日期:" + ldDate); System.out.println("=============================" ); intyear = ldDate.getYear(); System.out.println("year: " + year); Month m = ldDate.getMonth();System.out.println(m); System.out.println(m.getValue()); intmonth = ldDate.getMonthValue(); System.out.println("month: " + month); intday = ldDate.getDayOfMonth(); System.out.println("day:" + day); intdayofYear = ldDate.getDayOfYear(); System.out.println("dayOfYear:" + dayofYear); DayOfWeek dayOfWeek = ldDate.getDayOfWeek();System.out.println(dayOfWeek); System.out.println(dayOfWeek.getValue()); System.out.println(ldDate.isBefore(ldDate)); System.out.println(ldDate.isAfter(ldDate)); LocalDate withLocalDate = ldDate.withYear(2000 );System.out.println(withLocalDate); LocalDate minusLocalDate = ldDate.minusYears(1 );System.out.println(minusLocalDate); LocalDate plusLocalDate = ldDate.plusDays(1 );System.out.println(plusLocalDate); LocalDate birDate = LocalDate.of(2000 , 1 , 1 );LocalDate nowDate1 = LocalDate.now();MonthDay birMd = MonthDay.of(birDate.getMonthValue(), birDate.getDayOfMonth());MonthDay nowMd = MonthDay.from(nowDate1);System.out.println("今天是你的生日吗? " + birMd.equals(nowMd));
LocalTime 时、分、秒
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 LocalTime nowTime = LocalTime.now();System.out.println("今天的时间:" + nowTime); inthour = nowTime.getHour(); System.out.println("hour: " + hour); intminute = nowTime.getMinute(); System.out.println("minute: " + minute); intsecond = nowTime.getSecond(); System.out.println("second:" + second); intnano = nowTime.getNano(); System.out.println("nano:" + nano); System.out.println("------------------------------------" ); System.out.println(LocalTime.of(8 , 20 )); System.out.println(LocalTime.of(8 , 20 , 30 )); System.out.println(LocalTime.of(8 , 20 , 30 , 150 )); LocalTime mTime = LocalTime.of(8 , 20 , 30 , 150 );System.out.println(nowTime.isBefore(mTime)); System.out.println(nowTime.isAfter(mTime)); System.out.println(nowTime.withHour(10 )); System.out.println(nowTime.plusHours(10 ));
LocalDateTime 年、月、日、时、分、秒
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 LocalDateTime nowDateTime = LocalDateTime.now();System.out.println("今天是:" + nowDateTime); System.out.println(nowDateTime.getYear()); System.out.println(nowDateTime.getMonthValue()); System.out.println(nowDateTime.getDayOfMonth()); System.out.println(nowDateTime.getHour()); System.out.println(nowDateTime.getMinute()); System.out.println(nowDateTime.getSecond()); System.out.println(nowDateTime.getNano()); System.out.println("dayofYear:" + nowDateTime.getDayOfYear()); System.out.println(nowDateTime.getDayOfWeek()); System.out.println(nowDateTime.getDayOfWeek().getValue()); System.out.println(nowDateTime.getMonth()); System.out.println(nowDateTime.getMonth().getValue()); LocalDate ld = nowDateTime.toLocalDate();System.out.println(ld); LocalTime lt= nowDateTime.toLocalTime(); System.out.println(lt.getHour()); System.out.println(lt.getMinute()); System.out.println(lt.getSecond());
Duration 时间间隔(秒,纳,秒)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 LocalDateTime today = LocalDateTime.now();System.out.println(today); LocalDateTime birthDate = LocalDateTime.of(2000 , 1 , 1 , 0 , 0 , 0 );System.out.println(birthDate); Duration duration = Duration.between(birthDate, today);System.out.println("相差的时间间隔对象:" + duration); System.out.println("============================================" ); System.out.println(duration.toDays()); System.out.println(duration.toHours()); System.out.println(duration.toMinutes()); System.out.println(duration.toMillis()); System.out.println(duration.toNanos());
Period 时间间隔(年,月,日)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 LocalDate today = LocalDate.now();System.out.println(today); LocalDate birthDate = LocalDate.of(2000 , 1 , 1 );System.out.println(birthDate); Period period = Period.between(birthDate, today);System.out.println("相差的时间间隔对象:" + period); System.out.println(period.getYears()); System.out.println(period.getMonths()); System.out.println(period.getDays()); System.out.println(period.toTotalMonths());
ChronoUnit 时间间隔(所有单位)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 LocalDateTime today = LocalDateTime.now();System.out.println(today); LocalDateTime birthDate = LocalDateTime.of(2000 , 1 , 1 ,0 , 0 , 0 );System.out.println(birthDate); System.out.println("相差的年数:" + ChronoUnit.YEARS.between(birthDate, today)); System.out.println("相差的月数:" + ChronoUnit.MONTHS.between(birthDate, today)); System.out.println("相差的周数:" + ChronoUnit.WEEKS.between(birthDate, today)); System.out.println("相差的天数:" + ChronoUnit.DAYS.between(birthDate, today)); System.out.println("相差的时数:" + ChronoUnit.HOURS.between(birthDate, today)); System.out.println("相差的分数:" + ChronoUnit.MINUTES.between(birthDate, today)); System.out.println("相差的秒数:" + ChronoUnit.SECONDS.between(birthDate, today)); System.out.println("相差的毫秒数:" + ChronoUnit.MILLIS.between(birthDate, today)); System.out.println("相差的微秒数:" + ChronoUnit.MICROS.between(birthDate, today)); System.out.println("相差的纳秒数:" + ChronoUnit.NANOS.between(birthDate, today)); System.out.println("相差的半天数:" + ChronoUnit.HALF_DAYS.between(birthDate, today)); System.out.println("相差的十年数:" + ChronoUnit.DECADES.between(birthDate, today)); System.out.println("相差的世纪(百年)数:" + ChronoUnit.CENTURIES.between(birthDate, today)); System.out.println("相差的千年数:" + ChronoUnit.MILLENNIA.between(birthDate, today)); System.out.println("相差的纪元数:" + ChronoUnit.ERAS.between(birthDate, today));
包装类
包装类的本质是创建了一个对象,用一个对象把基本数据类型包装起来。 集合中不能存储基本数据类型,只能存储对象,需要用到包装类。 Java提供了两个类型系统,基本类型与引用类型,使用基本类型在于效率,然而很多情况,会创建对象使用,因为对象可以做更多的功能,如果想要我们的基本类型像对象一样操作,就可以使用基本类型对应的包装类,如下:
基本类型
对应的包装类(位于java.lang包中)
byte
Byte
short
Short
int
Integer
long
Long
float
Float
double
Double
char
Character
boolean
Boolean
自动装箱与自动拆箱
自动装箱:把基本数据类型自动转换为其对应的包装类
自动拆箱:把包装类自动转换为其对应的基本数据类型
Integer类
包装一个对象中的原始类型 int的值
Integer类构造方法及静态方法
方法名
说明
public Integer(int value)
有参构造,根据 int值创建 Integer 对象(过时)
public Integer(String s)
有参构造,根据 String 值创建 Integer 对象(过时)
public static Integer valueOf(inti)
返回表示指定的 int值的 Integer实例
public static Integer valueOf(String s)
返回保存指定String值的 Integer 对象
static string tobinarystring(inti)
得到二进制
static string tooctalstring(inti)
得到八进制
static string toHexstring(inti)
得到十六进制
static intparseInt(string s)
将字符串类型的整数转成int类型的整数
两种方式获取对象
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 Integer i3 = Integer.valueOf(100 );System.out.println(i3); Integer i4 = Integer.valueOf("100" );System.out.println(i4);
建议:获取Integer对象的时候不要自己new,而是采取直接赋值或者静态方法valueOf的方式。
因为在实际开发中,-128~127之间的数据,用的比较多。如果每次使用都是new对象,那么太浪费内存了。
所以,提前把这个范围之内的每一个数据都创建好对象,如果要用到了不会创建新的,而是返回已经创建好的对象。
两种方式获取对象的区别
底层原理:
Integer.valueOf()对-128~127 之间的256个数进行了优化,提前创建好了对象,只需要使用静态方法返回对象。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 Integer i6 = Integer.valueOf(127 );Integer i7 = Integer.valueOf(127 );System.out.println(i6 == i7); Integer i8 = Integer.valueOf(128 );Integer i9 = Integer.valueOf(128 );System.out.println(i8 == i9);
源码:
常用静态方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 String str1 = Integer.toBinaryString(100 );System.out.println(str1); String str2 = Integer.toOctalString(100 );System.out.println(str2); String str3 = Integer.toHexString(100 );System.out.println(str3); inti = Integer.parseInt("123" ); System.out.println(i); System.out.println(i + 1 ); String str = "true" ;boolean b = Boolean.parseBoolean(str);System.out.println(b);
包装类计算
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 Integer i1 = new Integer (1 );Integer i2 = new Integer (2 );intresult= i1.intValue() + i2.intValue(); Integer i3 = new Integer (result);System.out.println(i3); Integer i = 4 ;i = i + 5 ;
键盘录入
1 2 3 4 5 6 7 8 9 Scanner sc = new Scanner (System.in);String str = sc.next();System.out.println(str);