Value passing and reference passing in Java

Source: Internet
Author: User

Recently to interview, written examination of this is quite a lot, but also the basis for a summary.

1. Value passing

When a method is called, the passed parameter is passed as a copy of the value. Important feature: The transfer is a copy of the value, that is, after the transmission is unrelated.

Class Test {public static class Temptest {private void test1 (int a) {a = 5;  System.out.println ("a= in the Test1 method" + a);  }} public static void Main (string[] args) {temptest t = new temptest ();  int a = 3; T.test1 (a);//After passing, the Test1 method changes the value of the variable without affecting the A System.out.println here ("a= in the Main method" +a); }}

Results:

A=5 in the Test1 method
A=3 in the Main method

2. Delivery of references

When a method is called, the passed parameter is passed by reference, in fact the address of the reference, that is, the address of the memory space corresponding to the variable, so that when we change the value of the address point, the original point to the memory address of the value of the natural change. A reference to a value is passed, meaning that both before and after delivery point to the same reference (that is, the same memory space)

Class ref2{String temp = "Hello";}  public class RefDemo03 {public static void main (String args[]) {Ref2 r1 = new Ref2 ();  r1.temp= "Main:nihao";  System.out.println (r1.temp);  Tell (R1); System.out.println (r1.temp); } public static void-Tell (Ref2 R2) {r2.temp= "Tell:hi";}}

Results:

Main:nihao
Tell:hi

3. In particular, in Java, the string class is a constant, immutable constant, and StringBuffer is a string variable whose object can be expanded and modified. So there's the following scenario

public class RefDemo02 {public static void main (string args[]) {string str1 = "Hello";//String str1 = new String ("Hel  Lo ");  System.out.println (STR1);  Tell (STR1); System.out.println (STR1); } public static void-Tell (String str2) {str2+= ' Tell ';}}

Results:

Hello
Hello

There are two string objects created here, so the original reference did not change, or hello. But it's not the same for StringBuffer.

public class RefDemo02 {public static void main (String args[]) {stringbuffer sb = new StringBuffer ("Hello");  System.out.println (SB);  Tell (SB); System.out.println (SB); public static void-Tell (StringBuffer str2) {str2.append (' Tell ');}}

Results:

Hello
Hellotell

This creates a StringBuffer variable, two points to the same one, and changes the nature to take effect.

String why this happens, my guess is because the string is implemented by char[], similar to the integer is an int wrapper class, String is the equivalent of char[] wrapper class, the wrapper class in its value operation will appear to its corresponding basic type of nature, This is how the parameters are passed. Same integer,float and so on these wrapper class and string performance is the same, see Code

public class Test2 {public static void main (string[] arg) {//Integer i = 9;  Integer i = new Integer (9);  System.out.println (i);  Test (i); System.out.println (i); public static void Test (Integer i) {i = 11;}}

Results:

9

9

I am still a novice, all the code is actually run after, the result is no problem. But I do not know the following speculation is right, this aspect has the guidance of understanding, the blog has any error please point out, thank you!

Value passing and reference passing in Java

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.