J2SE第六章——常用类(三)包装类

public static int parseInt(String s) {
			把字符串转换成基本数据类型
		}

包装类        

         数据类型:基本数据类型+引用型(对象)

         基本类型    ========   封装类

         boolean                 Boolean

         byte                    Byte

         short                   Short

         int                     Integer

         long                    Long

         float                   Flaot

         double                  Double

         (以Integer为例讲解)

 

2.1 构造函数

      public  Integer(int);  整型100 封装成Integer对象

      public  Integer(String);  "100"

public class Test {
	public static void main(String[] args) {
		Integer i1 = new Integer(100);
		Integer i2 = new Integer("100")
	}
}

2.2静态成员变量  

         最大整数:Integer.MAX_VALUE;  public static finalint MAX_VALUE = 0x80000000;

         最小整数: Integer.MIN_VALUE;  public static final int MAX_VALUE =0x7fffffff;


2.3成员方法

                   2.3.1 parseInt()     <=======>    String.valueOf()

public static int parseInt(String s) {
	//把字符串转换成基本数据类型
}
public class Test {
	public static void main(String[] args) {
		String qqStr = "110";
		int qq = Integer.parseInt(qqStr); 

		System.out.println(qq+1);  //111
	}
}

         2.3.2 intValue()

public int intValue() {
	//把包装类转化成基本数据类型
}
public class Test {
	public static void main(String[] args) {
		Integer inte = new Integer(100);	// 基本整数100 转化成 对应包装类对象

		int i = inte.intValue();	//包装类对象还原成基本整数100
		System.out.println(i+1);  //101

		Integer in = 200;  // 自动拆箱
		int i = in;  // 也不出错,自动拆箱

	}
}
public class Test {
	public static void main(String[] args) {
		Integer i = new Integer(100);
		Double d = new Double("123.456");
		int j = i.intValue() + d.intValue();
		System.out.println(j);  // 223

		int i = Integer.parseInt("lss");
		// java.lang.NumberFormatException  (数字格式异常)
	}
}







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值