


Analysis of the difference between passing by value and passing by reference in Java (with code)
The content of this article is about the analysis of the difference between value passing and reference passing (address passing) in Java (with code). It has certain reference value. Friends in need can refer to it. I hope It will help you.
Value passing (pass by value) refers to passing the actual parameters # when calling a function ##Copy Pass a copy to the function, so that if the parameters are modified in the function, the actual parameters will not be affected. The transferred objects are often basic data structures such as integer floating point and character types.
public class PassByValueReference { //值传递 public static void main(String[] args) { int x = 9; pass(x); System.out.println(x); } private static void pass(int y) { System.out.println(y); y=0; } }The following is the running result: (The change in the value of integer y does not affect the value of integer x)
Pass by reference (pass by reference) refers to passing the address of the actual parameters directly to the function when calling the function , then the modifications made to the parameters in the function will affect the actual parameters. (Similar to a community) The transferred object is often an address data structure such as an array.
public class PassByValueReference { //引用传递 public static void main(String[] args) { int [] x = {9}; System.out.println(x[0]); pass(x); System.out.println(x[0]); } public static void pass(int [] y) { y[0] = 0; } }The following is the running result: (The value change of array y also affects the value of array x)
public class PassByValueReference { //值传递(赋值非函方式) public static void main(String[] args) { int x = 9; System.out.println(x); y = x; y = 10; System.out.println(x); } }Here x and y are basic data types. After assignment, the value of x is passed to y, which is equivalent to making a copy, instead of giving the entire x (i.e., x address) to y.
public class PassByValueReference { //引用传递(赋值非函方式) public static void main(String[] args) { int [] x = {1}; System.out.println(x[0]); int [] y = x; y[0] = 0; System.out.println(x[0]); } }Here x and y are arrays. This time the code does not simply set x[0]=y[0], but directly sets x=y. This assignment statement changes the array address of x. Given y, it is equivalent to being in the "same boat" and changing together. When y[0] changes, x[0] also changes. Related recommendations:
Detailed explanation of Java value passing and reference passing
java software engineer learns php -13. Value passing Passing by reference
The above is the detailed content of Analysis of the difference between passing by value and passing by reference in Java (with code). For more information, please follow other related articles on the PHP Chinese website!

How to solve the problem that Flink cannot find Python task script when submitting PyFlink job to YarnApplication? When you are submitting PyFlink jobs to Yarn using Flink...

Java array expansion and strange output results This article will analyze a piece of Java code, which aims to achieve dynamic expansion of arrays, but during operation...

Blank pages and proxy exceptions encountered when deploying front-end projects with Docker Nginx. When using Docker and Nginx to deploy front-end and back-end projects, you often encounter some...

Deployment method of external configuration files of SpringBoot3 project In SpringBoot3 project development, we often need to configure the configuration file application.properties...

Configuration method for converting Apache's .htaccess configuration to Nginx In project development, you often encounter situations where you need to migrate your server from Apache to Nginx. Ap...

JavaWeb application performance optimization: An exploration of the feasibility of Dao-level entity-class caching In JavaWeb application development, performance optimization has always been the focus of developers. Either...

Solving double integrals under polar coordinate system This article will answer a question about double integrals under polar coordinates in detail. The question gives a point area and is incorporated...

How to ensure the uniqueness of script tasks and monitor their operating status in a high concurrency environment? This article will explore how to ensure an outbound foot in a cluster environment...


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

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver CS6
Visual web development tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Chinese version
Chinese version, very easy to use