English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
First copy an interview/The question of the written exam:
When an object is passed as a parameter to a method, if this method can change the properties of the object and return the changed result, then is it pass by value or pass by reference?
Answer:
It is pass by value. Java language method calls only support passing parameters by value. When an object instance is passed as a parameter to a method, the value of the parameter is a reference to the object. The properties of the object can be changed during the call, but changes to the object reference will not affect the caller.++In C#, you can change the value of the passed parameter by passing by reference or by output parameter. In C#, you can write code as shown below, but this is not possible in Java.
Pass by value and pass by reference (pass by reference) in Java, it is necessary to understand these two concepts, and to understand pass by value and pass by reference.
Let's take a simple example to illustrate:
For example, if you go on a trip abroad and take a particularly good photo, and you want to share it with your friends, there are two ways: the first is to send the photo directly to your friends, that is, to give your friends a copy of the photo; the second is to upload this excellent photo to a QQ (Weibo) and so on, and you will get an address (URL) to access this photo, and then share this URL with your friends.
As shown in the example above, the first can be considered as passed by value, and the second can be considered as passed by reference (passed by reference). Knowing this concept, let's proceed with a simple analysis below.
Passed by value:Java passes parameters in this way only when the parameter is a primitive type variable.
In the case of sharing photos, if your friend gets a copy of the photo, then the friend's modification of the photo will not affect your photo, and your modification of the photo will not affect the photo you shared with your friend.
Passed by reference:Java passes parameters in this way only when the parameter is a reference type variable.
If the URL address is given to a friend, then if the friend also has the permission to modify, when the friend performs operations on the photo, the photo accessed by the friend is the result after the friend's operations.
Let's explain this example through code below:
package com.dufy.reforvalue; import java.util.Arrays; /** * Pass by value and pass by reference in Java * Passed by value: primitive type variables-Passed by value, passed by value gets a copy of the parameter by copying * Passed by reference: reference type variables-Passed by reference, passed by reference passes the reference address of the object * * @author dufy * @creation 2017Year2Month9Day */ public class ReferenceOrValue { /** * Primitive types, passed by value * For example: Share your photo with a friend, the friend receives an actual copy of your photo, * You and your friends can operate on your respective photos without affecting each other's photos! */ public static void testVal(int photo){ photo++;//The friend modifies the photo System.out.println("My friend see photo = " + photo); } /** * Reference types: Pass by address * For example: Share your photo with a friend, the URL (address) of the photo you uploaded to the internet is shared, * You and your friends can access the photo through this address and perform an operation on the photo! */ public static void testRef(Photo photo){ photo.setPhoto("java Photo,Great!");//Your friend modifies your photo System.out.println("My friend see photo = " + photo.getPhoto()); } /** * Reference types: Pass by address * Because arrays are reference types, the references are passed in, so their values are swapped in the method, which also necessarily affects their original values. */ public static void testArrayRef(int[] array){ for (int i = 0; i < array.length; i++) { array[i] = 0; } System.out.println("testArrayRef array is = "+Arrays.toString(array)); } public static void main(String[] args) { //One: Pass by value int photo = 10;//Define the photo to be sent testVal(photo);//Send the photo to your friend, and your friend gets a copy System.out.println("My see photo = " + photo); //Two: Pass by address Photo p = new Photo();//Define a photo object, a java photo I took myself p.setPhoto("java photo"); testRef(p);//Send the photo object (i.e., the URL address) to your friend, and your friend gets a URL (address). The photo is only displayed when the URL is opened. System.out.println("My friend see photo = " + p.getPhoto()); //Three: Arrays are also objects, arrays are in heap memory. References are on the stack. int array[] = {1,2,3,4,5}; testArrayRef(array); System.out.println("array is = "+Arrays.toString(array)); } /** * Photo class */ static class Photo{ String photo; public String getPhoto() { return photo; } public void setPhoto(String photo) { this.photo = photo; } } }
The output result is as follows:
My friend see photo = 11
My see photo = 10
My friend see photo = java Photo,Great!
My friend see photo = java Photo,Great!
testArrayRef array is = [0, 0, 0, 0, 0]
array is = [0, 0, 0, 0, 0]
Interlude: There is the following question: How should this method be written?
public static void main(String[] args) { int a = 10; int b = 20; method(a,b);//It is required to print out a= only after the method is called!100,b=200, please write the method(a,b) method! System.out.println("a = ", + a); System.out.println("b = ", + b); }
Certainly, many people may write the following code without thinking, just like me before:
private static void method(int a, int b) { a*=10; b*=10; }
However, after running the result, you will find that the desired result is not printed out!
At this point, if you carefully read the above introduction, it is not difficult to understand why such results appear!
Here are the possible correct results for this question:
private static void method(int a, int b) { System.out.println("a=",100,b=200"); System.exit(0); }
The above-mentioned is what the editor introduces to everyone about Java value passing and reference passing (common in interviews), hoping it will be helpful to everyone. If you have any questions, please leave a message, and the editor will reply to everyone in time. Here, I also want to express my heartfelt thanks to everyone for their support of the Yell Tutorial website!
Statement: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been manually edited, and does not assume any relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (Please replace # with @ when sending an email to report abuse, and provide relevant evidence. Once verified, this site will immediately delete the infringing content.)