0
点赞
收藏
分享

微信扫一扫

数据类型、包装类、String

janedaring 2022-01-05 阅读 62

数据类型、包装类、String

文章目录


String类型 —>基本数据类型、包装类

调用包装类的parseXxx(String s)

		String str1 = "123";
		//错误的情况:
		//int num1 = (int)str1;
		//Integer in1 = (Integer)str1;
		//可能会报NumberFormatException
		int num2 = Integer.parseInt(str1);

基本数据类型、包装类—>String类型

调用String重载的valueOf(Xxx xxx)

		int num1 = 10;
		//方式1:连接运算
		String str1 = num1 + "";
		//方式2:调用String的valueOf(Xxx xxx)
		String str2 = String.valueOf(num1);

基本数据类型 —>包装类

调用包装类的构造器
Integer内部定义了IntegerCache结构,IntegerCache中定义了Integer[],保存了从-128-127范围的整数。如果我们使用自动装箱的方式,给Integer赋值的范围在-128~127范围内时,可以直接使用数组中的元素,不用再去new了。目的:提高效率

		int num1 = 10;
		Integer in1 = new Integer(num1);

		//自动装箱
		Integer in2 = num1;

包装类—>基本数据类型

调用包装类Xxx的xxxValue()

		Integer in1 = new Integer(12);		
		int i1 = in1.intValue();
		
		//自动拆箱
		int i2 = in1;
举报

相关推荐

0 条评论