Whether the Java pass parameter is a value or a reference

Source: Internet
Author: User

This question, online a look more confused, tangled up first summed up under

First look at a chestnut:

1 public class Example {
2
3 public void Change (String str1, StringBuffer sbr1, char[] ch1, int a) {
4 str1 = "World";
5 Sbr1.append ("World");
6 ch1[0] = ' g ';
7}
8
9 public static void Main (String args[]) {
Ten String str = "Hello";
One StringBuffer sbr = new StringBuffer ("Hello");
char[] ch = {' A ', ' B '};
New Example (). Change (str, SBR, CH);
System.out.println ("str=" + str + "\ n sbr=" + sbr + "\ n ch[0]=" + ch[0]);
15}

16}

Output result: View Code

Basic data type parameters are passed by value this is not a problem, mainly for the reference data type transfer of tangled

The Java parameter pass does not exist for reference passing

Reference Horstmann in the Java Core Technology Volume One p123 chestnut

public static void Swap (Employee x,employee y) {

Employee temp=x;
X=y;
Y=temp;
}
Main ... {
Employee A=new Employee ("Jack");
Employee B=new employee ("Rose");
Swap (A, b);
}

In this case, Java is definitely not a reference.

Take a look at the concept:

Pass by value: The method receives the original worthy copy

Pass by reference: The method receives the memory address of the original value

Java references: In Java for reference data types, the stack will assign a memory address that can point to the target object in the heap to a variable, which is also called a reference

The basic data type is pass-through, which is for sure, so this is the case when the problem is considered, for reference data types

copy a thing that's just called "references," does it tell him to pass the quote or pass the value? (The way of passing, the things transmitted)

Don't get tangled up, you can understand it. "Of course, the form or the value of the transfer of a copy of the reference"

It can be thought that Java parameter passing is equivalent to a "=" procedure, it has 3 characteristics

1. "=" is a direct copy of a value for a base data type, a reference to a copy of a reference data type

2. "=" Discard the original value, reference, regain the left value, reference

3. Copy complete the left-hand side is okay.

Summary: Java parameter passing is equivalent to "=" inside Java

Whether the Java pass parameter is a value or a reference

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.