In Java, types fall into two broad categories: value types and reference typesValue types are basic data types (such as int,double, etc.), and reference types are all types except the basic variable types (such as those defined by class). All types are allocated a certain amount of memory (parameters are allocated space when they are used, and that space disappears when the method call is complete). Basic variable types have only one space (allocated in the stack). Reference types have two storage Spaces (one in the stack and one in the heap), and Java is still confused about whether to pass values or references during function calls.

Instead of allocating A chunk of memory in the heap to hold A() to which c refers, A refers to an instance of the same A. This is the same as A pointer in C++. Let d point to the memory that B points to. It’s obvious that references in Java are similar in principle to Pointers in C++, but remember that Java doesn’t have Pointers, only references. Let’s discuss references with some more concrete code:

1. Simple types are passed by value

Java methods whose arguments are simple types are passed by value. This can be illustrated by a simple example:

package test; Public class Test {// Swap two values public static void Swap(int a,int b){int c=a; a=b; b=c; System.out.println("a: "+a); System.out.println("b: "+b); } public static void main(String[] args){ int c=10; int d=20; Swap(c,d); System.out.println("After Swap:"); System.out.println("c: "+d); System.out.println("d: "+c); }} A: 20 b: 10 After Swap: c: 20 d: 10Copy the code

The Swap (a,b) method has no effect on the source variables, i.e. the a and B variables in main(String[]). That means that when parameter types are simple, they are passed by value. When a variable of a simple type is passed as an argument, it actually copies the value of the argument into the method function. Changing the value in the method function will only change the copied value, not the source value.

  1. What is a quote

The question of whether Java passes values or references is mostly about object passing, because Java has no references for simple types. Now that the argument mentions a quote, in order to understand the question, we have to know what a quote is.

In simple terms, an * reference is just like an object’s name or alias. An object requests a chunk of memory to hold data, depending on the size of the object. When we access an object, we do not directly access the object’s data in memory, but we access it by reference. A reference is also a data type, and we can think of it as something like a pointer in C++ that indicates an object’s location in memory — except we can’t see what that location is.

If we define more than one reference to the same object, those references are not the same, because a reference is also a data type and requires a stack space to hold it. But their values are the same, indicating the location of the same object in memory (heap space). For example: String a=”This is a Text! ; String b=a;

  As you can see from the code and graphic examples above, a and B are two different references, and we use two definition statements to define them. But they all have the same value, they all point to the same object: “This is a Text!” .Note, however, that the value of the String itself is immutable (like b = “World”; b = a; Instead of changing the value of the object “World”, it changes the value of its reference b to point to another String, a).

As shown in the figure, b begins with the value of “Word Two” pointed to by the green line, and then B =a; So b points to the Word that the red line points to.

Here I describe two main points:

(1) A reference is a data type (stored in the stack) that holds the address of an object in the heap. This type is neither a simple data type nor an instance of a class.

(2) Different references may refer to the same object, in other words, an object can have multiple references, i.e. variables of the class type.

3. How are objects passed

As you learn more, you may wonder how objects are passed: are they “passed by value” or “passed by reference”?

(1) Considered to be “passed by value” :

package test; public class Test { public static void Sample(int a){ a+=20; System.out.println("a: "+a); } public static void main(String[] args){ int b=10; Sample(b); System.out.println("b: "+b); }} Run result: A: 30 B: 10Copy the code

In this code, you change the value of variable A, not variable B, so it’s “value passing.”

(2) Considered “passed by reference” :

package test; public class Test { public static void Sample(StringBuffer a){ a.append(" Changed "); System.out.println("a: "+a); } public static void main(String[] args){ StringBuffer b=new StringBuffer("This is a test!" ); Sample(b); System.out.println("b: "+b); }}./* This is a test! Changed b: This is a test! ChangedCopy the code

Sample(StringBuffer) is a function that changes the value of reference A and changes the value of reference B, so it is “passed by reference”!

So objects (remember in Java everything is an object, whether it’s int a; Or a String a; , both a and A are objects) in what way are they passed? The answer can only be: pass by value is passed by reference, but usually primitive data types (int,double, etc.) are thought of as “value passed” while custom data types (class) are thought of as “reference passed”.

4. Correctly view the issue of value or reference passing

To put the problem in perspective, one has to understand why it exists.

In fact, the problem comes from C, not Java.

There is a data type called pointer in C, so when passing a data as an argument to a function, there are two ways: passing a value, or passing a pointer. In value passing, changing the value of a variable in a function does not change the value of the original variable, but through a pointer it does.

void Swap(int a,int b){ int c=a; a=b; b=c; } void Swap(int *a,int *b){ int c=*a; *a=*b; *b=c; } int c=10; int d=20; Swap(c,d); // do not change the value of c, d Swap(&c,&d); // change the values of c and dCopy the code

Many C programmers turned to Java and discovered that using methods like SwapValue(T,T)(when T is a value type) still can’t change the value of a simple data type passed in as an argument, but if T is a reference type, its members can be arbitrarily changed. So they thought it was like a value/pointer problem in C. But without Pointers in Java, the problem becomes a value/reference problem. Unfortunately, this is not an appropriate topic to discuss in Java.

The ultimate purpose of discussing such a problem is to figure out when it is convenient to change the value of an argument in a method function and make it valid in the long run.

5. How to implement a method similar to swap

The question of passing a value or a reference has been solved, but we still can’t solve the question: What if I have two ints, a and B, and I want to write a method to swap their values? There are many ways, but here’s a simple one:

package test; public class Test { public static void Swap(int[] a){ int c=a[0]; a[0]=a[1]; a[1]=c; } public static void main(String[] args){ int[] a=new int[2]; a[0]=10; a[1]=20; Swap(a); System.out.println(a[0]); System.out.println(a[1]); }}Copy the code

Arrays make it easy to exchange data sources of value types, but there is another way to encapsulate all variables in a class that references the type. Mysql, Netty, Spring, thread, Spring Cloud, JVM, source code, algorithm, etc., also have a detailed learning plan map, interview questions, etc., need to obtain these contents of the friend please add Q: sample: 909038429/./* Welcome to Java chat