Java pass by value and pass by reference

Basic type

The values given to the arguments of Java methods are basically passed by value. Passing by value means that the value given to the argument is copied. So if a change is made to an argument in a method, it is making a change to a copy of the argument.

Therefore, you can understand that the movement is as follows.

The easiest program to understand passing by value


int count = 8;
plus(count);
Log.d(count);

void plus(int value) {
    int result = value * 2;
    Log.d(result);
}

result


after applying plus 16
outside of method plus 8

Reference type

If you pass a reference type as an argument, the reference value will be copied.

In the code below, the left side is the reference value and the right side is the actual value. The reference value on the left side is, for example, the address of the memory where the actual value on the right side is stored.

List<String> list = new ArrayList<>();

The Imperial Palace is located in 1-1 Chiyoda, Chiyoda-ku, Tokyo. 1-1, Chiyoda, Chiyoda-ku, Tokyo is the reference value, and the Imperial Palace is like the actual value.

Based on the above, I would like to consider the following program.

A program in which changes in a method are reflected even after the method is applied


List<String> japanese = new ArrayList<>();
japanese.add("Too much");
plusElementJ(japanese);
Log.d(japanese.toString());

void plusElementJ(List<String> list) {
    list.add("Good morning");
    Log.d(list.toString());
}

result


[Too much,Good morning]
[Too much,Good morning]

In the above program, the 12th line declares that there is an Imperial Palace in 1-1 Chiyoda, Chiyoda-ku, Tokyo. Pass the address of 1-1 Chiyoda, Chiyoda-ku, Tokyo to the method. Within the method, we made an extension to the address we received. Then, even if I went out of the method, I was convinced that there was a result of expanding the Imperial Palace and it in 1-1 Chiyoda, Chiyoda-ku, Tokyo.

String is also a reference type. .. ..

Changes in the method are not reflected after applying the method


String name = "Too much";
concatenate(name);
Log.d(name);

void concatenate(String name) {
    name = "Good morning," + name;
    Log.d(name);
}

result


after applying concatenate Good morning, too much
outside of method concatenate too

String is a reference type, but immutable. That is, it cannot be changed. Therefore, isn't this process internally creating a new String object as shown below?

name = new String("Good morning,"  + name)

Therefore, I think the following things are happening.

  1. The copied reference value name is assigned a new reference value
  2. A new object is created with the reference value that name newly references.

reference

I can't say it's passed by reference anymore

Recommended Posts

Java pass by value and pass by reference
Java "pass by reference" problem summary
Whether Java arguments are passed by value or by reference
Basic data types and reference types (Java)
Java reference mechanism (stack and heap)
About Java primitive types and reference types
Java basic data types and reference types
[Swift] Implement screen transition and pass by value with Segue and present
Page number logic and reference code (java)
Java8 method reference
Java and JavaScript
XXE and Java
java8 method reference
JAVA reference materials
My Java reference
Method to describe by dividing into multiple methods How to pass arguments How to use return value Method overload Pass by value and pass by reference
[Java] The word passing by reference is bad!
Write ABNF in Java and pass the email address
Getters and setters (Java)
[Java] Thread and Runnable
Java true and false
[Java] String comparison and && and ||
[Java] How to get the key and value stored in Map by iterative processing
Understand bugs by implementing and analyzing them (1) Deadlock (Java)
Java --Serialization and Deserialization
[Java] Arguments and parameters
About Java basic data types and reference type memory
timedatectl and Java TimeZone
[Java] Branch and repeat
[Java] Variables and types
java (classes and instances)
Try Java return value
[Java] Overload and override
Introduction to Effective java by practicing and learning (Builder pattern)
Solving in Ruby, Perl and Java AtCoder ABC 113 C Reference
Understand the Singleton pattern by comparing Java and JavaScript code
Recommendation of set operation by Java (and understanding of equals and hashCode)
[Java] Difference between assignment of basic type variable and assignment of reference type variable
Same judgment / equal value judgment / comparison / order in Swift and Java
Understand the Iterator pattern by comparing Java and JavaScript code
Study Java # 2 (\ mark and operator)
[Java] Integer wrapper class reference
Basic data type and reference type
Java VB.net service reference halfway
[Java] Difference between == and equals
Pony and reference capability closure
[Java] Stack area and static area
[Java] Generics classes and generics methods
Java encryption and decryption PDF
Java and Iterator Part 1 External Iterator
[Java ~ Boolean value ~] Study memo (2)
Java if and switch statements
Object-oriented summary by beginners (Java)
Apache Hadoop and Java 9 (Part 1)
[Java] About String and StringBuilder
How to change arguments in [Java] method (for those who are confused by passing by value, passing by reference, passing by reference)
[Java] HashCode and equals overrides
☾ Java / Iterative statement and iterative control statement
Java SE8 Silver Pass Experience
Java methods and method overloads
java Generics T and? Difference