Basic data type and reference data type parameter passing diagram __java

Source: Internet
Author: User

Basic data type parameter passing

Class Demo
{public
     static void Main (String [] args)
         {
             int x = 3;
             Show (x);
             System.out.println ("x=" +x);
         }
     public static void Show (int x)
         {
             x=4;
         }
}

Analysis: Loads the Main method into the stack and assigns an int type of variable x, assign a value of 3, then call the Show method into the stack, assign the value of x 3 to the parameter x, and then assign the 4 to X, and then execute return (we've omitted to write, actually exist), So the show method is on the stack, which means there is no show () method in memory, so the value of the output X is x=3
So the change of the value of X is the x=3->x=4 (before the stack)->x=3 (the value of the output after the stack)
As shown in the figure:

Reference data type parameter passing

Class Demo
{
    int x = 3;
    public static void Main (String [] args)
    {
        Demo d = new demo ();
        d.x = 9;
        Show (d);
        System.out.println (d.x);
    }
    public static void Show (Demo d)
    {
        d.x = 4;
    }
}

parsing: int x = 3 is a member variable, so there is an initialization value of 0, the 3 assignment was then given to X, the X=3;main method loaded into the stack, the new object D in the heap memory, and the address value to D (for example, 0x0078);d. x = 9, which means that 9 is assigned a value of 3; Then execute the Show method, load into the stack, 4 assigned to X, then execute the return stack, the final printout x=4; The value of the
x changes: x=0 (initialized)->x=3 (first assignment)->x=9
->x=4;
as shown in the picture:

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.