1.包装类
java有八大基本数据类型 ,都有与之对应的包装类
包装类能实例化对象,包装类下面封装了很多方法可供使用
基本数据是没有的,所以要学他们的包装类
int —> Integer
short —> Short
byte —> Byte
long —> Long
char —> Character
boolean —> Boolean
float —> Float
double —> Double
除了char和int ,其他都是首字母大写
【重点】
1.JDK1.5版本以后,有装箱和拆箱装箱:将基本数据类型转为包装类对象拆箱:将包装类转为基本数据类型2.***Value();将包装类对象转为基本数据类型(类似上面的自动拆箱)的一个方法3.toString(变量名);将基本数据类型转为字符串类型的数据4.【重点】 parse***(变量名);将字符串类型转为基本数据类型5.valueOf(变量名);将基本数据类型转为包装类类型(类似于自动装箱)
package com.qfedu.test1;//装箱、拆箱public class Demo1 {public static void main(String[] args) {//30本身是int 有自动的强转(byte和short都有)// byte byte1 = (byte)30;byte byte1 = 30;//自动强转未byte类型//然后自动装箱,将byte类型的数据变成Byte包装类的对象Byte byte2 = 20;//这是不自动装箱的效果Byte byte3 = new Byte((byte) 50);//byte3是byte的包装类型数据,byte4是byte的基本数据类型//自动拆箱,将包装类对象转为基本数据类型byte byte4 = byte3;//***Value 使用方法进行拆箱Integer i1 = new Integer(45);int i2 = i1.intValue();System.out.println(i2);Short s1 = 45;short s2 = s1.shortValue();//toString();将基本数据类型转为字符串类型的数据String string1 = Integer.toString(i2);System.out.println(string1);//转为字符串了//parse***();将字符串类型转为基本数据类型 【重点】//将字符串78转为int78int int6 = Integer.parseInt("78");System.out.println(int6);//valueOf();将基本数据类型转为包装类类型(类似于自动装箱)byte b9 = 56;Byte byte9 = Byte.valueOf(b9);}}
简约版
package com.qfedu.test1;public class Demo2Test {public static void main(String[] args) {int i1 = 16;Integer i2 = i1;//装箱Short short3 = 30;short short4 = short3;//拆箱Byte byte5 = 20;byte byte6 = byte5.byteValue();//类似拆箱int i7 = 78;String string8 = Integer.toString(i7);//基本类型转字符串String string9 = "999";int i10 = Integer.parseInt(string9);//字符串转基本类型byte b11 = 56;Byte b12 = Byte.valueOf(b11);//类似装箱}}
2.Math
专门用来处理数学公式的一个类,都是静态方法,直接类名调用
绝对值 Math.abs(-100)
最大值 Math.max(12, 88) Math.max(10, Math.max(20, 30))//三个数比
最小值 Math.min(10, 20)
向上取整 Math.ceil(3.3) 输出4.0
向下取整 Math.floor(3.3) 输出3.0
四舍五入 Math.round(4.3) 返回值int
随机数 Math.random() [0,1)左开右闭 (int)(Math.random()*(100-50)+50)
指数 Math.pow(2, 3) 2的3次方
离最近的整数 Math.rint(1.5) 1.5离1.0和2.0一样远近,如果有两个,返回偶数
3.Random
专门处理随机数的,比Math.random提供的方法多
package com.qfedu.test3Random;import java.util.Random;//Random 随机数生成器public class Demo1 {public static void main(String[] args) {Random random = new Random();//在自己的范围内进行随机System.out.println(random.nextBoolean());//在T、F范围随机System.out.println(random.nextInt());//在int范围内随机System.out.println(random.nextDouble());//在double范围内随机}}
4.System
系统类,不能被实例化(没有构造方法),提供了静态属性和静态方法
package com.qfedu.test4System;import java.util.Properties;public class Demo1 {public static void main(String[] args) {System.out.println("out流");// System.err.println("错误流");//从197.1.1 0:0:0 到现在的毫秒数System.out.println(System.currentTimeMillis());//获取电脑的信息,确定系统属性Properties properties = System.getProperties();//系统名字System.out.println(properties.get("os.name"));//系统版本System.out.println(properties.get("os.version"));//系统用户名字System.out.println(properties.get("user.name"));//当前项目路径System.out.println(properties.get("user.dir"));//使用java的版本System.out.println(properties.get("java.version"));}}输出:out流1649387512875Windows 1010.0ZHUD:\eclipse-workspace\day4081.8.0_241
5.Runtime
获取java运行时的一些信息,不能被实例化(没有构造方法),通过getRuntime在获取
Runtime runtime = Runtime.getRuntime();
package com.qfedu.test5Runtime;import java.io.IOException;public class Demo1 {public static void main(String[] args) throws IOException {//不能实例化,只能靠这种方式搞出对象Runtime runtime = Runtime.getRuntime();//返回java虚拟机尝试使用的最大内存量,以字节为单位/1024/1024 变成M单位System.out.println(runtime.maxMemory()/1024/1024);//java虚拟机可用的内存量System.out.println(runtime.freeMemory()/1024/1024);//java虚拟机的内存总量,会随着时间变化System.out.println(runtime.totalMemory()/1024/1024);//exec(String command)//可以使用这个打开电脑的某个程序-->微信的.exe位置runtime.exec("D:\\微信\\WeChat\\WeChat.exe");}}
6.util包下的Date类
过期的一些方法
package com.qfedu.test6Date;import java.util.Date;import javax.xml.crypto.Data;public class Demo1 {public static void main(String[] args) {Date date = new Date();//Fri Apr 08 14:14:56 CST 2022System.out.println(date);//获取年份-1900System.out.println(date.getYear() + 1900);//老外的0代表1月 所以要 + 1System.out.println(date.getMonth() + 1);//打印的是这周的第几天 0是星期天 1星期一 5星期五 6星期六System.out.println(date.getDay());System.out.println(date.getHours());//时System.out.println(date.getMinutes());//分System.out.println(date.getSeconds());//秒}}
7.Calendar类
用来处理日期、时间的一个抽象类,不能被实例化,替换了Date。
通过getInstance()来获取他的方法
Calendar calendar = Calendar.getInstance();
package com.qfedu.test7Calendar;import java.text.SimpleDateFormat;import java.time.Year;import java.util.Calendar;import java.util.Date;public class Demo1 {public static void main(String[] args) {Calendar calendar = Calendar.getInstance();System.out.println(calendar.get(Calendar.YEAR));//外国习惯从0开始 所以 + 1System.out.println(calendar.get(Calendar.MONTH) + 1);//获取当月的第几天System.out.println(calendar.get(Calendar.DAY_OF_MONTH));//获取这一周的第几天 周日是第一天 周五是第六天System.out.println(calendar.get(Calendar.DAY_OF_WEEK));//获取这一年的第几天System.out.println(calendar.get(Calendar.DAY_OF_YEAR));//时 12小时制System.out.println(calendar.get(Calendar.HOUR));System.out.println(calendar.get(Calendar.MINUTE));//分System.out.println(calendar.get(Calendar.SECOND));//秒System.out.println("----------------------------");Date date = calendar.getTime();System.out.println(date);//对上面date格式化,弄成能看懂的格式SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");SimpleDateFormat sdf1 = new SimpleDateFormat("y-M-d H:m:s");// SimpleDateFormat sdf1 = new SimpleDateFormat("YYYY-MM-DD HH:MM:SS");System.out.println(sdf.format(date));System.out.println(sdf1.format(date));}}
练习:指定日期到现在的天数
package com.qfedu.test7Calendar;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date;public class Demo2 {public static void main(String[] args) throws ParseException {SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");String string = "2022-04-01";//讲字符串类型数据进行解析成Date类型才可以计算Date date = sdf.parse(string);//将时间转换成毫秒数 long 才能进行计算差值long dateTime = date.getTime();//2022.4.1到1970.1.1之间的毫秒数Date date1 = new Date();long todayTime = date1.getTime();//获取当前日期到1970.1.1的毫秒数long time = todayTime - dateTime;//除1000毫秒变秒 除60变分 除60变小时 除24变天System.out.println(time/1000/60/60/24);}}
