JavaSE:一些工具类

448 阅读3分钟

001.包装类

//自动装箱:基本类型 -->包装类
Integer d1 = 1;    //通过反编译发现本质是通过Integer.valueOf()方式

//自动拆箱:包装类 --> 基本类型
Integer d2 = new Integer(2);
int d3 = d2;  // //通过反编译发现本质是通过Integer.intValue()方式

//装箱:基本类型 -->包装类
int data1 = 1;
Integer data2 = Integer.valueOf(data1);

//拆箱:包装数据类-->基本数据类型
Integer data3 = new Integer(2);
int data4 = data3.intValue();

//包装类 --> 字符串String
Integer aa = new Integer(2);
String str = aa.toString();

//字符串String -->包装类
String str="123";
Integer bb = new Integer(str);  //通过new
Integer cc = Integer.valueOf(str);  //通过valueOf()

//字符串String -->基本类型
String str="123";
int num = Integer.parseInt(str);

0011.为什么有了基本类型还需要包装类型?

  • 包装类里面具有一些有用的方法和属性,如HashCode、ParseInt;
  • 基本类型不能赋予null值;
  • 如集合、泛型不能直接用基本类型;
class Student{
    float score; //默认值为0.0f
}
因为score默认值是0.0f,代表学生成绩为0.0f,无法表示学生缺考情况,如果score是引用数据类型的话,默认值是null,我们可以将score为null的时候代表学生缺考

002.Math

System.out.println(Math.ceil(9.2));  //向上取整 10.0
System.out.println(Math.floor(9.8)); //向下取整 9.0

//四舍五入
System.out.println(Math.round(9.62)); //10
System.out.println(Math.round(-9.62)); //-10

//取平方根
System.out.println(Math.sqrt(-9));  //NaN
System.out.println(Math.sqrt(9)); //3.0

//随机数
int num1 = (int) (Math.random()*10);
System.out.println(num1);    //[0,9]的随机数

int num2 = (int) (Math.random()*8)+2;
System.out.println(num2); //[2,9]的随机数

//取绝对值
int num3 = Math.abs(-1);
System.out.println(num3);  //1

003.Random

Random random = new Random();
int num = random.nextInt(9)+2;
System.out.println(num); //[2,10]的随机数

004.Date

Date date = new Date();
System.out.println(date);//Sun Dec 01 19:37:33 GMT+08:00 2019

System.out.println(date.toLocaleString());//2019-12-1 19:38:02

//DateFormat 格式化日期
Date date = new Date();
DateFormat dateFormat = DateFormat.getDateInstance();
String result = dateFormat.format(date);
System.out.println(result);

dateFormat = DateFormat.getDateTimeInstance();
result = dateFormat.format(date);
System.out.println(result);  //2019-12-1 19:49:14

date = dateFormat.parse(result); //将字符串日期转为Date对象
System.out.println(date); //Sun Dec 01 19:50:28 GMT+08:00 2019

//SimpleDateFormat 格式化日期,也是DateFormat的子类
Date date = new Date();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
String result = simpleDateFormat.format(date);
System.out.println(result);  //2019年12月01日 19:55:47

//将字符串转为Date对象时,注意格式要一致
date = simpleDateFormat.parse("2019年12月01日 19:55:47");
System.out.println(date);  //Sun Dec 01 19:55:47 GMT+08:00 2019

005.Calendar

和 Date一样都表示日期类,但对日期操作建议使用该类

Date date = new Date();
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
Date otherDate = calendar.getTime();

System.out.println(calendar.get(Calendar.YEAR));//获取年份
System.out.println(calendar.get(Calendar.MONTH)+1); //获取月份
System.out.println(calendar.get(Calendar.DAY_OF_MONTH)); //获取该月的日
System.out.println(calendar.get(Calendar.HOUR));  //获取小时
System.out.println(calendar.get(Calendar.HOUR_OF_DAY)); //获取小时(24小时制)
System.out.println(calendar.get(Calendar.MINUTE)); //获取分
System.out.println(calendar.get(Calendar.SECOND));//获取秒

006.Object

0061.equals方法

在Object类中,equals方法将判断两个对象是否具有相同的引用;如果两个对象具有相同的引用,则它们一定相等

0062.hashCode方法

散列码(hash code)是由对象导出的一个整型值

0061.hashCode()与equals()

  • 两个对象equals得到true,则hashcode是相等的;
  • 两个对象equals得到false,但hashcode有可能相等;
  • 两个对象的hashcode相等,则equals的值不一定是true;
  • 两个对象的hashcode不同,则equals的值一定是false;