Java基础知识强化65:基本类型包装类之Integer的构造方法

1. Integer类概述

(1)Integer类在对象中包装了一个基本类型 int 的值,Integer类型的对象包含一个int类型的字段。

(2)该类提供了多个方法,能在int类型和String类型之间互相转换,还提供了处理int类型时候非常有用的其他一些常量和方法。

 

2. Integer的构造方法

(1)public Integer(int value);

(2)public Integer(String s);

 

3. Integer类的案例

 1 package cn.itcast_02;
 2 
 3 /*
 4  * Integer的构造方法:
 5  * public Integer(int value)
 6  * public Integer(String s)
 7  *         注意:这个字符串必须是由数字字符组成
 8  */
 9 public class IntegerDemo {
10     public static void main(String[] args) {
11         // 方式1
12         int i = 100;
13         Integer ii = new Integer(i);
14         System.out.println("ii:" +ii);//ii:100 ,不是地址值(Integer重写了toString()方法)
16         // 方式2
17         String s = "100";
18         // NumberFormatException
19         // String s = "abc";
20         Integer iii = new Integer(s);
21         System.out.println("iii:" + iii);
22     }
23 }

 运行效果是:

posted on 2015-09-24 19:09  鸿钧老祖  阅读(291)  评论(0编辑  收藏  举报

导航