Parameter passing in Java-value passing, reference passing

Source: Internet
Author: User
Tags stringbuffer

Arguments are passed by value instead of by reference. The Java application has only one parameter passing mechanism that is passed by value.

Objects are never passed in a Java application, and only object references are passed. Therefore, the object is passed by reference. The fact that Java applications pass objects by reference does not imply that Java applications pass parameters by reference. Parameters can be object references, while Java applications pass object references by value.

A variable in a Java application can be one of the following two types: a reference type or a base type. When passed as a parameter to a method, the two types are handled the same way. Both types are passed by value, and no one is passed by reference.

Passed by value and by reference. Passing by value means that when a parameter is passed to a function, the function receives a copy of the original value. Therefore, if the function modifies the parameter, only the copy is changed, and the original value remains unchanged. Passing by reference means that when a parameter is passed to a function, the function receives the memory address of the original value instead of a copy of the value. Therefore, if the function modifies the parameter, the original value in the calling code changes accordingly.

1. Objects are passed by reference
2. Java applications have and only one parameter passing mechanism, that is, passing by value
3, passing by value means that when a parameter is passed to a function, the function receives a copy of the original value
4. Passing by reference means that when a parameter is passed to a function, the function receives the memory address of the original value, not the copy of the value

First, take a look at the 1th: objects are passed by reference
Indeed, I do not think we have any questions, such as:
Class Test01
{
public static void Main (string[] args)
{
StringBuffer s= New StringBuffer ("good");
StringBuffer s2=s;
S2.append ("afternoon.");
System.out.println (s);
}
}
Objects s and S2 point to the same address in memory and therefore point to the same object.
How do I explain that "objects are passed by reference"?
This means that an object assignment is a reference to an object, so the object is passed by reference, is there a problem?
The output of the program run is:
Good afternoon.
This shows that S2 and s are the same object.
One thing to clarify here is that the object is actually a value, because the object is a pointer, and this assignment is the assignment between pointers, so in Java it is said to be a reference. (What is a reference?) No, it's an address. What is the address, but is an integer value)
Take a look at the following example:
Class Test02
{
public static void Main (string[] args)
{
int i=5;
int i2=i;
i2=6;
System.out.println (i);
}
}
What is the result of the program? 5!!!
What this means is that the original data type is passed by value, and this by-pass also refers to the next problem when assigning a value: Java applications have only one parameter passing mechanism, that is, passing by value
Class Test03
{
public static void Main (string[] args)
{
StringBuffer s= New StringBuffer ("good");
StringBuffer s2=new StringBuffer ("bad");
Test (S,S2);
System.out.println (s);//9
SYSTEM.OUT.PRINTLN (S2);//10
}
static void Test (StringBuffer s,stringbuffer S2) {
System.out.println (s);//1
SYSTEM.OUT.PRINTLN (S2);//2
S2=s;//3
S=new StringBuffer ("new");//4
System.out.println (s);//5
SYSTEM.OUT.PRINTLN (S2);//6
S.append ("hah");//7
S2.append ("hah");//8
}
}
The output of the program is:
Good
Bad
New
Good
Goodhah
Bad
Exam Big tip: Why is the output like this?
The emphasis here is on the "parameter passing mechanism", which is different from the delivery mechanism when assigning statements.
We see that the output of 1, 2 is exactly the same as our forecast
3 point S2 to s,4 and point S to a new object
So 5 of the output prints the contents of the newly created object, while 6 prints the contents of the original S
7 and 82 places to modify object content, but why is the output of 9 and 10 so?
Java applications have only one parameter passing mechanism, which is passed by value.
At this point, I would like to summarize my final views on this issue and a method that I think can help you understand:
We can understand the objects in Java as pointers in C + +
For example, in C + +:
int *p;
Print (p);//1
*p=5;
Print (*p);//2
1 What is the result of printing, a 16-binary address, and 2 what is the result of printing? 5, which is what the pointer is pointing to.
Even though the pointer is actually a 32-bit integer in C + +, we can understand the value of a long type.
And in Java, what an object s is, is also a pointer, is an int (for the JVM), we are directly using (that is, the case of S2=s, but for System.out.print (s), the case of the exception, Because it is actually dangling Huangliang ystem.out.print (s.tostring ())) object when it is an int integer, this can simultaneously explain the assignment of the reference and the arguments when the pass value (in both cases is directly used), and we are in S. XXX in such cases when s is in fact the C + + *s in this use. This kind of different results in different usage situations is a simplification that Java has made for us, but it can be misleading for C + + programmers. There are many cases in Java that are automatically identified and handled according to the context, and here is a bit of an extreme scenario:
Class T
{
public static String t= "T";
public static void Main (string[] args)
{
T T =new t ();
T.T ();
}
static void T () {
System.out.println (t);
}
}

1. The object is to pass the reference
2. The original type is the pass value
3.String type because there is no function to provide its own modification, each operation is reborn into a string object, so special treatment. Can be considered as a pass value.

Parameter Passing in Java-value passing, reference passing

Related Article

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.