Parameter passing in java (only value passing, no reference passing)
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!

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

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

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Guide to Perfect Number in Java. Here we discuss the Definition, How to check Perfect number in Java?, examples with code implementation.

Guide to Random Number Generator in Java. Here we discuss Functions in Java with examples and two different Generators with ther examples.

Guide to Weka in Java. Here we discuss the Introduction, how to use weka java, the type of platform, and advantages with examples.

Guide to Smith Number in Java. Here we discuss the Definition, How to check smith number in Java? example with code implementation.

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

Guide to TimeStamp to Date in Java. Here we also discuss the introduction and how to convert timestamp to date in java along with examples.

Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods. Capsule volume formula The formula for capsule volume is as follows: Capsule volume = Cylindrical volume Volume Two hemisphere volume in, r: The radius of the hemisphere. h: The height of the cylinder (excluding the hemisphere). Example 1 enter Radius = 5 units Height = 10 units Output Volume = 1570.8 cubic units explain Calculate volume using formula: Volume = π × r2 × h (4
