Passing by value and passing by reference in Java with interview questions

Interview question sharing

Click me to go directly

2023 latest interview collection link

2023 major factory interview questions PDF

Interview questions PDF version

java, python interview questions

Project Practice: Best Practices for AI Text OCR Recognition

AI Gamma generates PPT tool direct link with one click

Play with cloud Studio, the online coding tool

Play with GPU AI painting, AI speech, and translation, GPU lights up the AI ​​imagination space

Sharing of the most complete documentation of AI painting stablediffusion in history

AI painting about SD, MJ, GPT, SDXL encyclopedia

AI painting stable diffusion Midjourney official GPT document AIGC encyclopedia data collection

AIGC information package

introduction

In Java programming, we often hear discussions about passing by value and passing by reference. These two concepts involve how data is passed between methods. Understanding these concepts is crucial to writing Java programs correctly. In this article, we will delve into what pass by value and pass by reference are and why there is only pass by value in Java.

What is pass by value?

Passing by value is a way of passing data, which is to pass a copy of the data to a method or function. When we pass a variable to a method, the method receives a copy of the original data, not the original data itself. This means that modifications to parameters within the method will not affect the original data.

Here is a simple Java code example to illustrate passing by value:

public class ValuePassingDemo {
    
    
    public static void main(String[] args) {
    
    
        int x = 10;
        modifyValue(x);
        System.out.println("x = " + x);
    }

    public static void modifyValue(int value) {
    
    
        value = 20;
    }
}

In this example, we define a modifyValuemethod named , which takes an integer parameter value, and then valuemodifies the value of 20. However, in mainthe method, we can see that xthe value of , is still 10. This is because modifyValueinside the method, valuemodifications to xthe value will not be affected. This is the characteristic of passing by value.

What is pass by reference?

Pass-by-reference is a data transfer method that passes the reference or address of data to a method or function. This means that modifications to parameters within the method will affect the original data. In some programming languages, such as C++, passing by reference can be achieved, but in Java, there is no real passing by reference.

Why is there only passing by value in Java?

In Java, although we often hear about passing by reference, in fact, Java only supports passing by value. This is because all data types in Java are objects, including basic data types (such as int, double, etc.). In Java, a reference to an object is passed to a method, not the object itself.

Let's illustrate this with an example:

public class ReferencePassingDemo {
    
    
    public static void main(String[] args) {
    
    
        int[] arr = {
    
    1, 2, 3};
        modifyArray(arr);
        System.out.println("arr[0] = " + arr[0]);
    }

    public static void modifyArray(int[] array) {
    
    
        array[0] = 100;
    }
}

In this example, we define an array of integers arrand then pass it to modifyArraythe method. Inside the method, we modify the first element of the array to be 100. However, if we run this program, we will find arr[0]that the value of 100 has indeed become 100. This is because in Java, arrays are objects, and modifyArraymethods receive references to arrays, so modifications to the array will affect the original array.

Although there is such a behavior that seems to be passed by reference in Java, in fact, it is still passed by value in Java. The method receives a copy of the reference, not the original reference itself. This means that inside the method, if we reassign the parameter to a new object, the original reference will not be affected. For example:

public class ReferencePassingDemo2 {
    
    
    public static void main(String[] args) {
    
    
        StringBuilder str = new StringBuilder("Hello");
        modifyString(str);
        System.out.println("str = " + str);
    }

    public static void modifyString(StringBuilder s) {
    
    
        s = new StringBuilder("World");
    }
}

In this example, although the parameters modifyStringare reassigned inside the method s, the value of is still "Hello" mainwithin the method . strThis is because modifyStringthe method receives stra copy of the , not the original reference.

Parameter passing in Java

In Java, whether it is a basic data type or an object, parameters are passed by value. This is because parameter passing actually passes a copy of the parameter value, regardless of whether the parameter is a primitive data type or an object reference.

When we pass an object to a method, the method receives a copy of the reference to the object. This reference copy points to the same object, so we can modify the object's state inside the method, but not the reference itself.

in conclusion

In Java, the only way to pass parameters is by value. Whether it is a primitive data type or an object, the method receives a copy of the parameter value. That's why in Java you often hear about pass by value, not pass by reference.

Understanding the difference between passing by value and passing by reference is crucial to writing correct Java programs. When using methods, we need to clearly know how the method parameters are passed to avoid unnecessary confusion and errors.

Guess you like

Origin blog.csdn.net/weixin_42373241/article/details/132732090