Does Java actually pass a value or a reference?

Source: Internet
Author: User

1. Simple types are passed by value

When the parameters of a Java method are simple types, they are passed by value. We can use a simple example to illustrate this point:

public class Test {
public static void test(boolean test) {
test =  ! test;
System.out.println("In test(boolean) : test = " + test);
}
public  static void main(String[] args) {
boolean test = true;
System.out.println ("Before test(boolean) : test = " + test);
test(test);
System.out.println ("After test(boolean) : test = " + test);
}
}

Run Result:

Before test(boolean) : test = true
In test(boolean) : test =  false
After test(boolean) : test = true

It is not difficult to see that, although the value of the passed parameter is changed in the test (Boolean) method, there is no effect on the parameter source variable itself, that is, the test variable in the main (string[]) method. That means that when a parameter type is a simple type, it is passed by value. When passing a variable of a simple type as a parameter, it is actually a copy of the parameter's value into the method function, then how to change its value in the method function, and the result is to change only the copied value, not the source value.

2. What is a reference

Whether Java is a value or a reference, the problem is mainly in the delivery of objects, because the simple type in Java is not referenced. Since the argument refers to the reference to this thing, in order to understand the problem, we have to know what the reference is.

To put it simply, a reference is like the name or alias of an object, in which an object requests a space to hold the data, depending on the size of the object, it may need to occupy a space of varying sizes. When accessing objects, we do not directly access the data in memory of the object, but by reference. A reference is also a data type, and we can think of it as something similar to a pointer in C, which indicates the address of an object in memory-except that we can't see what the address is.

If we define more than one reference to the same object, the references are different because the reference is also a data type and requires a certain amount of memory space to save. But their values are the same, indicating that the same object is in place in memory. Like what

String a = "Hello";
String b = a;

Here, A and B are different two references, and we use two definition statements to define them. But their values are the same, pointing to the same object "Hello". Maybe you don't think it's intuitive enough. Because the value of the String object itself is immutable (like B = "world"; b = A; this does not change the value of the object of "world", but rather changes the value of its reference B to point to another String object a). So let's use StringBuffer to give an example:

public class Test {
public static void main(String[] args)  {
StringBuffer a = new StringBuffer("Hello");
StringBuffer b = a;
b.append (", World");
System.out.println("a is " + a);
}
}

Run Result:

a is  Hello, World

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.