课前回顾:1.Object类中的方法:toString():输出地址值,如果想输出对象名不是地址值,就要重写toStringequals():比较地址值,如果想比较内容,重写equals方法getClass():获取类对象native方法:本地方法,可以作为java的扩展方法2.System类中方法gc()垃圾回收器,会回收堆中没有引用的对象IO流对象,Socket对象,数据库连接对象GC是回收不了的,需要我们手动的关闭finalize() : 用于回收对象 不是GC调用,而是GC通知对象调用3.String:a.概述:代表的是字符串b.特点:凡是带""都是String的对象字符串创建之后无法改变可以共享c.底层实现原理:final char[] valued.构造:String()String(String s)String(char[] char)String(byte[] bytes)String(char[] char,int offset,int count)->将char数组的一部分转成String对象String(byte[] bytes,int offset,int count)->将byte数组的一部分转成String对象简化:String 变量名 = ""e.面试题:String s = new String("abc")->创建了几个对象->1个或两个f.方法比较方法:equals(String s)->比较内容equalsIgnoreCase(String s)->比较内容,忽略大小写获取方法:length()->获取字符串长度charAt(int index)->根据索引获取对应的字符subString(int beginIndex)->截取字符串,从beginIndex开始截取到最后subString(int beginInext,int endIndex)->截取字符串,含头不含尾concat(String s)->拼接字符串indexOf(String s)获取指定字符串在原串儿中第一次出现的索引位置转换方法:getBytes()->将字符串转成byte数组toCharArray()->将字符串转成char数组replace(String s,String s1)->将s替换成s1分割方法:split(String regex)->按照指定的规则分割字符串扩展方法:trim()->去掉两端空格startsWith(String s)->判断字符串是否以s开头endsWith(String s)->判断字符串是否以s结尾toLowerCase()->将字符串转成小写toUpperCase()->将字符串转成大写contains(String s)->判断字符串是否包含s4.StringBuildera.作用:拼接字符串提高效率b.特点:底层自带缓冲区(char数组),默认长度为16超过16自动扩容,2倍+2如何扩容:Arrays.copyOf->利用的是数组赋值c.方法:append(任意类型)->字符串拼接,返回的是StringBuilder自己toString()->将StringBuilder转成Stringreverse()->字符串翻转今日内容:1.会使用Math类的方法做数学计算2.会使用大类型,进行计算(加减乘除)3.会使用Date,SimpleDateFormat,Calendar操作时间4.会使用System类中的arrayCopy完成数组复制5.会使用Arrays数组工具类6.每一个基本类型对应的包装类是啥
第一章.Math类
1.Math类介绍
1.概述:数学工具类,专门用于数学运算2.特点:a.构造被私有化了,所以,我们不能用构造new对象b.方法为静态方法3.使用:类名直接调用
2.Math类方法
public static int abs(int a) ->求参数的绝对值public static double ceil(double a)向上取整public static double floor(double a)向下取整public static int round(float a)按照四舍五入返回最接近参数的intstatic int max(int a, int b) ->求两个参数中较大的值static int min(int a, int b) ->求两个参数中较小的值
public class Test01 {public static void main(String[] args) {// public static int abs(int a) ->求参数的绝对值System.out.println(Math.abs(-1));// public static double ceil(double a)向上取整System.out.println(Math.ceil(2.3));System.out.println(Math.ceil(-2.3));// public static double floor(double a)向下取整System.out.println(Math.floor(-2.1));// public static int round(float a)按照四舍五入返回最接近参数的intSystem.out.println(Math.round(2.5));// static int max(int a, int b) ->求两个参数中较大的值System.out.println(Math.max(10,20));// static int min(int a, int b) ->求两个参数中较小的值System.out.println(Math.min(10,20));}}
第二章.BigInteger
1.BigInteger介绍
1.作用:处理大的整数2.为什么要学这个类:现实中有没有可能出现比long型还大的数?有那么在java中,比long型还大的数我们就不用称之为数,我们就称之为对象了3.使用:构造:BigInteger(String val)String表示是我们要处理的数字,参数必须要符合数字格式
2.BigInteger使用
构造:BigInteger(String val) ->根据传入的符合数字形式的字符串,创建BigInteger对象方法:add(BigInteger value)加subtract(BigInteger value)减multiply(BigInteger value)乘divide(BigInteger value)除
public class Demo01BigInteger {public static void main(String[] args) {BigInteger b1 = new BigInteger("12121212121212121212121");BigInteger b2 = new BigInteger("12121212121212121212121");//add(BigInteger value)加BigInteger add = b1.add(b2);System.out.println(add);//subtract(BigInteger value)减BigInteger subtract = b1.subtract(b2);System.out.println(subtract);//multiply(BigInteger value)乘BigInteger multiply = b1.multiply(b2);System.out.println(multiply);//divide(BigInteger value)除BigInteger divide = b1.divide(b2);System.out.println(divide);}}
第三章.BigDecimal类
1.BigDecimal介绍
1.作用:专门处理小数(解决float和double型的小数直接做运算可能出现精度损失的问题)
2.BigDecimal使用
1.构造:BigDecimal(String s)->s必须要是 数字形式2.方法:add(BigDecimal value)加subtract(BigDecimal value)减multiply(BigDecimal value)乘divide(BigDecimal value)除如果除不尽,会报错:ArithmeticExceptionBigDecimal divide(BigDecimal divisor,int scale,int roundingMode)- divesor:此 BigDecimal 要除以的值。- scale:保留的位数- roundingMode:舍入方式舍入方式:BigDecimal类提供静态的成员变量来表示舍入的方式- BigDecimal.ROUND_UP 向上加1。- BigDecimal.ROUND_DOWN 直接舍去。- BigDecimal.ROUND_HALF_UP 四舍五入。
public class Demo02BigDecimal {public static void main(String[] args) {BigDecimal b1 = new BigDecimal("3.55");BigDecimal b2 = new BigDecimal("2.12");//add(BigDecimal value)加BigDecimal add = b1.add(b2);System.out.println(add);//subtract(BigDecimal value)减BigDecimal subtract = b1.subtract(b2);System.out.println(subtract);//multiply(BigDecimal value)乘BigDecimal multiply = b1.multiply(b2);System.out.println(multiply);//divide(BigDecimal value)除BigDecimal divide = b1.divide(b2);//因为两个数除不尽,导致报错了System.out.println(divide);}}
public class Demo03BigDecimal {public static void main(String[] args) {BigDecimal b1 = new BigDecimal("3.55");BigDecimal b2 = new BigDecimal("2.12");BigDecimal divide = b1.divide(b2, 3, BigDecimal.ROUND_UP);System.out.println(divide);}}
第四章.Date日期类
1.Date类的介绍
1.概述:类 Date 表示特定的瞬间,精确到毫秒2.毫秒:时间单位1秒 = 1000毫秒毫秒都是用long型表示3.时间原点:1970年1月1日0时0分0秒4.地理知识:我们所在的地区:东8区,比时间原点所在时区多8个小时一个时区差一个小时0度经线:本初子午线划分南北半球:赤道
2.Date类的使用
1.构造:Date()->获取的是当前系统时间Date(long time)->相当于设置时间,从时间原点开始算
public class Test01 {public static void main(String[] args) {Date date = new Date();System.out.println(date);System.out.println("=====================");Date date1 = new Date(1000L);System.out.println(date1);}}
3.Date类的常用方法
long getTime() ->获取当前系统时间的毫秒值void setTime(long time)->为时间赋值,传递毫秒值,从时间原点开始往后推算
public class Test02 {public static void main(String[] args) {Date date = new Date();long time = date.getTime();System.out.println(time);Date date1 = new Date(1623033586680L);System.out.println(date1);System.out.println("=======================");Date date2 = new Date();date2.setTime(2000L);System.out.println(date2);}}
经验值:
将一段代码快速封装到一个方法中:选中要封装的代码,按ctrl+alt+M
第五章.Calendar日历类
1.Calendar介绍
1.概述:日历类,是一个抽象类2.获取:static Calendar getInstance()3.方法:- public int get(int field):返回给定日历字段的值。- public void set(int field, int value):将给定的日历字段设置为给定值。- public abstract void add(int field, int amount):根据日历的规则,为给定的日历字段添加或减去指定的时间量。- public Date getTime():将Calendar转成Date对象日历字段: Calendar中的字段(成员变量),都是静态的
![day12[常用API] - 图1](/uploads/projects/liuye-6lcqc@vk53cd/b729b68d2f4ea6ea0e23fa2230e6e2a3.png)
public class Test01 {public static void main(String[] args) {//获取Calendar对象Calendar calendar = Calendar.getInstance();//System.out.println(calendar);//- public int get(int field):返回给定日历字段的值。int year = calendar.get(Calendar.YEAR);System.out.println(year);//- public void set(int field, int value):将给定的日历字段设置为给定值。/* calendar.set(Calendar.YEAR,2022);int year1 = calendar.get(Calendar.YEAR);System.out.println(year1);*///- public abstract void add(int field, int amount):根据日历的规则,为给定的日历字段添加或减去指定的时间量。/* calendar.add(Calendar.YEAR,-1);int year2 = calendar.get(Calendar.YEAR);System.out.println(year2);*///- public Date getTime():将Calendar转成Date对象Date time = calendar.getTime();System.out.println(time);System.out.println("===============");//void set(int year, int month, int date) 设置年月日calendar.set(2021,2,1);System.out.println(calendar.get(Calendar.YEAR));System.out.println(calendar.get(Calendar.MONTH));}}
第六章.SimpleDateFormat日期格式化类
1.DateFormat介绍
1.概述:DateFormat日期格式化类,抽象类2.使用:用子类->SimpleDateFormat3.怎么使用:a.构造:SimpleDateFormat(String pattern)pattern:代表的是指定的日期格式yyyy-MM-dd HH:mm:ss中间连接符可以变,但是字母不能变
![day12[常用API] - 图2](/uploads/projects/liuye-6lcqc@vk53cd/83869a5bc8bd196e7431978e8b033233.png)
2.DateFormat常用方法
1.String format(Date date) -> 将Date对象表示的时间按照指定的格式去格式化,返回的是String2.Date parse(String source) -> 将符合规则的字符串时间表示形式转成Date对象
public class Test {public static void main(String[] args) throws ParseException {format();parse();}private static void parse() throws ParseException {//创建SimpleDateFormatSimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");String time = "2021-1-1 00:00:00";Date date = sdf.parse(time);System.out.println(date);}private static void format() {//创建SimpleDateFormatSimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");Date date = new Date();String format = sdf.format(date);System.out.println(format);}}
第七章.JDK8新日期类
1. LocalDate 本地日期
1.1.获取LocalDate对象
1.static LocalDate now()2.static LocalDate of(int year, Month month, int dayOfMonth)
public class Test01 {public static void main(String[] args) {// 1.static LocalDate now()LocalDate localDate = LocalDate.now();System.out.println(localDate);// 2.static LocalDate of(int year, Month month, int dayOfMonth)LocalDate localDate1 = LocalDate.of(2020, 1, 1);System.out.println(localDate1);}}
1.2.获取日期字段的方法 : 名字是get开头
- int getYear() 获取年份- int getDayOfMonth()返回月中的天数- int getMonthValue() 返回月份
public class Test02 {public static void main(String[] args) {// 2.static LocalDate of(int year, Month month, int dayOfMonth)LocalDate localDate1 = LocalDate.of(2020, 1, 1);//- int getYear() 获取年份System.out.println(localDate1.getYear());//- int getMonthValue() 返回月份System.out.println(localDate1.getMonthValue());//- int getDayOfMonth()返回月中的天数System.out.println(localDate1.getDayOfMonth());}}
1.3.设置日期字段的方法 : 名字是with开头
- LocalDate withYear(int year)设置年份- LocalDate withMonth(int month)设置月份- LocalDate withDayOfMonth(int day)设置月中的天数
public class Test03 {public static void main(String[] args) {LocalDate localDate = LocalDate.now();//- LocalDate withYear(int year)设置年份LocalDate localDate1 = localDate.withYear(2020);//- LocalDate withMonth(int month)设置月份LocalDate localDate2 = localDate1.withMonth(1);//- LocalDate withDayOfMonth(int day)设置月中的天数LocalDate localDate3 = localDate2.withDayOfMonth(1);System.out.println(localDate3);}}
1.4.日期字段偏移
- 设置日期字段的偏移量, 方法名plus开头,向后偏移- 设置日期字段的偏移量, 方法名minus开头,向前偏移
public class Test04 {public static void main(String[] args) {//plus();minus();}/*日期偏移,向前方法l:minusXXX()xxx代表具体的字段:年 月 日*/private static void minus() {LocalDate localDate = LocalDate.now();LocalDate localDate1 = localDate.minusMonths(1);System.out.println(localDate1.getMonthValue());}/*日期偏移,向后方法:plusxxx()xxx代表具体字段*/private static void plus() {LocalDate localDate = LocalDate.now();LocalDate localDate1 = localDate.plusMonths(10);System.out.println(localDate1.getMonthValue());}}
2.Period和Duration类
2.1 Period 计算日期之间的偏差
- static Period between(LocalDate d1,LocalDate d2)计算两个日期之间的差值.- getYears()->获取相差的年- getMonths()->获取相差的月- getDays()->获取相差的天
public class Test05 {public static void main(String[] args) {LocalDate localDate1 = LocalDate.of(2021, 2, 2);LocalDate localDate2 = LocalDate.of(2020, 1, 1);//- static Period between(LocalDate d1,LocalDate d2)计算两个日期之间的差值.Period period = Period.between(localDate2, localDate1);System.out.println(period);//- getYears()->获取相差的年System.out.println("相差的年:"+period.getYears());//- getMonths()->获取相差的月System.out.println("相差的月:"+period.getMonths());//- getDays()->获取相差的天System.out.println("相差的天:"+period.getDays());}}
2.2 Duration计算时间之间的偏差
static Duration between(Temporal d1,Temporal d2)计算两个日期之间的差值.参数传递Temporal的子类->LocalDateTimetoMinutes()->获取相差分钟toDays()->获取相差天数toMillis()->获取相差秒(毫秒为单位)toHours()->获取相差小时
public class Test06 {public static void main(String[] args) {/*创建LocalDateTime对象,可以操作年月日时分秒创建:static LocalDateTime now()static LocalDateTime of(int year, int month, int dayOfMonth, int hour, int minute, int second)*/LocalDateTime local1 = LocalDateTime.of(2020, 1, 1, 10, 10, 10);LocalDateTime local2 = LocalDateTime.of(2021, 2, 2, 20, 20, 20);/*LocalDateTime是Temporal实现类*///static Duration between(Temporal d1,Temporal d2)计算两个日期之间的差值.Duration duration = Duration.between(local1, local2);//toDays()->获取相差天数System.out.println("相差:"+duration.toDays()+"天");//toHours()->获取相差小时System.out.println("相差:"+duration.toHours()+"个小时");//toMinutes()->获取相差分钟System.out.println("相差:"+duration.toMinutes()+"分钟");//toMillis()->获取相差秒(毫秒为单位)System.out.println("相差:"+duration.toMillis()+"秒");}}
2.3 DateTimeFormatter
1.概述:日期格式化对象2.方法:- static DateTimeFormatter ofPattern(String str)自定义的格式s:我们自己定义的格式TemporalAccessor:接口实现类:LocalDate LocalDateTime等- TemporalAccessor parse(String s)字符串解析为日期对象static LocalDateTime from(TemporalAccessor temporal)- String format(TemporalAccessor t)将日期按照格式转成String
public class Test07 {public static void main(String[] args) {//parse();format();}private static void format() {//获取DateTimeFormatter对象DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");String format = dtf.format(LocalDateTime.now());System.out.println(format);}private static void parse() {//获取DateTimeFormatter对象DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");String time = "2020-01-01 00:00:00";TemporalAccessor parse = dtf.parse(time);System.out.println(parse);LocalDateTime from = LocalDateTime.from(parse);System.out.println(from);}}
第八章.System类
1.特点:构造被私有了不能new对象方法是static的2.方法:static long currentTimeMillis() ->获取当前系统时间毫秒值static void exit(int status) -> 退出jvmstatic void arraycopy(Object src, int srcPos, Object dest, int destPos, int length)->数组复制src:源数组srcPos:代表的是从原数组的哪个索引开始复制dest:目标数组destPos:从目标数组的哪个索引开始粘贴length:复制多少个
public class Test01 {public static void main(String[] args) {long start = System.currentTimeMillis();for (int i = 0; i < 100; i++) {//退出jvmSystem.exit(0);System.out.println("我爱大数据");}long end = System.currentTimeMillis();System.out.println(end-start);}}
public class Test02 {public static void main(String[] args) {/*static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length)->数组复制src:源数组srcPos:代表的是从原数组的哪个索引开始复制dest:目标数组destPos:从目标数组的哪个索引开始粘贴length:复制多少个*/int[] arr1 = {1,2,3,4,5,6,7,8,9};int[] arr2 = new int[10];System.arraycopy(arr1,0,arr2,0,5);System.out.println(Arrays.toString(arr2));}}
第九章.Arrays数组工具类
1.概述:数组工具类2.特点:a.构造私有化b.成员都是static的c.类名直接调用3.方法:static int binarySearch(int[] a, int key) ->二分查找static void sort(int[] a) ->对数组中的元素进行排序->升序static String toString(int[] a) ->按照指定格式打印static int[] copyOf(int[] original, int newLength) ->数组扩容
public class Test03 {public static void main(String[] args) {//static int binarySearch(int[] a, int key) ->二分查找//binarySearch();//static void sort(int[] a) ->对数组中的元素进行排序->升序//sort();//static int[] copyOf(int[] original, int newLength) ->数组扩容copyOf();}private static void copyOf() {int[] arr = {1,2,3,4,5};/*copyOf->底层实现原理先new了一个新数组通过数组复制(System.arrayCopy)将老数组中的元素复制到新数组中然后返回新数组*/arr = Arrays.copyOf(arr, 10);System.out.println(Arrays.toString(arr));}private static void sort() {int[] arr = {5,3,6,4,4,6,8,5};Arrays.sort(arr);//static String toString(int[] a) ->按照指定格式打印System.out.println(Arrays.toString(arr));}private static void binarySearch() {int[] arr = {11,22,33,44,55,66};int index = Arrays.binarySearch(arr, 11);System.out.println(index);}}
第十章.包装类
1.基本数据类型对应的引用数据类型(包装类)
1.包装类:基本类型对应的引用类型基本类型 包装类byte Byteshort Shortint Integerlong Longfloat Floatdouble Doublechar Characterboolean Boolean2.注意:基本类型和对应的包装类之间自动转换3.拆箱 装箱 -> 自动的装箱:将基本类型转成对应的包装类拆箱:将包装类转成对应的基本类型
public class Test01 {public static void main(String[] args) {Integer i = 10;//装箱System.out.println(i+1);ArrayList<Integer> list = new ArrayList<>();list.add(1);//添加元素 -> 自动装箱//获取,根据索引获取//Integer integer = list.get(0);int element = list.get(0);//自动拆箱System.out.println(element);}}
2.Integer的介绍以及使用
1.概述:int对应的包装类2.装箱:public Integer(int value)public Integer(String s)public static Integer valueOf(int i)public static Integer valueOf(String s)3.注意:我们传递的字符串必须是数字形式4.拆箱:int intValue()
public class Test02 {public static void main(String[] args) {//public Integer( int value)Integer integer = new Integer(1);System.out.println(integer+1);//public Integer(String s)Integer integer1 = new Integer("1");System.out.println(integer1+1);//public static Integer valueOf ( int i)Integer integer2 = Integer.valueOf(1);System.out.println(integer2+1);//public static Integer valueOf (String s)Integer integer3 = Integer.valueOf("1");System.out.println(integer3+1);System.out.println("==================");int i = integer3.intValue();System.out.println(i);}}
3.基本类型和String之间的转换
3.1 基本类型往String转换
1.直接拼接 用+2.通过String类静态方法valueOf(int i)
public class Test03 {public static void main(String[] args) {//1.直接拼接 用+int i = 1;System.out.println(i+"啊哈哈哈");//2.通过String类静态方法valueOf(int i)String s1 = String.valueOf(1);System.out.println(s1+"哈哈哈");}}
3.2 String转成基本类型
基本类型中有都静态方法parseXXX()->XXX代表具体转成什么基本类型- public static byte parseByte(String s):将字符串参数转换为对应的byte基本类型。- public static short parseShort(String s):将字符串参数转换为对应的short基本类型。- public static int parseInt(String s):将字符串参数转换为对应的int基本类型。- public static long parseLong(String s):将字符串参数转换为对应的long基本类型。- public static float parseFloat(String s):将字符串参数转换为对应的float基本类型。- public static double parseDouble(String s):将字符串参数转换为对应的double基本类型。- public static boolean parseBoolean(String s):将字符串参数转换为对应的boolean基本类型。注意:String必须是数字形式
public class Test04 {public static void main(String[] args) {int i = Integer.parseInt("111");System.out.println(i+1);}}
