Java "pass by reference" problem summary

Overview

Java's evaluation strategy is by value. Not passed by reference. Some people misuse it as "pass by reference" to refer to passing a reference value. There are various people who point out that it is misused, people who understand it and use it, people who do not really understand it, and it seems that they are smoldering, so I summarized the information.

Evaluation strategy

Computer science has the concept of an evaluation strategy. Both pass by value (call by value or pass by value) and pass by reference (call by reference or pass by reference) are one of the evaluation strategies.

For more information:

Java evaluation strategy

Java's evaluation strategy is by value. When you pass reference values and references in Java, they are also evaluated by passing by value.

That is also stated in the Java language specification.

When the method or constructor is invoked, the values of the actual argument expressions initialize newly created parameter variables, each of the declared type, before execution of the body of the method or constructor.

From http://docs.oracle.com/javase/specs/jls/se9/html/jls-8.html#jls-8.4.1 My translation:

When a method or constructor is called, initialize the new formal argument value of each declared type with the ** value ** of the actual argument part representation before executing the body of the method or constructor.

For more information, read Java is Pass-by-Value, Dammit! written by Scott Stanchfield.

The right way of thinking about evaluation strategy

Generally, when considering an evaluation strategy, it is best to think about how the call is evaluated when a program that calls a function, method, subroutine, etc. is input to the programming language processing system. The right way of thinking about strategy.

Don't think about the behavior when you actually run a program or binary. The behavior of the program changes completely depending on the execution environment of the programming language. For example, the actual behavior of a program depends on whether the execution environment of the program is a compiler, how much optimization is done, whether it is an interpreter, or whether a human "executes" it with a blackboard and chalk. When considering a programming language evaluation strategy, don't think about what the CPU actually does, what the memory is, what the address is, and so on.

Personal opinion

Passing a reference in Java is called "passing by reference" ** should be stopped immediately **. As you can see by searching by Java reference, there are some articles that are clearly misunderstood. There must be tens or hundreds of times that number, some who are misunderstood and not observed, and some who are dragged by misuse and hinder their understanding. If you don't stop doing this, you will lose the industry.

Recommended Posts

Java "pass by reference" problem summary
Java pass by value and pass by reference
Object-oriented summary by beginners (Java)
Java knowledge summary
Java related summary
[Java] Problem No. 2
[Java] Problem No.3
Java 8 documentation summary
[Java] Problem No.1
Java 11 document summary
java8 method reference
JAVA reference materials
My Java reference
[Java] The word passing by reference is bad!
[Java] Basic summary of Java not covered by Progate ~ Part 1 ~
Java 12 new feature summary
[Summary] Java environment preparation
Java 13 new feature summary
Summary of revisions (new era) support by Java version
Summary of knowledge required to pass Java SE8 Silver
Java static [Personal summary]
Whether Java arguments are passed by value or by reference
Thread safe summary ~ Java ~
Java Primitive Specialization Summary
Java development link summary
Personal summary about Java
Java 10 new feature summary
java regular expression summary
Java 14 new feature summary
Summary of Java support 2018
Java design pattern summary
Java reserved word summary
Java8 Stream Rough Summary
[Java] Basic summary of Java not covered by Progate ~ Part 2 ยท List ~
Summary of values returned by the Spliterator characteristics method #java
[Java] Integer wrapper class reference
What is Java Assertion? Summary.
Java VB.net service reference halfway
[Java11] Stream Summary -Advantages of Stream-
Progate Java (Beginner) Review & Summary
[Java] Summary of regular expressions
Zunda's 1-digit addition problem Java 11
Java8 stream, lambda expression summary
Java SE8 Silver Pass Experience
Summary of Java language basics
Java tips --Spring execution Summary
[Java] Summary of for statements
Summary of Java Math class
[Java11] Stream Usage Summary -Basics-
[Java] Summary of control syntax
Summary of java error processing
[Java] Summary of design patterns
[Java] Summary of mathematical operations
The story of not knowing the behavior of String by passing Java by reference