java中数据类型可分为两大类:类型(reference)和基本类型(primitive)。
1)基本类型
基本类型有8种:boolean、char、byte、short、int、long、float、double。它们都有相应的包装类,这些包装类都属于类型,它们依次是:Boolean、Character、Byte、Short、Integer、Long、Float、Double。
2)类型
所有
java对象都是通过对象(Object References)进行访问的,类似于C++中的指针,这个指向堆heap中的某块区域,实际的对象存在于heap中。
例如,声明如下代码:
int prim = 10;
Integer refer = new Integer(10);
这两者在
内存中的布局如下图所示:
49_3710_8cb2574ebdd5ade.gif[删除]
至此,有人可能会想:当这两种类型作为函数参数传递时,到底是值传递(value)还是传递(reference),还是两种类型各自为政?其实
java中的函数参数都是以值方式传递的。见代码片段:
[
java]
import
java.awt.Point;
/**
*
java中参数都以传值方式传递,而不是传方式传递
* @author ASCE1885
*
*/
public class PassByValue {
public static void modifyPoint(Point pt, int in) {
//这里的pt是入参pt的的副本,而不是入参pt的副本,即
java以传值方式传递pt的
//也就是说,pt和入参pt现在指向的是同一个Point对象,详见图示
pt.setLocation(5, 5);
in = 15; //这里的in其实是入参in的一个副本,所有的更改都是对in而言,跟入参in无关
System.out.println("During modifyPoint " + "pt = " + pt + "and in = " + in);
}
public static void main(String[] args) {
Point p = new Point(0,0);
int i = 10;
System.out.println("Before modifyPoint " + "p = " + p + "and i = " + i);
modifyPoint(p, i);
System.out.println("After modifyPoint " + "p = " + p + "and i = " + i);
}
}
49_3710_537080c3268b0b8.gif[删除]
上面代码的输出是:
49_3710_278f9c06e2f0cd0.gif[删除]