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.
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'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.
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.
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