现在的位置: 首页 > 综合 > 正文

java基础之包装类

2017年05月17日 ⁄ 综合 ⁄ 共 722字 ⁄ 字号 评论关闭

(1)装箱及拆箱 
将基本数据类型变为包装类称为装箱
反之就是拆箱

package KownClass;

public class test1{
	public static void main(String args[]){
		int x = 30 ;		// 基本数据类型
		Integer i = new Integer(x) ;	// 装箱:将基本数据类型变为包装类
		int temp = i.intValue()	;// 拆箱:将一个包装类变为基本数据类型
	}
};

在包装类中 实际上还存在着最大的特点 就是可以将字符串变为指定数据类型



(2)包装类的应用(Integer,Float)

包装类在实际中用的最多的还是在于字符串变为基本数据操作上 

例如:讲一个全由数字组成的字符变为一个int或者folat类型的数据在Integer分别提供了一下两种方法


A.Integer类 字符串转int型  Int parseInt(String s)
B Float类  字符串转float型  float parseFloat(String s)

package KownClass;

public class test1{
	public static void main(String args[]){
		String str1 = "30" ;	// 由数字组成的字符串
		String str2 = "30.3" ;	// 由数字组成的字符串
		int x = Integer.parseInt(str1) ;	// 将字符串变为int型
		float f = Float.parseFloat(str2) ;	// 将字符串变为int型
		System.out.println("整数乘方:" + x + " * " + x + " = " + (x * x)) ;
		System.out.println("小数乘方:" + f + " * " + f + " = " + (f * f)) ;
	}
};

抱歉!评论已关闭.