java 传值问题

简介: public class Cat { private String id; private String name; public String getId() { return id; } public void setId(String id) { this.
public class Cat {
private String id;
private String name;
public String getId() {
	return id;
}
public void setId(String id) {
	this.id = id;
}
public String getName() {
	return name;
}
public void setName(String name) {
	this.name = name;
}
public Cat(String id, String name) {
	this.id = id;
	this.name = name;
}

private static void changeName(Cat cat){
	cat.setName("other");
}

public static void main(String[] args) {
	Cat cat = new Cat ("1","kitty");
	changeName(cat); 
	System.out.println(cat.getName());
}
/*
 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());
    }

    private 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());
    }

    private static void tripleSalary(Employee x) {
        x.raiseSalary(200);
        System.out.println("End of method: salary=" + x.getSalary());
    }

    private static void tripleValue(double x) {
        x=3*x;
        System.out.println("End of Method X= "+x);
    }
}


Testing tripleValue:
Before: percent=10.0
End of Method X= 30.0
After: percent=10.0

Testing tripleSalary:
Before: salary=50000.0
End of method: salary=150000.0
After: salary=150000.0

Testing swap:
Before: a=Alice
Before: b=Bob
End of method: x=Bob  //可见引用的副本进行了交换
End of method: y=Alice
After: a=Alice  //引用本身没有交换
After: b=Bob

 */

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

捐助开发者

在兴趣的驱动下,写一个免费的东西,有欣喜,也还有汗水,希望你喜欢我的作品,同时也能支持一下。 当然,有钱捧个钱场(右上角的爱心标志,支持支付宝和PayPal捐助),没钱捧个人场,谢谢各位。



 
 
 谢谢您的赞助,我会做的更好!

 

 

目录
相关文章
|
6月前
|
存储 Java
Java-参数传值
Java-参数传值
59 4
|
Java 测试技术 数据处理
Java多线程父线程向子线程传值解决方案 2
Java多线程父线程向子线程传值解决方案
131 0
|
Java
Java多线程父线程向子线程传值解决方案 1
Java多线程父线程向子线程传值解决方案
387 0
|
Java
JAVA参数传值机制中值传递和引用传递
JAVA参数传值机制中值传递和引用传递
94 0
|
Java
08 java中方法传参的传值与传址问题
java的值传递与址传递 问题:在java中方法传参到底是值传递还是址传递?
101 0
08 java中方法传参的传值与传址问题
|
存储 Java C++
基于堆栈内存详析 Java函数形参是传值还是引用? | C++指针与Java引用的区别 | C++引用、指针等之间的区别 | C++与Java类的实例化的区别
基于堆栈内存详析 Java函数形参是传值还是引用? | C++指针与Java引用的区别 | C++引用、指针等之间的区别 | C++与Java类的实例化的区别
|
Java
Java - 参数传递(传值、传址/引用)
Java - 参数传递(传值、传址/引用)
363 0
Java - 参数传递(传值、传址/引用)
|
Java C语言
【JavaSE基础】深入了解Java的传值和传引用
【JavaSE基础】深入了解Java的传值和传引用
134 0
【JavaSE基础】深入了解Java的传值和传引用
|
Java Windows
Java是传值还是传引用?
Java是传值还是传引用?
|
Java
Java 传参传值
Java 传参传值 案例代码 public class PassParamExplain { public static void main(String[] args) { //example A System.
10431 0