在上一篇文章中,我们回顾到了java常用类的String类,现在我们接着回顾java中的其他常用类。
一、八种包装类型
在java中有八种基本数据类型,这八种基本数据类型都有对应的包装类。
八种包装类型属于引用数据类型,父类都是Object,所有的包装类型都是由final修饰的,所以所有的包装类型都是不可变对象。
包装类的类层次结构是:
拆箱和装箱
装箱:基本数据类型转换成包装类。
拆箱:包装类型转换成基本数据类型。
在jdk5之后 ,引入了一种新特性,叫做自动装箱,自动拆箱,也就是在程序编译时,需要基本数据类型的地方,即使你写的时包类型,他也会自动帮你转换成基本数据类型。在八种包装类型中,只有Boolean和Character中没有valouOf()方法,除了这两个包装类,其他类都有相互转换的方法。
public class StringTest {
public static void main(String[] args) {
//自动装箱和自动拆箱
Integer integer = 10;//自动把基本数据类型10,装箱成了包类型
Integer in = new Integer(10);
int a = 5;
System.out.println(a + in);//15 自动拆箱,把包类型in自动拆箱成基本数据类型。
}
}
日期类
常用的日期类:
java.util.Date
java.text.SimpleDateFormat
java.util.Calendar
直接调用Date类的构造方法就可以获取系统当前时间,但是这个时间的格式不符合中国人阅读时间的习惯。
public class StringTest {
public static void main(String[] args) {
Date date = new Date();
System.out.println(date);//Wed Nov 25 15:57:36 CST 2020
}
}
所以我们使用日期格式化的包来把日期的格式进行转换
public class StringTest {
public static void main(String[] args) {
Date date = new Date();
System.out.println(date);//Wed Nov 25 15:57:36 CST 2020
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(format.format(date));//2020-11-25 16:00:31
}
}
在这个类中
yyyy代表年:一共四位
MM:代表月
dd:日
HH:时
mm:分
ss:秒
SSS:毫秒
日期格式中年月日不能乱写,但是连接年月日的符号格式可以自由组织。
String format(Date date); 这是方式是把时间按照指定字符串显示的方法。
把字符串转换成时间的方法:Date parse(String s);
注意:此方法有ParseException(解析异常)/
public class StringTest {
public static void main(String[] args) throws ParseException {
Date date = new Date();
System.out.println(date);//Wed Nov 25 15:57:36 CST 2020
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(format.format(date));//2020-11-25 16:00:31
String time = "2020-11-25 16:00:31 444";
SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");
Date date1 = format1.parse(time);
}
}
获取从1970年1月1日到当前系统时间的总毫秒数:
long untilTime = System.currentTimeMillis();
System.out.println(untilTime);//1606292701703
这个方法属于System类,他最常用的作用是用来统计方法执行时间。
数字类
数字格式化,他可以把数字按照你想要的格式输出:
public class StringTest {
public static void main(String[] args){
/***
* 数字格式规则
* # 代表任意一个数字
* , 代表千分位
* . 代表小数点
*/
DecimalFormat decimalFormat = new DecimalFormat("###,###.##");//表示加入千分位,保留两位小数
float f = 1223.12345423f;
String s = decimalFormat.format(f);
System.out.println(s);
}
}
BigDecimal
BigDecimal是财务软件专用,精度非常高。属于引用数据类型,在财务软件中double是不够用的,必须使用精度非常高的BigDecimal
//这个300不是普通的300.是精度非常高的300
BigDecimal bigDecimal = new BigDecimal(300);
System.out.println(bigDecimal);//300
System类:
随机数类 Random
此类能够产生随机数,用法如下
public class StringTest {
public static void main(String[] args){
//创建一个随机类对象
Random random = new Random();
int num = random.nextInt(101);//产生一个随机数,从[0,100],不包括101
System.out.println(num);
//输出5个,从[0,100]的整数
for (int i = 0; i < 5; i++){
System.out.print(random.nextInt(101) + " ");
}
}
}
枚举类 enum
枚举类主要用于返回结果是多种情况时,如果返回情况只有两种建议使用布尔类型。
枚举也是一种引用数据类型,编译后产生class文件。
用法:
import java.util.Scanner;
public class StringTest {
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
int c = scanner.nextInt();
if (c > 0 && c <= 3){
System.out.println(四季.SPRING);
}else if (c > 3 && c <= 6){
System.out.println(四季.SUMMER);
}else if (c > 6 && c <= 9){
System.out.println(四季.AUTUMN);
}else if (c > 9 && c <= 12){
System.out.println(四季.WINTER);
}else {
System.out.println(四季.WRONG);
}
}
}
//定义四季枚举
enum 四季{
SPRING,SUMMER,AUTUMN,WINTER,WRONG
}
java常用类就到此结束了,下一篇文章开始,我们将回顾异常。