Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

An example Analysis of passing parameters between string and Array in JAVA

2025-04-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the JAVA string and array to do parameter transfer example analysis, the article is very detailed, has a certain reference value, interested friends must read it!

First of all, it is clear that only values are passed in java! Only values are passed! The theoretical basis comes from "think in java". The next step is to specify why only values are passed in java.

Because there are two data types in java: basic type and reference type, plus the special type of String, it can be explained in three aspects.

1. Basic data type

Look at the code first.

Public class Demo01 {public void change (int a) {System.out.println ("initial value of replica a" + a); a = 20; System.out.println ("new value of replica a" + a);} public static void main (String [] args) {int a = 10; Demo01 d = new Demo01 (); d.change (a) System.out.println ("value after change method execution" + a);}}

Analysis:

In java, the basic data type follows value passing, so when object d calls the change () method, it just passes a copy of the original data a to the parameters in the method. For the first time, the value of the original data an and the copy an is 10, and after the execution to the axiom 20, the value of the copy a becomes 20.

So the running result is:

Refer to the following figure for principle

two。 Reference data type

Look at the code first.

Public class Demo02 {char [] ch = {'a', 'baked,' c'}; public void change (char ch []) {System.out.println ("initial value of ch [0] in the method:" + ch [0]); ch [0] ='ch [0]; System.out.println ("New value after execution of ch [0] in the method:" + ch [0]) } public static void main (String [] args) {Demo02 d = new Demo02 (); System.out.println ("the initial value of the array in object d is" + d.ch); d.change (d.ch); System.out.println ("the final value of the array in object d is:" + d.ch);}}

Analysis:

When the reference type is passed as a parameter, it is also a value pass, and a copy of the address value is passed, but the two addresses point to the same place. When the copy address does not change the direction, the operation on the data pointed to by the copy address will affect the value of the original data. The ch [] array and the original ch [] array in the method point to the same data, so the initial phase ch [0] points to'a 'array; then a new assignment is made to the ch [0] in the copy to' g'.

So the running result is:

Refer to the following figure for principle

3. Parameter passing of string

Look at the code first.

Public class Demo03 {public void change (String str2) {System.out.println ("initial str2 value in method" + str2); System.out.println ("initial hashcode value of str2 in method" + str2.hashCode ()); str2 = "bbb"; System.out.println ("after str2 assignment in method:" + str2); System.out.println ("hashcode value after str2 assignment in method:" + str2.hashCode ()) } public static void main (String [] args) {String str1 = new String ("aaa"); System.out.println ("hashcode value of the original string str1:" + str1.hashCode ()); Demo03 d = new Demo03 (); d.change (str1); System.out.println ("value of str1 after method call" + str1);}}

Analysis:

A string is a special data type, and its underlying layer is a final-type char [] array, which cannot be changed, so when a string is passed as a parameter, it can be operated as a special array. Similarly, it references a copy of the original object to the copy. In this case, both the reference of the copy object and the reference of the original object point to the location of the original string. That is, at the beginning of initialization, str2 points to the same address as the original object str1, that is, the initial hashcode value of str2 is the same as that of the original object str1. After the str2 goes through the str2= "bbb" operation, due to the immutability of the string, the str2 will point to a new object reference, that is, the location where the str2 points to "bbb". The hashcode value of str2 changes, but the object reference of the original str1 does not change, and the "aaa" does not change, so the str1 still points to "aaa". The running results are as follows:

Let's take a look at a more specific string example:

Public class Demo04 {public static void main (String [] args) {StringBuffer s = new StringBuffer ("hello"); StringBuffer S2 = new StringBuffer ("hi"); test (s, S2); System.out.println ("value of s after method usage:" + s); System.out.println (value of S2 after method usage: "+ S2") } static void test (StringBuffer S3, StringBuffer S4) {System.out.println ("value of method initialization S3" + S3); System.out.println ("value of method initialization S4" + S4); S4 = S3; S3 = new StringBuffer ("new"); System.out.println ("value of S3 after the first step change" + S3) System.out.println ("value of S4 after first step change" + S4); s3.append ("boy"); s4.append ("gril"); System.out.println ("value of S3 after second step change" + S3); System.out.println ("value of S4 after second step change" + S4);}}

This time, let's look at the results:

Then analyze:

Before the method is executed, there is no doubt that the strings S1 and S2 point to "hello" and "hi" respectively.

(1) then go inside the method. The parameters S3 and S4 in the method are initialized the same as the above example, and they point to the same location as s1s2, or s1s2 copies the object reference to s3s4, and the values of s3s4 are "hello" and "hi".

(2) then execute s4=s3, which refers the object of S3 to S4, where S4 is "hello"; s3=new StringBuffer ("new"); note that this operation is equivalent to giving S3 a new object reference, and S3 points to a position where the string is "new", so at this time S3 = "new", S4 = "hello".

(3) then s3.append ("boy"); s4.append ("gril"); the append method in StringBuffer should note that its operation does not point to a new object reference for s3s4, but operates on the original basis, so after the operation, S3 = "newboy", S4 = "hellogril"

(4) when the method is called, go back to the impact of s3s4 on s1s2 in this process.

-A. First, S3 just points to "hello" like S1, and then creates a new object reference "new" for S3. There is no relationship between S3 and S1. After S3 append (boy), S3 = "newboy".

-B. Both S4 and S2 point to "hi" at first, and then S3 gives its initial value (that is, a copy of S1) to S4, and S4 points to "hello" (which makes S4 have a relationship with S1). S4 performs an append (grill) operation, because it and S1 point to the same location, so the object they both point to will change. S4rooms1 = "hellogrill".

-C. Then it becomes clear that the object "hi" pointed to by S2 has not changed, and the "hello" pointed to by S1 becomes "hellogril" under the append ("grill") operation.

4. Summary

When the basic data type is used as the formal parameter of the method, the modification of the parameter in the method body does not affect the value of the actual parameter.

When using the reference data type as the formal parameter of the method, if you modify the data content pointed to by the formal parameter in the method body, it will affect the value of the argument variable, because the shape parameter variable and the argument variable share the same heap area.

When using the reference data type as the formal parameter of the method, if the direction of the formal parameter variable is changed in the method body, it will not affect the value of the parameter variable, so the formal parameter variable and the argument variable point to different heap areas respectively; the last example is the most vivid explanation.

Regarding the string as a parameter, it also depends on whether its parameter variable direction has changed, because the bottom layer of String is final type char []. When you are in String s = "aaa" or String s = new String ("aaa"), a new object reference will be created for s. But when the append () method is called, it will not point to the new object, but will change on the original object, and the object reference shared with it will also change.

Finally, it is repeated that there is no reference passing in java, only value passing, and the reference type is a special value pass (a copy of its address is given to the parameter, but it is different from the basic data type, if the object that the address points to changes, because of sharing reasons, the original object will also change).

The above is all the contents of the article "sample Analysis of string and Array Parameter passing in JAVA". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report