参考:alex_lo的博客,网址:http://www.cnblogs.com/alexlo/p/3493755.html
Java基本数据类型有:byte8,short16,int32,long64,float32,double64,cha16,boolean1
1 基本数据类型,只传值
public class TestFun {
public static void testInt(int i){
i=5; //i先等于0,之后赋值为5
}
public static void main(String[] args) {
int a=0 ;
TestFun.testInt(a);
System.out.println("a="+a);
}
}
输出仍为0,其为基本数据类型,传递的只是一个副本(字面量引用变量的副本),因此方法针对副本的修改不会影响数据本身。
2 对象传递,引用的对象
public class Example2
{
static void check(StringBuffer obj)
{
obj.append(“JAVA”);
}
public static void main(String[]args)
{
StringBuffer x=new StringBuffer(“Hello ”);
check(x);
System.out.println(“Example2.x=”+x);
}
} 输出为Hello JAVA
此为对象(String,StringBuffer,类对象引用,接口引用和数组等)的传参:传递的是该数据对象的某个引用变量而不是对象内容本身,传入后都对引用内容进行修改,这样可以修改引用的内容。
3
Public class Example3
{
static void check(String obj)
{
obj=“JAVA”;
}
public static void main(String[]args)
{
String x=”Hello ”;
check(x);
System.out.println(“Example3.x=”+x);
}
} 输出为Hello
都是引用,但String是final不可变的,其类型对象不可变,则不会通过引用该对象进行任何改变。
8万+

被折叠的 条评论
为什么被折叠?



