模块十五重点:
1.会BigInteger和BigDecimal操作
2.会Date和SimpleDateFormat的操作
3.会System中的常用方法->主要是数组复制
4.会Arrays中的常用方法
5.会利用包装类定义一个标准的JavaBean
6.会包装类和String之间的转换
第一章.Math类
1.Math类介绍
1.概述:数学工具类
2.作用:主要用于数学运算
3.特点:
a.构造方法私有了(工具类一般都这样~)
b.方法都是静态的
4.使用:
类名直接调用
2.Math类方法:
1.static int abs(int a)->求参数的绝对值
2.static double ceil(double a)->向上取整
3.static double floor(double a)->向下取整
4.static long round(double a)->四舍五入
5.static int max(int a,int b)->求两个数之间的较大值
6.static int min(int a,int b)->求两个数之间的较小值
package xyf0214_math_a;
public class Demo01Math {
public static void main(String[] args) {
//static int abs(int a)->求参数的绝对值
System.out.println(Math.abs(-10));
System.out.println(Math.abs(-999));
//static double ceil(double a)->向上取整
System.out.println(Math.ceil(3.6));
System.out.println(Math.ceil(4.2));
//static double floor(double a)->向下取整
System.out.println(Math.floor(3.6));
System.out.println(Math.floor(4.2));
//static long round(double a)->四舍五入
System.out.println(Math.round(3.1));
System.out.println(Math.round(6.6));
//static int max(int a,int b)->求两个数之间的较大值
System.out.println(Math.max(18,30));
System.out.println(Math.max(-18,-30));
//static int min(int a,int b)->求两个数之间的较小值
System.out.println(Math.max(18,30));
System.out.println(Math.max(-18,-30));
}
}
第二章.BigInteger
1.BigInteger介绍
1.问题描述:我们操作数据,将来的数据有可能非常大,大到比long还要大,这种数据我们一般称之为"对象"
2.作用:
处理超大整数
3.构造:
BigInteger(String val)->参数的格式必须是数字形式
4.方法:
BigInteger add(BigInteger val) 返回其值为(this+val)的BigInteger BigInteger subtract(BigInteger val) 返回其值为(this-val)的BigInteger BigInteger multiply(BigInteger val) 返回其值为(this*val)的BigInteger BigInteger divide(BIgInteger val) 返回其值为(this/val)的BigInteger
package xyf0214_math_BigInteger;
import java.math.BigInteger;
public class Demo02BigInteger {
public static void main(String[] args) {
BigInteger b1 = new BigInteger("121212121212121212121212121212121212121");
BigInteger b2 = new BigInteger("121212121212121212121212121212121212121");
//BigInteger add(BigInteger val) 返回其值为(this+val)的BigInteger
System.out.println(b1.add(b2));
//BigInteger subtract(BigInteger val) 返回其值为(this-val)的BigInteger
System.out.println(b1.subtract(b2));
//BigInteger multiply(BigInteger val) 返回其值为(this*val)的BigInteger
System.out.println(b1.multiply(b2));
//BigInteger divide(BIgInteger val) 返回其值为(this/val)的BigInteger
System.out.println(b1.divide(b2));
}
}
int intValue()将BIgInteger转成int
long longValue()将BigInteger转成long
BigInteger上限:42亿的21亿次方,内存根本扛不住,所以我们可以认为BigInteger无上限
第三章.BigDecimal类(一级重点)
1.BigDecimal介绍
1.问题描述:已知直接用double或者float做运算会出现精度损失的问题,所以将来涉及到钱的运算,我们就不能直接用double或者float直接运算
2.作用:主要是解决double和float直接做运算出现的精度损失问题
3.构造方法:
BigDecimal(String val)->val必须要是数字形式
4.常用方法:
1.static BigDecimal valueOf(double val)->此方法初始化小数时可以传入double型数据
2.BigDecimal add(BigDecimal val) 返回其值为(this+val)的BigDecimal
3.BigDecimal subtract(BigDecimal val) 返回其值为(this-val)的BigDecimal
4.BigDecimal multiply(BigDecimal val) 返回其值为(this*val)的BigDecimal
5.BigDecimal divide(BigDecimal val) 返回其值为(this/val)的BigDecimal
6.BigDemical divide(BigDemical divisor,int scale,int roundingMode)
divisor:除号后面的那个数
scale:指定保留几位小数
roundingMode:取舍方式
static int ROUND_UP->向上加1
static int ROUND_DOWN->直接舍去
static int ROUND_HALF_UP->四舍五入
5.注意:
用方法5时,如果除不尽,会报错出现运算异常。此时可以用方法6
2.BigDecimal使用
package xyf0214_math_BigInteger_BigDecimal;
import java.math.BigDecimal;
public class Demo03BigDecimal {
public static void main(String[] args) {
// big01();
// big02();
big03();
}
private static void big03(){
BigDecimal b1 = new BigDecimal("3.55");
BigDecimal b2 = BigDecimal.valueOf(2.12);
BigDecimal divide = b1.divide(b2, 2, BigDecimal.ROUND_UP);
System.out.println("divide="+divide);
//double doubleValue( ) 将此BIgDecimal转成double
double v=divide.doubleValue();
System.out.println("v="+v);
}
private static void big02(){
BigDecimal b1 = new BigDecimal("3.55");
//new对象:BigDecimal b2 = new BigDecimal("2.12");
//1.static BigDecimal valueOf(double val)->此方法初始化小数时可以传入double型数据
BigDecimal b2 = BigDecimal.valueOf(2.12);
//2.BigDecimal add(BigDecimal val) 返回其值为(this+val)的BigDecimal
BigDecimal add=b1.add(b2);
System.out.println("add="+add);
//3.BigDecimal subtract(BigDecimal val) 返回其值为(this-val)的BigDecimal
BigDecimal subtract=b1.subtract(b2);
System.out.println("subtract="+subtract);
//4.BigDecimal multiply(BigDecimal val) 返回其值为(this*val)的BigDecimal
BigDecimal multiply=b1.multiply(b2);
System.out.println("multiply="+multiply);
//5.BigDecimal divide(BigDecimal val) 返回其值为(this/val)的BigDecimal
BigDecimal divide=b1.divide(b2);
System.out.println("divide="+divide);
}
private static void big01(){
float a=3.55F;
float b=2.12F;
float result=a-b;
System.out.println("result="+result);//result=1.4300001有精度损失问题呢
}
}
double doubleValue( ) 将此BIgDecimal转成double
3.BigDecimal除法过时方法解决
1.注意:如果调用的成员上面有一个横线,证明此成员过时了,底层会有一个注解@Deprecated修饰,但是过时的成员还能使用,只不过被新成员代替了,不推荐使用了
2.方法:
过时的):
BigDemical divide(BigDemical divisor,int scale,int roundingMode)
解决):
divide(BigDecial divisor,int scale,RoundingMode roundingMode)
divisor:代表除号后面的数据
scale:保留几位小数
roundingMode:取舍方式->RoundingMode是一个枚举,里面的成员可以类名直接调用
UP:向上加一
DOWN:直接舍去
HALF_up:四舍五入
private static void big04(){
BigDecimal b1 = new BigDecimal("3.55");
BigDecimal b2 = BigDecimal.valueOf(2.12);
BigDecimal divide = b1.divide(b2, 2, RoundingMode.DOWN);
System.out.println("divide="+divide);
}
第四章.Date日期类(一级重点)
1.Date类的介绍
1.概述:表示特定的瞬间,精确到毫秒

2.Date类的使用:
1.构造:
Date( )->获取当前系统时间
Date(long time)->获取指定时间,传递毫秒值->从时间原点开始算
package xyf0214_math_BigInteger_BigDecimal_Date;
import java.util.Date;
public class Demo01Date {
public static void main(String[] args) {
date01();
}
private static void date01(){
//Date( )->获取当前系统时间
Date date1 = new Date();
System.out.println("date1="+date1);
//Date(long time)->获取指定时间,传递毫秒值->从时间原点开始算
Date date2 = new Date(1000L);
System.out.println("date2="+date2);
}
}
3.Date的常用方法:
1.void setTime(Long time)->设置时间,传递毫秒值->从时间原点开始算 2.long getTime()->获取时间,返回毫秒值
package xyf0214_math_BigInteger_BigDecimal_Date;
import java.util.Date;
public class Demo01Date {
public static void main(String[] args) {
//date01();
date02();
}
private static void date02(){
Date date = new Date();
//1.void setTime(Long time)->设置时间,传递毫秒值->从时间原点开始算
date.setTime(1000L);
//2.long getTime()->获取时间,返回毫秒值
System.out.println(date.getTime());
}
private static void date01(){
//Date( )->获取当前系统时间
Date date1 = new Date();
System.out.println("date1="+date1);
//Date(long time)->获取指定时间,传递毫秒值->从时间原点开始算
Date date2 = new Date(1000L);
System.out.println("date2="+date2);
}
}
第五章.Calender日历类(一级重点)
1.Calender介绍
1.概述:日历类,抽象类
2.获取:Calender中的方法:
static Calender getInstance( )

常用方法: feild:代表的是日历字段->年月日星期等等,都是静态的 int get(int field)->返回给定日历字段的值 void set(int field,int value):将给定的日历字段设置为指定的值 void add(int field,int amount):根据日历的规则,为给定的日历字段添加或者减去指定的时间量 Date getTime():将Calendar转成Date对象 }
package xyf0217time;
import java.util.Calendar;
import java.util.Date;
public class Demo01Calendar {
public static void main(String[] args) {
//calender01();
calendar02();
}
private static void calendar02(){
Calendar calendar=Calendar.getInstance();
//int get(int field)->返回给定日历字段的值
int year=calendar.get(Calendar.YEAR);
System.out.println("year="+year);
//void set(int field,int value):将给定的日历字段设置为指定的值
// calendar.set(Calendar.YEAR,2028);
// System.out.println(calendar.get(Calendar.YEAR));
//void add(int field,int amount):根据日历的规则,为给定的日历字段添加或者减去指定的时间量
calendar.add(Calendar.YEAR,1);
System.out.println(calendar.get(Calendar.YEAR));
//Date getTime():将Calendar转成Date对象
Date date=calendar.getTime();
System.out.println("date="+date);
}
private static void calendar01(){
Calendar calendar= Calendar.getInstance();
System.out.println(calendar);
}
}
扩展方法: void set(int year,int month,int date)->直接设置年月日 需求:键盘录入一个年份,判断这一年是闰年,还是平年 步骤: 1.创建Calendar对象 2.创建Scanner对象,键盘录入一个年份 3.调用set方法,传递年,月,日 set(年,2,1)->国外是0-11,所以设置成2月就是代表3月 4.将day减一天(3月1日减一天,就是2月的最后一天,知道2月的最后一天是28/29,就可以知道是平年还是闰年了) 5.获取day判断是平年还是闰年,输出结果
package xyf0217time;
import java.util.Calendar;
import java.util.Date;
import java.util.Scanner;
public class Demo01Calendar {
public static void main(String[] args) {
//calender01();
//calendar02();
calendar03();
}
private static void calendar03(){
//1.创建Calendar对象
Calendar calendar=Calendar.getInstance();
//2.创建Scanner对象,键盘录入一个年份
Scanner sc=new Scanner(System.in);
int year = sc.nextInt();
//3.调用set方法,传递年,月,日
//set(年,2,1)->国外是0-11,所以设置成2月就是代表3月
calendar.set(year,2,1);
//4.将day减一天(3月1日减一天,就是2月的最后一天,知道2月的最后一天是28/29,就可以知道是平年还是闰年了)
calendar.add(Calendar.DATE,-1);
int day = calendar.get(Calendar.DATE);
//5.获取day判断是平年还是闰年,输出结果
if(day==29){
System.out.println("闰年");
}else{
System.out.println("平年");
}
}
private static void calendar02(){
Calendar calendar=Calendar.getInstance();
//int get(int field)->返回给定日历字段的值
int year=calendar.get(Calendar.YEAR);
System.out.println("year="+year);
//void set(int field,int value):将给定的日历字段设置为指定的值
// calendar.set(Calendar.YEAR,2028);
// System.out.println(calendar.get(Calendar.YEAR));
//void add(int field,int amount):根据日历的规则,为给定的日历字段添加或者减去指定的时间量
calendar.add(Calendar.YEAR,1);
System.out.println(calendar.get(Calendar.YEAR));
//Date getTime():将Calendar转成Date对象
Date date=calendar.getTime();
System.out.println("date="+date);
}
private static void calendar01(){
Calendar calendar= Calendar.getInstance();
System.out.println(calendar);
}
}
第六章.SimpleDateFormat日期格式化类(一级重点)
1.simpleDateFormat介绍
1.概述:日期格式化类(就是格式化日期的)
2.构造:
SimpleDateFormat(String pattern)
3.pattern代表啥:代表的是我们自己指定的日期格式
字母不能改变,但是中间的连接符我们可以改变

2.simpleDateFormat常用方法
1.String format(Date date)->将Date对象按照指定的格式转成String
2.Date parse(String source)->将符合日期格式的字符串转成Date对象
package xyf0217time;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Demo02SimpleDateFormat {
public static void main(String[] args) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//1.String format(Date date)->将Date对象按照指定的格式转成String
String time1=sdf.format(new Date());
//2.Date parse(String source)->将符合日期格式的字符串转成Date对象
String time2="2000-10-10 10:10:10";
Date date=sdf.parse(time2);
System.out.println("date="+date);
}
}
第七章.JDK8新日期类
1.LocalDate本地日期
1.1获取LocalDate对象
1.概述:LocalDate是一个不可变的日期时间对象,表示日期,通常被视为年月日
2.获取:
static LocalDate now( )->创建LocalDate对象
static LocalDate of(int year,int month,int dayOfMonth)->创建LocalDate对象,设置年月日
1.2LocalDateTime对象
1.LocalDateTime概述:LocalDateTime是一个不可变的日期时间对象,代表日期时间,通常被视为年-月-日-时-分-秒
2.获取:
static LocalDateTime now( ) 创建LocalDateTime对象
static LocalDateTime of(int year,Month month,int datOfMonth,int hour,int minute,int second)创建LocalDateTime对象,设置年月日时分秒
package xyf0217time;
import java.time.LocalDateTime;
public class Demo04LocalDateTime {
public static void main(String[] args) {
//static LocalDateTime now( ) 创建LocalDateTime对象
LocalDateTime localDateTime = LocalDateTime.now();
System.out.println("localDateTime="+localDateTime);
//static LocalDateTime of(int year,Month month,int datOfMonth,int hour,int minute,int second)
//创建LocalDateTime对象,设置年月日时分秒
LocalDateTime localDateTime1 = LocalDateTime.of(2000, 10, 10, 10, 10, 10);
System.out.println("localDateTime1="+localDateTime1);
}
}
1.3.获取日期字段方法:名字是get开头
int getYear( )->获取年份
int getMonthValue( )->获取月份
int getDayOfMonth( )->获取月中的几天
package xyf0219time;
import java.time.LocalDate;
public class Demo01LocalDate {
public static void main(String[] args) {
get();
}
private static void get() {
LocalDate localDate = LocalDate.now();
//int getYear()->获取年份
System.out.println(localDate.getYear());
//int getMonthValue()->获取月份
System.out.println(localDate.getMonthValue());
//int getDayOfMonth()->获取月中的几天
System.out.println(localDate.getDayOfMonth());
}
}
1.4.设置日期字段的方法:名字是with开头
LocalDate withYear(int year):设置年份
LocalDate withMonth(int month):设置月份
LocalDate withDayOfMonth(int day):设置月份中的天数
private static void with() {
LocalDate localDate = LocalDate.now();
//LocalDate withYear(int year):设置年份
LocalDate localDate1=localDate.withYear(2000);
System.out.println(localDate1);
注意月份和天数分别调用谁的对象
//LocalDate withMonth(int month):设置月份
LocalDate localDate2 = localDate1.withMonth(10);
System.out.println("localDate2="+localDate2);
//LocalDate withDayOfMonth(int day):设置月份中的天数
LocalDate localDate3 = localDate2.withDayOfMonth(10);
System.out.println("localDate3="+localDate3);
//链式调用:
LocalDate localDate4=localDate.withYear(2001).withMonth(11).withDayOfMonth(11);
System.out.println("localDate4="+localDate4);
}
package xyf0219time;
import java.time.LocalDate;
public class Demo01LocalDate {
public static void main(String[] args) {
//get();
with();
}
private static void with() {
LocalDate localDate = LocalDate.now();
//LocalDate withYear(int year):设置年份
LocalDate localDate1=localDate.withYear(2000);
System.out.println(localDate1);
//LocalDate withMonth(int month):设置月份
LocalDate localDate2 = localDate1.withMonth(10);
System.out.println("localDate2="+localDate2);
//LocalDate withDayOfMonth(int day):设置月份中的天数
LocalDate localDate3 = localDate2.withDayOfMonth(10);
System.out.println("localDate3="+localDate3);
//链式调用:
LocalDate localDate4=localDate.withYear(2001).withMonth(11).withDayOfMonth(11);
System.out.println("localDate4="+localDate4);
}
private static void get() {
LocalDate localDate = LocalDate.now();
//int getYear()->获取年份
System.out.println(localDate.getYear());
//int getMonthValue()->获取月份
System.out.println(localDate.getMonthValue());
//int getDayOfMonth()->获取月中的几天
System.out.println(localDate.getDayOfMonth());
}
}
1.5.日期字段偏移:
设置日期字段的偏移量,方法名plus开头,向后偏移
设置日期字段的偏移量,方法名minus开头,向前偏移
package xyf0219time;
import java.time.LocalDate;
public class Demo01LocalDate {
public static void main(String[] args) {
//get();
//with();
plusAndMinus();
}
private static void plusAndMinus() {
LocalDate localDate = LocalDate.now();
//设置日期字段的偏移量,方法名plus开头,向后偏移
LocalDate localDate1 = localDate.plusYears(1);
System.out.println("localDate1="+localDate1);
//设置日期字段的偏移量,方法名minus开头,向前偏移
LocalDate localDate2 = localDate.minusYears(1);
System.out.println("localDate2="+localDate2);
}
private static void with() {
LocalDate localDate = LocalDate.now();
//LocalDate withYear(int year):设置年份
LocalDate localDate1=localDate.withYear(2000);
System.out.println(localDate1);
//LocalDate withMonth(int month):设置月份
LocalDate localDate2 = localDate1.withMonth(10);
System.out.println("localDate2="+localDate2);
//LocalDate withDayOfMonth(int day):设置月份中的天数
LocalDate localDate3 = localDate2.withDayOfMonth(10);
System.out.println("localDate3="+localDate3);
//链式调用:
LocalDate localDate4=localDate.withYear(2001).withMonth(11).withDayOfMonth(11);
System.out.println("localDate4="+localDate4);
}
private static void get() {
LocalDate localDate = LocalDate.now();
//int getYear()->获取年份
System.out.println(localDate.getYear());
//int getMonthValue()->获取月份
System.out.println(localDate.getMonthValue());
//int getDayOfMonth()->获取月中的几天
System.out.println(localDate.getDayOfMonth());
}
}
2.Period和Duration类
2.1Period计算日期之间的偏差
方法:
static Period between(LocalDate d1,LocalDate d2):计算两个日期之间的差值
getYears();->获取相差的年
getMonths()->获取相差的月
getDays()->获取相差的天
2.2Duration计算时间之间的偏差
1.static Duration between(Temporal startInclusive,Temporal endExclusive)->计算时间差
2.Temporal:是一个接口
实现类:LocalDate LocalDateTime
3.参数需要传递Temporal的实现类对象,注意需要传递LocalDateTime
因为Duration计算精确时间偏差,所以需要传递能操作精确时间的LocalDateTime
4.利用LocalDateTime获取相差的时分秒->to开头
toDays():获取相差天数
toHours():获取相差小时
toMinutes():获取相差分钟
toMillis():获取相差秒(毫秒)
package xyf0219time;
import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Period;
public class Demo02PeriodAndDuration {
public static void main(String[] args) {
//period();
duration();
}
private static void duration() {
LocalDateTime local1 = LocalDateTime.of(2022, 12, 12,12,12,12);
LocalDateTime local2 = LocalDateTime.of(2021, 11, 11,11,11,11);
Duration duration = Duration.between(local2, local1);
System.out.println(duration.toDays());
System.out.println(duration.toHours());
System.out.println(duration.toMinutes());
System.out.println(duration.toMillis());
}
private static void period() {
LocalDate local1 = LocalDate.of(2022, 12, 12);
LocalDate local2 = LocalDate.of(2021, 11, 11);
Period period = Period.between(local1, local2);
System.out.println(period.getYears());
System.out.println(period.getMonths());
System.out.println(period.getDays());
}
}
如果计算年月日,就用Period
如果计算时分秒,就用Duration
3.DateTimeFormatter日期格式化类
1.获取:
static DateTimeFormatter ofPattern(String pattern)->获取对象,指定格式
2.方法:
String format(TemporalAccessor temporal)->将日期对象按照指定的规则转成String
TemporalAccessor:接口,子接口有Temporal
Temporal的实现类:LocalDate LocalDateTime
TemporalAccessor parse(CharSequence text)->将符合规则的字符串转成日期对象
LocalDateTime的静态方法:
static LocalDateTime from(TemporalAccessor temporal)
package xyf0219time;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAccessor;
public class Demo03DateTimeFormatter {
public static void main(String[] args) {
//format();
parse();
}
private static void parse(){
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String time="2000-10-10 10:10:10";
TemporalAccessor temporalAccessor = dtf.parse(time);
//System.out.println(temporalAccessor);
LocalDateTime localDateTime = LocalDateTime.from(temporalAccessor);
System.out.println("localdateTime="+localDateTime);
}
private static void format() {
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime localDateTime = LocalDateTime.now();
String time = dtf.format(localDateTime);
System.out.println("time="+time);
}
}
第八章.System类
1.概述:系统相关类,是一个工具类
2.特点:
a.构造私有,不能利用构造方法new对象
b.方法都是静态的
3.使用:
类名直接调用
arraycopy方法(数组复制)(一级重点)

package xyf0220_utils;
public class Demo01System {
public static void main(String[] args) {
//currentTimeMillis();
//exit();
arraycopy();
}
/*
private static void arraycopy(Object src,int srcPos,Object dest,int destPos,int length) {
src:数组
srcPos:从源数组的哪个索引开始复制
dest:目标数组
destPos:从目标数组的哪个索引开始粘贴
length:复制多少个元素
}
*/
private static void arraycopy() {
int[] arr1={1,2,3,4,5};
int[] arr2=new int[10];
System.arraycopy(arr1,0,arr2,0,5);
for (int i = 0; i < arr2.length; i++) {
System.out.print(arr2[i]+" ");
}
}
private static void exit() {
for (int i = 0; i < 100; i++) {
if(i==5){
System.exit(0);//一般exit都填0
}
System.out.println("helloworld"+i);
}
}
private static void currentTimeMillis() {
long time= System.currentTimeMillis();
System.out.println("time="+time);
}
}
第九章.Arrays数组工具类(一级重点)
1.概述:数组工具类
2.特点:
a.构造私有
b.方法静态
3.使用:类名直接调用
注意这个二分查找方法的前提是升序

package xyf0220_utils;
import java.util.Arrays;
public class Demo02Arrays {
public static void main(String[] args) {
int[] arr={5,3,4,6,5,4,7};
//Arrays.toString(数组名)按照格式打印数组元素
System.out.println(Arrays.toString(arr));
System.out.println("======================");
//Arrays.sort(数组名)升序排序
Arrays.sort(arr);
System.out.println(Arrays.toString(arr));
System.out.println("======================");
//Arrays.binarySearch(数组名,查找的元素)二分查找,但是必须是升序之后使用的方法。寻找元素对应的索引
int[] arr1={1,2,3,4,5,6,7};
int index=Arrays.binarySearch(arr1,3);
System.out.println("index="+index);
System.out.println("======================");
int[] arr2={1,2,3,4,5};
//Arrays.copyOf(数组名,扩容长度)数组扩容
int[] newArr = Arrays.copyOf(arr2, 10);
System.out.println(Arrays.toString(newArr));
arr2=newArr;
System.out.println(Arrays.toString(arr2));
}
}
第十章.包装类
1.基本数据类型对应的引用数据类型(包装类)
1.概述:就是基本类型对应的类(包装类),我们需要将基本类型转成包装类,从而让基本类型拥有类的特性(说白了,将基本类型转化成包装类之后,就可以使用包装类中的方法操作数据)
2.为啥要学包装类:
a.将来有一些特定场景,特定操作,比如调用方法传递包装类
比如:ArrayList集合,里面有一个方法add(Integer i),此时我们不能调用add方法之后直接传递基本类型,因为引用类型不能直接接收基本类型的值,就需要先将基本类型转成包装类,传递到add方法中
b.将来我们还可以将包装类转成基本类型:
包装类不能直接使用+ - * /,所以需要将包装类转成基本类型,才能使用+ - * /
基本类型和包装类型之间的对应(一级重点)

2.Interger的介绍以及使用
2.1Integer基本使用
1.概述:Integer是int的包装类
2.构造:不推荐使用了,但是还能用
Integer(int Value)
Integer(String s) s必须是数字形式
package xyf0220ArrayList;
import java.util.ArrayList;
public class Demo01Integer {
public static void main(String[] args) {
// ArrayList<Integer> list=new ArrayList<>();
// list.add(1);//这里发生了一个类型转换,自动将基本类型转成了包装类
Integer i1=new Integer(10);//已经被弃用了,不过还是能运行
System.out.println("i1="+i1);
Integer i2 = new Integer("10");
System.out.println("i2="+i2);
System.out.println("=====================");
Boolean b1 = new Boolean("true");
System.out.println("b1="+b1);
Boolean b2 = new Boolean("false");
System.out.println("b2="+b2);
Boolean b3 = new Boolean("true");
System.out.println("b3="+b3);
}
}

拆箱装箱(一级重点)
1.装箱:将基本类型转成对应的包装类(常用)
2.方法:
static Integer valueOf(int i)
static Integer valueOf(String s)
package xyf0220ArrayList;
public class Demo02Integer {
public static void main(String[] args) {
Integer i1 = Integer.valueOf(10);
System.out.println("i1="+i1);
Integer i2 = Integer.valueOf("100");
System.out.println("i2="+i2);
}
}
1.拆箱:将包装类转成基本类型
2.方法:
int intValue();
package xyf0220ArrayList;
public class Demo03Integer {
public static void main(String[] args) {
Integer i1 = Integer.valueOf(10);
System.out.println("i1="+i1);
int i=i1.intValue();
System.out.println("(i+10)="+(i+10));
}
}
2.2自动拆箱装箱
1.拆箱和装箱很多时候都是自动完成的
package xyf0220ArrayList;
public class Demo04Integer {
public static void main(String[] args) {
Integer i=10;//发生了自动装箱了
Integer sum=i+10;//发生了自动拆箱
System.out.println("sum="+sum);
}
}
反编译证明自动装箱拆箱:

package xyf0220ArrayList;
public class Demo5Integer {
public static void main(String[] args) {
Integer i1=100;
Integer i2=100;
System.out.println(i1==i2);//true
Integer i3=128;
Integer i4=128;
System.out.println(i3==14);//false
}
}

注意这些包装类(基本数据类型对应的的引用数据类型)都有范围:

3.基本类型和String类型之间的转换
3.1基本类型往String转
1.方式1:
+拼接
2.方式2:String中的静态方法
static String valueOf(int i)
package xyf0220ArrayList;
public class Demo06Parse {
public static void main(String[] args) {
method01();
}
private static void method01(){
/*
3.1基本类型往String转
1.方式1:
+拼接
*/
int i=10;
String s1=i+"";
System.out.println(s1+1);
System.out.println("===================");
/*
2.方式2:String中的静态方法
static String valueOf(int i)
*/
String s=String.valueOf(10);
System.out.println(s+1);
}
}
3.2String转成基本数据类型
每个包装类中都有一个类似的方法:parseXXX
以Integer为例:
package xyf0220ArrayList;
public class Demo06Parse {
public static void main(String[] args) {
//method01();
method02();
}
private static void method02() {
int number = Integer.parseInt("1111");
System.out.println(number+1);
}
private static void method01(){
/*
3.1基本类型往String转
1.方式1:
+拼接
*/
int i=10;
String s1=i+"";
System.out.println(s1+1);
System.out.println("===================");
/*
2.方式2:String中的静态方法
static String valueOf(int i)
*/
String s=String.valueOf(10);
System.out.println(s+1);
}
}

标准JavaBean(一级重点)
1.在实际开发过程中如何定义一个标准的JavaBean:
定义JavaBean的时候一般会将基本数据类型的属性定义成包装类型的属性(说白了,就是将基本类型变成包装类型)
package xyf0220ArrayList;
public class User {
// private int uid;//用户id
private Integer uid;//用户id
private String username;//用户名
private String password;//密码
public User() {
}
public User(Integer uid, String username, String password) {
this.uid = uid;
this.username = username;
this.password = password;
}
public Integer getUid() {
return uid;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public void setUid(Integer uid) {
this.uid = uid;
}
}
将基本类型变成包装类下的原因(了解即可):

1539

被折叠的 条评论
为什么被折叠?



