Java—包装类
一、包装类的概念
1.简介:将基本数据类型的数据封装成一个类,可以使用类的方法以及属性;
2.特点:int类型与char类型的包装比较特殊之外,其他包装类都是首字母大写
3.罗列:
| 基本数据类型 | 包装类 |
| byte | Byte |
| short | Short |
| int | Integer |
| long | Long |
| double | Double |
| float | Float |
| boolean | Boolean |
| char | Character |
二、Integer
1.概念:Integer类在对象中包装了一个基本数据类型即 int 的值;该类提供了多个方法,能在int类型和String类型之间互相转换;提供处理int类型时非常有用的其他一些常量和方法;
2.常量:
| 常量的名称 | 常量的描述 |
| public static final int MAX_VALUE | 值为常量,表示int类型能够表示的最大值 |
| public static final int MIN_VALUE | 值为常量,表示int类型能够表示的最小值 |
| public static final int SIZE | 表示int值的比特位数 |
| public static final Class TYPE | 表示基本数据类型int的Class实例 |
案例1:
1 public class test05 {
2 public static void main(String[] args) {
3 //获取int类型最大值
4 System.out.println(Integer.MAX_VALUE);//2147483647
5 //获取int类型最小值
6 System.out.println(Integer.MIN_VALUE);//-2147483648
7 //获取比特位 一个字节占8bit
8 System.out.println(Integer.SIZE);//32
9 //获取class实例
10 System.out.println(Integer.TYPE);//int
11 }
12 }
3.构造方法
| 构造方法的名称 | 构造方法的描述 |
| public Integer(int value) | 将int的值包装成包装类Integer |
| public Integer(String s) | 将一个字符串的数值包装成Integer |
4.常用方法
| 方法的名称 | 方法的描述 |
| public static String toBinaryString(int i) | 将十进制的整数转化为二进制字符 |
| static String toOctalString(int i) | 将十进制的整数转化为八进制字符串 |
| public static String toHexString(int i) | 将十进制的整数转换为十六进制字符串 |
| public int xxxValue() | 将Integer转换各种基本数据类型(xxx为各种进本数据类型) |
| public static int parseInt(String s) | 将字符串转换为整数类型 |
| public static Integer valueOf(int i) | 返回一个表示指定的int值的Integer实例 |
| public static Integer valueOf(String s) | 指定的String的值的Integer对象 |
5.自动拆箱与自动滚箱
(1)装箱:将基本数据类型包装成包装类(也就是引用数据类型)的过程;
(2)拆箱:将包装类(引用数据类型)拆成基本数据类型的过程;
(3)在jdk1.5之后,为自动装箱与自动拆箱;
(4)自动装箱(将基本数据类型---->引用数据类型):也就是可以将基本数据类型变量,赋值给包装类的引用;
(5)自动拆箱(将引用数据类型----->基本数据类型):将包装类的引用,赋值给基本数据类型的变量;
案例2:
1 public class test05 {
2 public static void main(String[] args) {
3 //定义一个包装类
4 Integer in = new Integer(5);
5 //自动拆箱
6 int num = in;
7 System.out.println(num);
8 //自动装箱
9 Integer i = num;
10 System.out.println(i);
11 }
12 }
案例3:
1 import java.util.Arrays;
2
3 public class test05 {
4 public static void main(String[] args) {
5 //定义一个字符串
6 String str="100 200 -1 30 90 899 56 996";
7 //根据空格进行拆分
8 String[] arrays = str.split(" ");
9 Integer[] in = new Integer[arrays.length];
10 //使用循环遍历
11 for (int i=0; i<arrays.length;i++){
12 //将字符串转换成整数类型,并赋值给整数类型数组in
13 in[i]=Integer.parseInt(arrays[i]);
14 }
15 //对数组内容进行排序
16 Arrays.sort(in);
17 //将结果变为字符串输出
18 System.out.println(Arrays.toString(in));
19 }
20 }
三、Math
1.概念:包含基本数学运算的方法;
2.常量:
| 常量的名 | 常量的描述 |
| public static final double E | 自然对数的底数 |
| public static final double PI | π |
3.常用的方法
| 方法的名称 | 方法的描述 |
| public static int abs(int a) | 绝对值 |
| public static double cbrt(double a) | 立方根 |
| public static double sqrt(double a) | 正平方根 |
| public static double ceil(double a) | 向上取整 |
| public static double floor(double a) | 向下取整 |
| public static double long round(double a) | 四舍五入 |
| public static double max(double a, double b) | 较大值 |
| public static long min(long a,long b) | 较小值 |
| public static double pow(double a,double b) | 返回第一个参数的第二个参数次幂的值 |
| public static double random() | 大于等于0.0且小于1.0 |
五、System
1.概念:System类包含一些有用的类字段和方法。它不能被实例化,只提供了一个私有的构造方法;在System类提供的设施中,有标准的输入、标准的输出和错误输出流;
2.常量
| 方法的名称 | 方法的描述 |
| public static final PrintStream err | 标准错误输出流(关联控制台) |
| public static final InputStream in | 标准输入流(关联键盘) |
| public static final PrintStream out | 标准输入流(关联控制台) |
3.常用的方法
| 方法的名称 | 方法的描述 |
| public static long currentTimeMills() | 返回当前系统的毫秒值(1970年1月1日) |
| public static void exit(int status) | 终止当前正在运行的Java虚拟机(非0状态码表示异常终止) |
| public static void gc() | 运行垃圾回收器 |
六、Date
1.概念:表示特定的瞬间,精确到毫秒,是一个时间类;
2.构造方法:public Date() 初始化date,表示当前系统的时间;
3.常用方法:void setTime(long time);long getTime();
七、Calendar
1.概念:Calendar类是一个抽象类,它为特定的瞬间与一组诸如YEAR\MONTH\DAY_OF_MONTH\HOUR等日历字段之间的转化提供了一些方法;如:1970年1月1日的00:00:00;
2.常用方法
| 方法名 | 解释 |
| public static Calendar getInstance() |
返回的Calendar基于当前时间 |
| public final Date getTime() |
将Calendar转换为Date |
| public final void setTime(Date date) |
给指定的Date设置此Calendar的时间 |
| public void set(int feild,int value) |
将给定的日历字段设置给指定值 |
| public int get(int field) |
返回给定日历字段值 |
| public abstract void add(int field, int amount) |
根据日历的规制,给指定的字段添加或者减去指定的时间量 |

浙公网安备 33010602011771号