In-depth analysis of Java pass parameters

Source: Internet
Author: User

Overview

The problem of parameter passing in Java can be broadly divided into three types according to the type of the parameter: passing the basic type, passing the string type, passing the reference type, as to whether the final can be summed up as value passing and reference passing, according to each person's understanding different, the answer is different, here does not emphasize.

Passing Basic types
Publicclass test1 {public static void  Main (string[] args) {int n = 3; System. out.println ( "before change, n =" + N); Changedata (n); System. out.println ( "after Changedata (n), n =" + N);} public static void changedata (int N) {n = 10;}            

result : Before change, n = 3
After Changedata (n), n = 3

parsing (comparison of simple non-associative bytecode analysis):
1. Thread call Main method, create stack frame A, local variable table has n=3
The 2.main method calls the ChangeDate method, passes in the parameter n=3, the thread creates the stack frame B, assigns 10 to N, and the local variable table has n=10
The 3.changeDate method executes, stack frame B pops up, and the value of n in the output stack frame A is 3

Passing String type
Publicclass test2 {public static void  Main (string[] args) {string str = new String (" string "); System. out.println ( "before change, str =" + str); Changedata (str); System. out.println ( "after Changedata (n), str =" + str);} public static void changedata (string str) {str = " newstring "; }} 

result : before change, str = String
After Changedata (n), str = String

The instruction code is (after the above code two output statements are deleted, compiled, disassembled, in order to highlight the main process):

  PublicStaticvoidMain(java.lang.string[]);0:New#2Class Java/lang/string3:dup4:ldc#3Returns a reference to a string in a constant pool, and the stack6:invokespecial #4 //Method java/lang/string. " <init> ":(ljava/lang/string;) V 9:astore_1 10:aload_1 Span class= "Hljs-number" >11:invokestatic  #5 //Method changeData :(ljava/lang/string;) V 14: return public static void changedata (java.lang.String); 0:LDC  #6 //returns a reference to the string in the constant pool and into the stack 2:astore_0  3: return}   

parsing : 1.new,dup,idc,invokespecial,astore_1: A String object is instantiated in stack frame A, and a reference to the object is stored in the local variable table operation
2.aload_1,invokestatic: Call the ChangeDate method, pass in the reference, create the stack frame B
3.idc,astore_0,return: In stack frame B, a reference to the "newstring" string in the constant pool is pressed into the operand stack and the reference is saved to the local variable table, and then the stack frame B pops up
4. Stack frame a local variable table that reference still points to a string object whose value is still string

Passing reference types
PublicClasstest3 {public static void main ( String[] args) {stringbuffer sb = new stringbuffer ( "Hello"); System. out.println ( "before change, SB =" + SB); Changedata (SB); System. out.println ( "after Changedata (n), SB =" + SB);} public static void changedata (stringbuffer StrBuf) { Strbuf.append ( "world!");}          

result : Before change, SB = Hello
After Changedata (n), sb = Hello world!

The instruction code is (after the above code two output statements are deleted, compiled, disassembled, in order to highlight the main process):

 PublicStaticvoidMain(java.lang.string[]);0:New#2Class Java/lang/stringbuffer3:dup4:ldc#3String Hello6:invokespecial#4Method Java/lang/stringbuffer. " <init> ":(ljava/lang/string;) V9:astore_110:aload_111:invokestatic#5Method changedata: (ljava/lang/stringbuffer;) V14: return  public static void changedata (Java.lang.StringBuffer); stack=2, Locals=1, args_size=< Span class= "Hljs-number" >1 0:aload_0 1:LDC  #6 //String world! 3:invokevirtual  #7 //Method java/ Lang/stringbuffer.append: (ljava/lang/string;) Ljava/lang/stringbuffer; 6:pop 7: return}     

Parse (illustrates the difference between the next and string arguments):
In the ChangeDate method, there is a aload operation, that is, the passed reference is pressed into the operand stack, and the subsequent idc,invokevirtual operation instructions related to the object to which the reference is directed, obviously when the stack frame B pops up, A reference to the object in the local variable table of the stack frame a has changed.

Summarize

Look back: In a comprehensive view of the basic variables and string variables, when the parameters are changed, there is no use of the passed parameter value (that is, no aload operation), the basic type value or the literal reference to the constant pool directly assigned to the variable. It's kind of awkward, because string is essentially a different class and base type, and I understand that since the string class is designed as a final class, implies that the positive effect of a string variable is greater than the negative effect of having to deposit a new string string because the string variable cannot be changed, and for reuse, the assignment statement for a string variable is specially processed at compile time. Find in the constant pool whether the string already exists, if any, return a reference, to the purpose of reuse, if not, put the string in a constant pool to return the reference in order to reuse it next. For other reference variables, when the stack frame B to change the incoming parameters, the aload operation, because the JVM is stack-based bytecode execution, the Aload parameter can only be referenced in the stack frame a copy, this differs from C, because C is a register-based operation, its pointer passing, The operation is the pointer variable itself, you can use a classic reference Exchange instance to differentiate, there is an example on the net, not the statement, above.

In-depth analysis of Java pass parameters

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.