标签:his 数学 自动 装箱 封装 system 文档 ble oat
Java采用一切皆对象的编程方式,在从C++到JAVA的跨越中...
对于基本数据类型,如 int,double,char等,java也应当将其看做对象。
于是将基本数据类型进行封装操作,使其变为对象。
示例 对于int型
class MyInt{
private int n;
public MyInt(){}
public MyInt(int n){
this.n=n;
}
}
构造的目的是为了将基本数据类型传递给对象
class MyInt{
private int n;
public MyInt(){}
public MyInt(int n){
this.n=n;
}
//将包装的内容返回
public int intValue(){
return this.n;
}
}
使用包装类:
class MyInt{
private int n;
public MyInt(){}
public MyInt(int n){
this.n=n;
}
public int intValue(){
return this.n;
}
}
public class t3 {
public static void main(String[] args) {
MyInt mi=new MyInt(66);
int number=mi.intValue();
System.out.println(number);
}
}
在JAVA系统自动提供了包装类,
对基本数据类型
byte
short
int
long
float
double
char
boolean
其中除了int对应的对象Integer和char对应的对象Character,其余的全部是首字母大写
即是:Byte Short Integer Long Float Doubel Character Boolean
其中又分为两类:
1、对象型包装类:Character Boolean
2、数值型包装类:其他
对象型包装类是Object类的直接子类(看文档),而其他,比如double,中间还有一个Number抽象类


自动装箱与自动拆箱
对象进行数学计算(在C++中需进行运算符重载,才能对对象进行相应的运算)
public class t4 {
public static void main(String[] args) {
Integer i=24;//自动装箱
int ret=i;//自动拆箱
i*=2;//对象进行数学计算(可看做C++的运算符重载)
System.out.println(ret);
System.out.println(i);
}
}
标签:his 数学 自动 装箱 封装 system 文档 ble oat
原文地址:http://www.cnblogs.com/void-m/p/6208924.html