Why is it possible to change the attribute value of a reference variable? Please see the answer below.
Data types in java
Data types in Java are divided into two categories: basic types and reference types. Correspondingly, variables are also divided into two types: basic types and reference types.
The basic type variable saves the original value, that is, the value it represents is the value itself;
The value saved by the reference type variable is the reference value, and the "reference value" points to the address of the memory space , represents a reference to an object, rather than the object itself. The object itself is stored at the address represented by the reference value.
Basic types include: byte, short, int, long, char, float, double, Boolean, returnAddress.
Reference types include: classes, interface types and arrays.
There is only value transfer in java
In daily coding, you will often see the following phenomenon:
1. For basic type parameters, in the method Reassigning parameters within the body will not change the value of the original variable.
2. For reference type parameters, re-assigning a reference to the parameter in the method body will not change the reference held by the original variable.
3. The parameters are operated within the method body without changing the value of the original variable.
4. For reference type parameters, the method body operates on the properties of the object pointed to by the parameter, which will change the property value of the object pointed to by the original variable.
For example:
public class Main { private static void getMiddleOne(boolean b, Boolean boo, Boolean[] arr){ b = true; boo = new Boolean(true); arr[0] = true; } //测试 public static void main(String[] args) { boolean b = false; Boolean boo = new Boolean(false); Boolean[] arr = new Boolean[]{false}; getMiddleOne(b, boo, arr); System.out.println(b); System.out.println(boo.toString()); System.out.println(arr[0]); /** * output: * false * false * true */ } }
We can answer the above phenomenon as long as we understand the following two points:
1. Basic data types The value is the value itself, so the value of b in the example is false; because the wrapper class will automatically box and unbox, it can be processed in the same way as the basic type, so the value of boo in the example is false; the array is a reference type, so arr The value is a reference to the Boolean[].
2. In Java, there is only value transfer and no reference transfer, so the three parameters passed into the getMiddleOne method are the value copy of b, the value copy of boo, and the value copy of arr.
It will be clear from the above two points. b=true and boo = new Boolean(true) executed in the getMiddleOne method assign new values to their copies, so the value of the original variable is not changed. ; Similarly, arr[0] = true copies true to the first element of the array pointed to by the copy of arr. The value of arr and the value of the copy of arr are both references to the array, so the copy of arr points to The array and the array pointed to by arr are the same, so changing the elements of the array copied by arr will also affect the original variable arr.
Summary
In Java, only value is passed. Basic types pass a copy of the value, and reference types pass a copy of the reference.
The above is the detailed content of Parameter passing in java (only value passing, no reference passing). 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

SublimeText3 Linux new version
SublimeText3 Linux latest version

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Chinese version
Chinese version, very easy to use

Dreamweaver Mac version
Visual web development tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software