class Value
{
public int i = 15;
}public class Test
{
public static void main(String argv[])
{
Test t = new Test();
t.first();
} public void first()
{
int i = 5;
Value v = new Value();
v.i = 25;
second(v, i);
System.out.println(v.i + " 第四" + i); } public void second(Value v, int i)
{
System.out.println(v.i + " 第一" + i);
i = 0;
v.i = 20;
System.out.println(v.i + "第二 " + i);
Value val = new Value();
v = val;
System.out.println(v.i + " 第三 " + i);
}
}结果是:为什么??
25 第一5
20第二 0
15 第三 0
20 第四5高人指点!
[email protected]
http://my.6cncn.cn
{
public int i = 15;
}public class Test
{
public static void main(String argv[])
{
Test t = new Test();
t.first();
} public void first()
{
int i = 5;
Value v = new Value();
v.i = 25;
second(v, i);
System.out.println(v.i + " 第四" + i); } public void second(Value v, int i)
{
System.out.println(v.i + " 第一" + i);
i = 0;
v.i = 20;
System.out.println(v.i + "第二 " + i);
Value val = new Value();
v = val;
System.out.println(v.i + " 第三 " + i);
}
}结果是:为什么??
25 第一5
20第二 0
15 第三 0
20 第四5高人指点!
[email protected]
http://my.6cncn.cn
解决方案 »
- JAVA中ArrayList是否允许两个线程同时进行插入和删除操作?
- NC5.6环境搭建
- 请教一个集合的问题
- JAVA 编程!!!
- java中的class类继承JFrame,为什么出现JPanel上的控件不能移动被锁定了还出现一个空对象的异常但运行没错误???高手帮帮!!!
- 华为公司 java 面试题 大家一起来讨论
- 在JEditorPane里显示网页,响应点击链接事件的问题
- Swing程序一段时间不操作自动跳转到首页,如何实现???
- 如何用管道实现线程间多次通信,不是一次
- 数据库引擎损坏是指什么???如何解决呢?在线等候!!谢谢(来者有分)
- 如何用Process控制Process实例(exec方法返回的),能向Process进程中输入数据用getInputStream()
- 【求助】如何遍历一个二叉树!(老总快给我逼疯了,急啊)
-------------------前面一,二,三都没问题第四个的vi.i为什么是20
i的道理就很显然了,一定是5。
second返回以后,依旧返回作为参数的对象v,而这个对象v的i已经被更改为20;
i的道理就很显然了,一定是5。
--------------------------------------------------------------------------- second(v, i);
把v传进去了!可是当
System.out.println(v.i + "第二 " + i);
Value val = new Value();
v = val;//重新赋直了!难道外面的变量v 没有指到new Value();
--------------------------------------------
public void second(Value v, int i)传人的v和函数second的参数v是什么关系?
v是相当于在public void second里面的一个变量吗?
当调用方法second(v, i);的意思是
second(Value v=v, i);
------------------------------------------
v是一个变量可以指到其他地方吗?
如v = new Value();
当调用方法second(v, i);的意思是
second(Value v=v, int i=i);
------------------------------------------
v是一个局部(方法里面的)变量可以指到其他地方
如v = new Value();
Value val = new Value();
v = val;//是这句吧,其实这句可以理解成C中的指针,但是在这个second内有效方法,
System.out.println(v.i + " 第三 " + i);
PS:java对类的传递不同于C++,不是引用调用,而是值传递
顺便附上一段core java 的源代码,大家仔细体会
@version 1.00 2000-01-27
@author Cay Horstmann
*/public class ParamTest
{
public static void main(String[] args)
{
/*
Test 1: Methods can't modify numeric parameters
*/
System.out.println("Testing tripleValue:");
double percent = 10;
System.out.println("Before: percent=" + percent);
tripleValue(percent);
System.out.println("After: percent=" + percent); /*
Test 2: Methods can change the state of object
parameters
*/
System.out.println("\nTesting tripleSalary:");
Employee harry = new Employee("Harry", 50000);
System.out.println("Before: salary=" + harry.getSalary());
tripleSalary(harry);
System.out.println("After: salary=" + harry.getSalary()); /*
Test 3: Methods can't attach new objects to
object parameters
*/
System.out.println("\nTesting swap:");
Employee a = new Employee("Alice", 70000);
Employee b = new Employee("Bob", 60000);
System.out.println("Before: a=" + a.getName());
System.out.println("Before: b=" + b.getName());
swap(a, b);
System.out.println("After: a=" + a.getName());
System.out.println("After: b=" + b.getName());
} public static void tripleValue(double x) // doesn't work
{
x = 3 * x;
System.out.println("End of method: x=" + x);
} public static void tripleSalary(Employee x) // works
{
x.raiseSalary(200);
System.out.println("End of method: salary="
+ x.getSalary());
} public static void swap(Employee x, Employee y)
{
Employee temp = x;
x = y;
y = temp;
System.out.println("End of method: x=" + x.getName());
System.out.println("End of method: y=" + y.getName());
}
}class Employee // simplified Employee class
{
public Employee(String n, double s)
{
name = n;
salary = s;
} public String getName()
{
return name;
} public double getSalary()
{
return salary;
} public void raiseSalary(double byPercent)
{
double raise = salary * byPercent / 100;
salary += raise;
} private String name;
private double salary;
}
谁顶给谁分!
http://community.csdn.net/Expert/topic/5055/5055552.xml?temp=.322262