Verify the Java underlying data type parameter delivery method

Source: Internet
Author: User


The test proves that the Java underlying data type parameter pass value is a reference pass but the value does not change. The object is a reference pass, and the value changes. Why? Find a passage to explain the problem. "For a string object, although a reference is passed when the argument is passed, the Java Virtual Machine makes a special processing of the string object inside the function-depending on the string object as constant (final), so the string object referenced by the incoming reference address can be modified directly, Instead, a copy is created to manipulate it, and the operation on it does not affect the original value. "Refer to Sun's official certification questions to string,long,double as an example. Test whether the basic data type parameter pass value is a reference pass or a value pass:        Public Static voidA1 () {String s = NewString ("Hello"); modify1(s);System. out. println (s);      }             Public Static voidmodify1 (String s) {S + ="world!";      }             Public Static voidA2 () {Double d = NewDouble ("1.0"); modify2(d);System. out. println (d);      }             Public Static voidmodify2 (Double d) {d = 2.0;      }             Public Static void A3 () {Long d = NewLong ("1"); modify3(d);System. out. println (d);      }        Public Static voidModify3 (Long d) {d = 2L;      }             Public Static voidMain (string[] args) { A1(); A2(); A3();      }

Verify the Java underlying data type parameter delivery method

Related Article

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.