包装类(如,Integer、Double等)这些类封装了一个相应的基本数据类型数值,并为其提供了一系列操作。
一般基本数据类型分配在栈上,那么要想让它分配在堆上那么就可以用基本类型的包装类。
基本数据类型与之对应的包装类的对应关系表
byte | short | int | long | float | double | char | boolean |
Byte | Short | Integer | Long | Float | Double | Character | Boolean |
(1)以java.lang.Integer类为例,构造方法(还是那句老话,想弄清楚它的方法还是看api文档或者源码)
复制代码
1
2
3
4public Integer(int value) public Integer(String s) throws NumberFormatException { this.value = parseInt(s, 10); }
(2)以java.lang.Integer类为例,常见方法
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26//最小的int型数(-231) @Native public static final int MIN_VALUE = 0x80000000; //最大的int型数(2^31-1) @Native public static final int MAX_VALUE = 0x7fffffff; //返回封装数据的int值 public int intValue() { return value; } //返回封装数据的double值 public double doubleValue() { return (double)value; } //返回封装数据的long值 public long longValue() { return (long)value; } //将字符串类型转为int型数据并返回 public static int parseInt(String s) throws NumberFormatException { return parseInt(s,10); } //返回Integer对象,其中封装的整型数据为字符串s所表示 public static Integer valueOf(String s) throws NumberFormatException { return Integer.valueOf(parseInt(s, 10)); }
(3)以java.lang.Integer类为例,常见方法使用举例(自己动手去写写体会会深一点)
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37/** * 说明:包装类方法使用举例 * * @author huayu * @date 2018/8/30 9:47 AM */ public class IntegerDemo { public static void main(String[] args) { Integer integer=new Integer(10); int a=integer.intValue(); double b=integer.doubleValue(); long c=integer.longValue(); try { int d = Integer.parseInt("12"); System.out.println("d="+d); }catch (NumberFormatException e){ e.printStackTrace(); System.out.println("输入数据出错"); } try { //double类型非强转这儿肯定会报错 int d = Integer.parseInt("1.2"); System.out.println("d="+d); }catch (NumberFormatException e){ //NumberFormatException是运行时异常,可捉可不捉这个在以前的博客讲过异常处理 e.printStackTrace(); System.out.println("输入数据出错"); } int dou=1; Integer e=Integer.valueOf(dou); System.out.println("a="+a+" b="+b+" c="+c+" e="+e); System.out.println(Integer.MAX_VALUE); System.out.println(Integer.toBinaryString(2)); } }
(4)应用小实验
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42/** * 说明:将字符串转换为double类型的数组(下面的注释为了让大家看的清楚写的过细了) * 思路:1、先分解字符串 * 2、将分解好的字符串转为double类型放入double数组中 * @author huayu * @date 2018/8/30 10:13 AM */ public class StringToDoubleArray { public static void main(String[] args) { //定义一个二维数组 double[][] doubles; //题目是转为double类型,所以字符串中应能在分解后转为double类型 String str = "1,2;3,4,5;6,7,8"; //第一次分解按照行分解 String[] flistSplit = str.split(";"); //分配第一维空间 doubles = new double[flistSplit.length][]; for (int i = 0; i < flistSplit.length; i++) { // System.out.println(flistSplit[i]); //第二次分解按照列分解 String[] secondSplit = flistSplit[i].split(","); //分配第二维空间 doubles[i] = new double[secondSplit.length]; //将原字符串处理结束后转为double类型并放入Double数组 for (int j = 0; j < secondSplit.length; j++) { // System.out.println(secondSplit[j]); doubles[i][j] = Double.parseDouble(secondSplit[j]); } } //打印数组看结果 for (int i = 0; i < doubles.length; i++) { for (int j = 0; j < doubles[i].length; j++) { System.out.println("i=" + i + " j=" + j + " "+doubles[i][j] + " "); } System.out.println(); } } }
最后
以上就是傻傻小馒头最近收集整理的关于【java基础】基本数据类型的包装类的全部内容,更多相关【java基础】基本数据类型内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复