Java Essay II (deep analysis of Java simple types, string and object value passing and reference passing)

Source: Internet
Author: User

About the value of Java transfer and reference to pass, read a lot of information and blog, feel most talk is very messy, are they understand after the appearance of not speaking, finally is a more understanding of these concepts, the following to do a summary.


1. Parameter passing of simple type

the parameters of the Java method are simple types , it is passed by value (pass by value). Here's a classic swap function:

methods that cannot exchange values :

Package Testtransferpack;public class Testtransfer {public static void main (string[] args) {//TODO auto-generated method Stubint a = 1;int B = 5;swap (A, b); System.out.println (A + "  + b);} public static void swap (int a,int b) {int temp=0;temp = b;b=a;a=temp;}}
Result:1  5
when passing a variable of a simple type in the form of a parameter, the value of the parameter is actually passed into the method function, then how to change its value in the method function, the result is to change only the copy value, not the source value.

Methods for exchanging values:

(1) array

Package Testtransferpack;public class TestTransfer1 {public static void main (string[] args) {//TODO auto-generated method Stubint a = 1;int B = 5;int[] test = new Int[2];test[0]=a;test[1]=b;swap (test,0, 1); a = Test[0];b = Test[1]; System.out.println (A + "  + b);} public static void Swap (int[] data,int index1,int index2) {   int tmp = DATA[INDEX1];   DATA[INDEX1] = Data[index2];   DATA[INDEX2] = tmp;}}
Result:5 1

This method of using arrays can change the values of two simple types.

C + + has pointers, the simple type is passed in the same way as in 1, is the value of the pass, but you can pass a pointer to exchange the value of two simple types, but there is no pointer in Java, so the value of a simple type of exchange to special processing.

2. Parameter passing of ordinary objects
Ordinary objects are generally passed by reference, let's say, that is, you say by address, address delivery is the original object address, so that the changes will change the original value, the subsequent will be a detailed analysis of the way to explain.

Package Testtransferpack;public class TestTransfer2 {public static void main (string[] args) {//TODO auto-generated method Stubint a = 1;int B = 5; Test TT1 = new Test (a); Test TT2 = new Test (b); Swap1 (TT1,TT2); System.out.println (tt1.a+ "  " + tt2.a);} public static void Swap1 (Test index1,test index2) {int tmp = INDEX1.A;INDEX1.A = INDEX2.A;INDEX2.A = tmp;} public static class Test{int a=0;public Test (int b) {a = B;}}}

Result:5  1

As you can see, the data of two objects in a function can be modified.

However, this does not change the value of the object:

Package Testtransferpack;public class TestTransfer3 {public static void main (string[] args) {//TODO auto-generated method Stubint a = 1;int B = 5; Test TT1 = new Test (a); Test TT2 = new Test (b); Swap1 (TT1,TT2); System.out.println (tt1.a+ "  " + tt2.a);}  public static void Swap1 (Test index1,test index2) {/*int tmp = index1.a;index1.a = index2.a;index2.a = tmp;*/test tmp = new Test (); tmp = INDEX1;INDEX1 = INDEX2;INDEX2 = tmp;} public static class Test{int a=0;public Test (int b) {a = B;} Public Test () {}}}
Result:1 5
The reason, we can go to the C + + parameters passed, C + + If the parameter is passed a pointer, then the pointer to the object content can be changed, but the pointer itself is a copy, and therefore cannot change the value of the pointer, the parameter of the object in Java is passed an address, This address points to something that can be changed, like the example in TestTransfer2 above, but this address is passed by value, and changing the address is completely useless, just like in TestTransfer3.
we usually give a string as a special object to show that the object is passed by value or by reference, but often it's messy, usually after you understand it, and the following section discusses string issues. I am not very proficient, if there is anything wrong in writing, please correct me.

3. Parameter passing of string and related object

In layman's terms, a string is passed by value.

Package Testtransferpack;public class TestTransfer4 {public static void main (string[] args) {//TODO auto-generated method Stubstring tt1 = new String ("Hello"); String tt2 = new String ("World"); Swap1 (TT1,TT2); System.out.println (tt1 + tt2);} public static void Swap1 (String index1,string index2) {string tmp = INDEX1;INDEX1 = INDEX2;INDEX2 = tmp;}} Result:hello World

This is the same as TestTransfer3, of course, can not change the value of string, some people say with StringBuffer, of course, not.

Package Testtransferpack;public class TestTransfer5 {public static void main (string[] args) {//TODO auto-generated method Stubstringbuffer  tt1 = new StringBuffer ("Hello"); StringBuffer  tt2 = new StringBuffer ("World"); Swap1 (TT1,TT2); System.out.println (tt1.tostring () + tt2.tostring ());} public static void Swap1 (StringBuffer  index1,stringbuffer  index2) {StringBuffer tmp = INDEX1;INDEX1 = Index2; INDEX2 = tmp;}}
<pre name= "code" class= "Java" >result:hello World

You might think of changing the value of index1 in a function, such as INDEX1=INDEX2, but neither string nor StringBuffer can, and the changed value does not take out the function.

But this can:

Package Testtransferpack;public class TestTransfer6 {public static void main (string[] args) {//TODO auto-generated method Stubstringbuffer  tt1 = new StringBuffer ("Hello"); StringBuffer  tt2 = new StringBuffer ("World"); append (TT1,TT2); System.out.println (tt1.tostring () + tt2.tostring ());} public static void Append (StringBuffer  index1,stringbuffer  index2) {index1.append (INDEX2);}}
Result:hello World
Here's a look at the implementation of append, all the way down, we'll find this sentence:

System.arraycopy (value, Srcbegin, DST, Dstbegin, Srcend-srcbegin);
Use this method to extend the StringBuffer array size before you copy the data. This will change the content of the space index1 points to.

It is said that string creates new objects in the process of assignment, so that the problems in TestTransfer4 and TestTransfer5 are solved, because they are not an object and how this value can be changed.

Here's a look.

we know that Hashcode is the unique identifier for object. We can use hashcode to determine the object variation of string in an assignment or other operation.

Package Testtransferpack;public class TestTransfer7 {public static void main (string[] args) {//TODO auto-generated method Stubstringbuffer  tt1 = new StringBuffer ("Hello"); StringBuffer  tt2 = new StringBuffer ("World"); append (TT1,TT2); System.out.println (tt1.tostring () + tt2.tostring ()); System.out.println (Tt1.hashcode ());} public static void Append (StringBuffer  index1,stringbuffer  index2) {index1.append (INDEX2); System.out.println (Index1.hashcode ()); index1 = Index2; System.out.println (Index1.hashcode ());}}
Result:11671659211442002549hello World 1167165921
As you can see, index1 is no longer the original object after the second assignment. string is the same.








Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Java Essay II (deep analysis of Java simple types, string and object value passing and reference passing)

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.