"There is only value passing in the Java language, no reference passing" understanding

Source: Internet
Author: User

Create a class human

 Public classHuman {Private intAge ; Private intSex//0-Female, 1-male     Public intGetage () {returnAge ; }     Public voidSetage (intAge ) {         This. Age =Age ; }     Public intGetsex () {returnsex; }     Public voidSetsex (intsex) {         This. Sex =sex; }}

Test class

classhumantest {@TestvoidTest () {Human Human=NewHuman (); intAge=Human.getage ();        Addage (age);                System.out.println (age);        Man (human);        System.out.println (Human.getage ()); assertHuman.getsex () ==1; }    voidMan (Human Human) {human.setage (20); Human.setsex (1); }    voidAddage (intAge ) { Age+=2; }}

Output results

020

(1). Value passing: a copy of the value is passed. That means they don't close each other after they're delivered.

the difference between heap and stack:
(1). Stack: The stack holds the base data type (basic data types include: int, short, double, long, float, Boolean, char, Byte, Note no string), and a reference to the object.
(2). Heap: heap memory is used to hold objects and arrays created by new. The memory allocated in the heap is managed by the automatic garbage collector of the Java Virtual machine.

When passing a parameter as an object, a reference to the object is passed in the stack, and the object's properties exist in the heap, the property value is modified, the reference to the object is unchanged,

So the essence is still the value of transmission;

"There is only value passing in the Java language, no reference passing" understanding

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.