search
HomeJavajavaTutorialHow to pass value in java?
How to pass value in java?Nov 15, 2019 pm 03:03 PM
javaPass value

We all know that there are basically two ways to pass by value: pass by value and pass by reference. So in JAVA, is it pass by value or pass by reference? The following article will introduce it to you, I hope it will be helpful to you.

How to pass value in java?

Value passing: refers to copying a copy of the actual parameters and passing them to the formal parameters when calling the function, so that the formal parameters can be modified in the function Will not affect the actual parameter value.

Pass by reference: refers to passing the address of the actual parameter directly to the formal parameter when calling the function. Then the modification of the parameter in the function will affect the actual parameter. value.

How to pass value in java?

Java's value-passing method: value-passing (all changes are only limited to the method body, outside the method body, any modification operations are no longer valid). [Recommended learning: java course]

We can use a program to verify that only value is passed in Java

/**
 * 验证java中只有值传递
 */
class User{    
    private String name;    public String getName() {        return name;
    }    public void setName(String name) {        this.name = name;
    }
}public class TestValue {    public static void change(User user2,int a2){
        System.out.println("改变之前:"+user2.getName()+",a2="+a2);
        
        user2.setName("李四"); //改变 user2 的 name 值
        a2 = 10; //改变 a2 的值
        System.out.println("改变之后:"+user2.getName()+",a2="+a2);
        
        user2 = new User(); //将 user2 重新指向一个新对象
        user2.setName("王五");
        System.out.println("重新指向一个新对象后:"+user2.getName());
    }    public static void main(String[] args){
        User user1 = new User();
        user1.setName("张三"); //初始化 user1 的 name 为张三
        int a1 = 5; //初始化 a1 的值为 5
        change(user1,a1); //调用方法验证传值方式
        System.out.println("调用方法后:"+user1.getName()+",a1="+a1);
    }
}

Run this program, the output result is:

改变之前:张三,a2=5
改变之后:李四,a2=10
重新指向一个新对象后:王五
调用方法后:李四,a1=5

Result Analysis

How to pass value in java?

## Let’s use the above picture as an aid to analyze this paragraph Program, first we define a

User class, and then instantiate a User object in the test class, named user1, and assign it a value of name = 'Zhang San'.

At this time, in the memory, as shown in

Figure 1, instantiating an object is equivalent to opening a piece of memory in the heap, and the memory address is 017 , at this time, the reference of this object is user1, and the memory address is 001. It saves the address of the object in the memory, which means it points to the object.

Next, we call the method

change() to try to change the name value of user1 to verify the value passed in java Way.

We pass

user1 as an actual parameter to the change() method, and the formal parameter user2 accepts this actual parameter, which is reflected here. The difference between the two methods of passing parameters is revealed. If passed by value, then it is as defined.

As shown in

Figure 2, user2 is a copy of user1, which means that when passing parameters, user1 (itself a reference to an object), made a copy named user2, which is also a reference to an object, and user1 and user2Points to the same object at this time.

And if it is passed by reference, it is as defined, as shown in

Figure 5, when passing parameters, user1 is directly passed to the formal parameter , just changed the name to user2, but essentially user1 and user2 are actually the same. It is a reference to an object.

Next, let’s analyze the output results. Whether it is passed by value or by reference, the output result in line 1 must be

张三, because they all point to the same object. For the output in line 2, we still can't tell which method is used, because the same object is changed, and the value will also change; the key lies in the output in line 3 and line 4.

At this time, we redirect

user2 to a new object and assign a value to this object name = '王五', if it is passed by reference , then user1 will also change the pointer to point to the new object. The output result after calling the method in the last line will be the same as the third line, 王五, but in fact the output It’s 李思. This shows that user1 and user2 are not actually the same.

The real calling process is as shown in

Figure 2~Figure 4, so that user2 points to a new object,# The object pointed to by ##user1 has not changed, it is still the original object. For basic type parameters, the value of

a1

has not changed at the end, indicating that a2 is one of a1 when the method is executed. copy. For reference type parameters, such as the

User

object, when calling the method, its reference user1 is actually used as the actual parameter, then it is passed to the form The reference will be a copy of the reference reference user2, although this is two references (such as the relationship between a1 and a2). But it points to the same object, and all operations are for the same object.

EndWe can know through the above analysis.

There is only one way of passing by value in Java

, but for reference types, the parameters passed are references to objects.

The above is the detailed content of How to pass value in java?. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
带你搞懂Java结构化数据处理开源库SPL带你搞懂Java结构化数据处理开源库SPLMay 24, 2022 pm 01:34 PM

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于结构化数据处理开源库SPL的相关问题,下面就一起来看一下java下理想的结构化数据处理类库,希望对大家有帮助。

Java集合框架之PriorityQueue优先级队列Java集合框架之PriorityQueue优先级队列Jun 09, 2022 am 11:47 AM

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于PriorityQueue优先级队列的相关知识,Java集合框架中提供了PriorityQueue和PriorityBlockingQueue两种类型的优先级队列,PriorityQueue是线程不安全的,PriorityBlockingQueue是线程安全的,下面一起来看一下,希望对大家有帮助。

完全掌握Java锁(图文解析)完全掌握Java锁(图文解析)Jun 14, 2022 am 11:47 AM

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于java锁的相关问题,包括了独占锁、悲观锁、乐观锁、共享锁等等内容,下面一起来看一下,希望对大家有帮助。

一起聊聊Java多线程之线程安全问题一起聊聊Java多线程之线程安全问题Apr 21, 2022 pm 06:17 PM

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于多线程的相关问题,包括了线程安装、线程加锁与线程不安全的原因、线程安全的标准类等等内容,希望对大家有帮助。

详细解析Java的this和super关键字详细解析Java的this和super关键字Apr 30, 2022 am 09:00 AM

本篇文章给大家带来了关于Java的相关知识,其中主要介绍了关于关键字中this和super的相关问题,以及他们的一些区别,下面一起来看一下,希望对大家有帮助。

Java基础归纳之枚举Java基础归纳之枚举May 26, 2022 am 11:50 AM

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于枚举的相关问题,包括了枚举的基本操作、集合类对枚举的支持等等内容,下面一起来看一下,希望对大家有帮助。

java中封装是什么java中封装是什么May 16, 2019 pm 06:08 PM

封装是一种信息隐藏技术,是指一种将抽象性函式接口的实现细节部分包装、隐藏起来的方法;封装可以被认为是一个保护屏障,防止指定类的代码和数据被外部类定义的代码随机访问。封装可以通过关键字private,protected和public实现。

归纳整理JAVA装饰器模式(实例详解)归纳整理JAVA装饰器模式(实例详解)May 05, 2022 pm 06:48 PM

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于设计模式的相关问题,主要将装饰器模式的相关内容,指在不改变现有对象结构的情况下,动态地给该对象增加一些职责的模式,希望对大家有帮助。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SecLists

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.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!