一 包装类
为什么要用包装类
java中的基本数据类型不是面向对象的,这时就需要用包装类将基本数据类型转化为对象
基本数据类型 包装类
byte Byte
boolean Boolean
short Short
char Character
int Integer
long Long
float Float
double Double
1 运用
1.1 将基本类型转换为引用类型
byte b=10;
Byte b1=new Byte(b);
1.2 获取最大值和最小值
System.out.println(Integer.MAX_VALUE);
System.out.println(Integer.MIN_VALUE);
1.3 类型转换
int类型转换为Integer
Integer i1=new Integer(10);
Integer转换为int
int i2=i1.intValue();
把纯数字字符串转换为int类型,不能有小数点
int i3= Integer.parseInt("123");
double 允许有一个小数点
double d=Double.parseDouble("1.2");
将int类型的值转换为二进制的字符串表示形式
String s1=Integer.toBinaryString(10);
转换为八进制
String s2=Integer.toOctalString(10);
转换为十六进制
String s3=Integer.toHexString(10);
String转换为Integer
Integer i3=Integer.valueOf("10");
Integer转换为String
String s1=i3.toString();
String转换为int
int i4=Integer.parseInt("10");
int转换为String
String s2=2+"";
1.4 自动装箱和拆箱
JDK1.5以后装箱拆箱自动转换
Integer i1=2;
会自动加Integer.valueOf(2);
int i2=i1;
会自动加i1.intValue();
二 System
代表系统,long currentTimeMillis()获取当前系统时间的毫秒数,从1970-01-01 0:0:0 000开始到现在的时间毫秒数
System.exit(0) 关闭JVM 0是正常退出 非0异常退出
三 Date
表示特定的瞬间,精确到毫秒
1 创建Date对象获取当前系统时间
Date d1=new Date ();
获取时间原点到指定毫秒数的时间
d1=new Date(1000);
2 时间格式化
创建格式化对象指定格式
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss SSS");
对时间进行格式化,返回字符串类型
String strDate = sdf.format(d1);
3 Calendar
获取日历对象 使用Calendar.getInstance()方法调用它的子类GregorianCalendar的构造器
四 Math
数学操作
五 BigInteger
就是高精度 可以存储超过int类型的最大值的数据
而且可以进行加减乘除取余操作
六 Random
随机数 先创建随机数生成器
Random r=new Random();
int result = r.nextInt(10);
后面的数字就是大于等于0且小于这个数的随机整数
示例: