中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

In Java, everything is pass-by-value

發布時間:2020-08-06 19:36:19 來源:ITPUB博客 閱讀:176 作者:Jewyi 欄目:編程語言
In Java, everything is pass-by-value
Q: Can you provide some authoritative quotation to support the saying "In Java, everything is pass-by-value"?
A:
From The Java Programming Language, by James Gosling et al. 3rd edition (pg. 56):

quote:



Some people will say incorrectly that objects are passed "by reference." In programming language design, the term pass by reference properly means that when an argument is passed to a function, the invoked function gets a reference to the original value, not a copy of its value. If the function modifies its parameter, the value in the calling code will be changed because the argument and parameter use the same slot in memory. The Java programming language does not pass objects by reference; it passes object references by value. Because two copies of the same reference refer to the same actual object, changes made through one reference variable are visible through the other. There is exactly one parameter passing mode -- pass by value -- and that helps keep things simple.

From The Java Tutorial

quote:


Pass by Value

In Java methods, arguments are passed by value.
When invoked, the method receives the value of the variable passed in. When the
argument is of primitive type, pass-by-value means that the method cannot change
its value. When the argument is of reference type, pass-by-value means that the
method cannot change the object reference, but can invoke the object's methods
and modify the accessible variables within the object.

This is often the
source of confusion--a rogrammer writes a method that attempts to modify the
value of one its arguments and the method doesn't work as expected. Let's look
at such method and then investigate how to change it so that it does what the
programmer originally intended.


Quiz question from Sun:
In Java programming, all method parameters are passed by value.
Answer: true
In Java, everything is pass-by-value, unless you think Dr. James Gosling forgot how he invented Java, AND Sun does not know how Java works.If we all agree those are not the cases. Then the question should change to that how to understand the fact "In Java, everything is pass-by-value".If you go up-and-down a little, you will find a lot of help here.

Two examples to prove:

Example one:(from:http://bobcat.webappcabaret.net/javachina/faq/07.htm#pas_Q2)

// PassByValueTest.java
public class PassByValueTest { 
  public static void main(String [] args) { 
    String arr[]=new String[2]; 
          
    arr[0]="hello"; 
    arr[1]="hi"; 
          
    // nothing will change here
    swap(arr[0], arr[1]); 
    System.out.println(arr[0] + ", " + arr[1]); //hello, hi
          
    // two Strings are actually swapped
    swap(arr, 0, 1); 
    System.out.println(arr[0] + ", " + arr[1]); //hi, hello
  }
        
  // useless swap method, since you swap the copies 
  // of two strings' reference inside the method 
  // Strings outside are not affected.
  public static void swap(String s1,String s2){ 
    String tmp = null;
           
    tmp  = s1; 
    s1   = s2; 
    s2   = tmp; 
  } 
        
  // real practical swap here
  // swap array elements with two indices
  // very useful for sorting algorithm, etc.
  // reference of arr will never change (pass-by-value)
  // but the contents of arr will change permanently
  // since the copy of arr reference still refer to the same object.
  public static void swap(String arr[], int ix1, int ix2) { 
    String tmp = null; 
          
    tmp      = arr[ix1]; 
    arr[ix1] = arr[ix2]; 
    arr[ix2] = tmp; 
  } 
} 

Example two(my code):

public class ParamTest {

/**
* @param args
* @author Jianming
* @version 1.0
*/

public static void main(String[] args){
/*
* Test one: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 two: Methods can modify the state of object parameters
*/

System.out.println("nTesting tripleSalary:");
Employee harry = new Employee("Harry", 5000);
System.out.println("Before: salary = " + harry.getSalary());
tripleSalary(harry);
System.out.println("After:salary = " + harry.getSalary());

/*
* Test three:Methods can't attach new objects to object parameters
*/

System.out.println("nTesting swap:");
Employee a = new Employee("Jianming", 10000);
Employee b = new Employee("James", 20000);
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){
x = 3 * x;
System.out.println("End of method:x = " + x);
}

public static void tripleSalary(Employee x){
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{
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 double salary;
private String name;

}

The result:

Testing tripleValue:
Before: percent = 10.0
End of method:x = 30.0
After: percent = 10.0

Testing tripleSalary:
Before: salary = 5000.0
End of method:salary = 15000.0
After:salary = 15000.0

Testing swap:
Before: a = Jianming
Before: b = James
End of method: x = James
End of method: y = Jianming
After: a = Jianming
After: b = James

Note:

(1):Methods can't modify the parameters of primitive type

(2):Methods can modify the state of object parameters,but methods can't attach new objects to object parameters

an example to explain the question:

電視機和遙控器可以很形象的描敘和解釋這個問題。
可以把遙控器看作是電視機的一個引用拷貝,只要電視機存在,也就是用遙控器對準一臺電視機,按遙控器上面的各種按扭(function)可以對電視機產生各種影響,但是你換一個遙控器對電視機來說是不會產生影響的,電視機并不會因為遙控器換了而變成別的電視機。同時遙控器也可以不對準原來的電視機,轉去對準別的電視機,這對原來的電視機也是不會產生影響的。一臺電視機可以有多個遙控器,并且只要某個遙控器對準了這臺電視機,這個遙控器就可以通過它上面的按扭改變電視機的狀態。

a picture to show how it works:

In Java, everything is pass-by-value

End.

[@more@]

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

含山县| 大荔县| 延寿县| 华阴市| 资兴市| 六枝特区| 鄯善县| 齐齐哈尔市| 东辽县| 南平市| 乾安县| 新兴县| 封开县| 丰都县| 信宜市| 莱西市| 隆化县| 申扎县| 淅川县| 文化| 巴南区| 永嘉县| 柳州市| 凌海市| 台前县| 琼中| 介休市| 永兴县| 南漳县| 渭南市| 筠连县| 荥经县| 宜宾市| 涪陵区| 固镇县| 汕尾市| 通许县| 遂川县| 德惠市| 乃东县| 拜泉县|