Java Reference Mechanism--aliasing

Source: Internet
Author: User

The so-called reference transfer refers to the use of the heap memory space to multiple stack memory space

Example <1>

public class Aliasing {int temp = 30;public static void Main (string[] args) {//TODO auto-generated method stub aliasing D1 = new aliasing () ;d 1.temp = 50; System.out.println (d1.temp); fun (D1); System.out.println (d1.temp);} public static void Fun (aliasing D2) {d2.temp = 1000;}}

Example <2> it is passed a string object, because the contents of string is not modifiable, so the value of str1 or Hello, if passed is the object's string property, it can be modified

public class Aliasing {int temp = 30;public static void Main (string[] args) {//TODO auto-generated method stub String str1 = "Hello"; System.out.println (STR1); fun (STR1); System.out.println (STR1);} public static void Fun (String str2) {str2 = "Hello2";}}

Example <3> passing is the string property of the object

public class Aliasing {String temp = "Hello";p ublic static void Main (string[] args) {//TODO auto-generated method stub aliasing D1 = new Al Iasing ();d 1.temp = "World"; System.out.println (d1.temp); fun (D1); System.out.println (d1.temp);} public static void Fun (aliasing D2) {d2.temp= "HELLO";}}

Java Reference Mechanism--aliasing

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.