Basic type and reference type Basic type: The value is stored in the local variable table. No matter how modified, it will only modify the value of the current stack frame. After the method is executed, no changes will be made outside the method; at this time, the outer layer needs to be changed. Variables must return active assignments. Reference data type: The pointer is stored in the local variable table. When the method is called, the copy reference is pushed onto the stack, and the assignment only changes the reference of the copy. However, if the value of the copy reference is directly changed and the object of the reference address is modified, then of course the object referencing this address outside the method will be modified. (Two references, the same address, any modification behavior takes effect on both references at the same time)
public class Test2 { public static void setValue(String str){ str = "ss"; } public static void setValue(Man str){ str = new Man("test"); } public static class Man{ private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } public Man(String name) { this.name = name; } @Override public String toString() { return "Man{" + "name='" + name + '\'' + '}'; } } public static void main(String[] args) { String str = "s"; setValue(str); System.out.println(str); Man man = null; setValue(man); System.out.println(man); } }
As shown in the above code practice, the result output
s null
The reason is that the method has The concept of stack frame, when pushing onto the stack, is that the push method parameters are copies of the incoming parameters.
Java Advanced Features
Distinguish data types at this time: basic types and reference types
Basic type: The value is stored in the local variable table. No matter how you modify it, it will only modify the value of the current stack frame. After the method is executed, no changes will be made outside the method; at this time, you need To change the outer variable, active assignment must be returned.
Reference data type: The pointer is stored in the local variable table. When the method is called, the copy reference is pushed onto the stack, and the assignment only changes the reference of the copy. But if you directly change the value of the copy reference and modify the object of the reference address, then of course the object referencing this address outside the method will be modified. (Two references, the same address, any modification takes effect on both references at the same time)
For example,
public static void setValue(StringBuilder str){ str = new StringBuilder("sss"); } public static void setValue2(StringBuilder str){ str.append("sss"); } public static void main(String[] args) { StringBuilder str = new StringBuilder(); setValue(str); System.out.println(str.toString()); setValue2(str); System.out.println(str.toString()); }
Regarding String, it is essentially a final type char array and cannot be modified. , can only assign values. When modifying the parameters passed in to the method, it is actually a new object. You must return and re-assign the external variables to take effect on the external String reference.
You can understand by looking at any method of the String source code
/** * Returns a string resulting from replacing all occurrences of * {@code oldChar} in this string with {@code newChar}. * <p> * If the character {@code oldChar} does not occur in the * character sequence represented by this {@code String} object, * then a reference to this {@code String} object is returned. * Otherwise, a {@code String} object is returned that * represents a character sequence identical to the character sequence * represented by this {@code String} object, except that every * occurrence of {@code oldChar} is replaced by an occurrence * of {@code newChar}. * <p> * Examples: * <blockquote><pre class="brush:php;toolbar:false"> * "mesquite in your cellar".replace('e', 'o') * returns "mosquito in your collar" * "the war of baronets".replace('r', 'y') * returns "the way of bayonets" * "sparring with a purple porpoise".replace('p', 't') * returns "starring with a turtle tortoise" * "JonL".replace('q', 'x') returns "JonL" (no change) ** * @param oldChar the old character. * @param newChar the new character. * @return a string derived from this string by replacing every * occurrence of {@code oldChar} with {@code newChar}. */ public String replace(char oldChar, char newChar) { if (oldChar != newChar) { int len = value.length; int i = -1; char[] val = value; /* avoid getfield opcode */ while (++i
The reference type will cause shallow copy and deep copy phenomena.
Related articles:
How to modify variables outside the scope in JavaScript
PHP closure function Methods of passing parameters and using external variables, php variables
The above is the detailed content of Modify function incoming values in Java: basic types and reference types. For more information, please follow other related articles on the PHP Chinese website!

The article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.

The article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

The article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]

Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version
Visual web development tools

Notepad++7.3.1
Easy-to-use and free code editor