Whether Java is a value pass or a reference pass

Source: Internet
Author: User

The eight basic data types in Java are value passing (only the value is passed, the original variable has no relation, the original value does not change), but if the data is the object type then the reference is passed, as in the example below:

Package com.minimax.demo;public class test {public static void main (String[ ] args)  {        int a=10;         system.out.println ("Before change (a)". A.. " +A); Change (a); System.out.println ("After change (a)". A.. " +A);//Summary: Java in the basic parameters of the transfer is the value of the transfer, that is, the passed parameter is a copy of the original value, regardless of whether the value passed in the method is changed or not has any relationship with the original data, will not have any effect stringbuffer str= New stringbuffer (); Str.append ("Zhangsan"); System.out.println ("Before change2 (str) ..." +str); Change2 (str); System.out.println ("After change2 (str) ..." +str); Private static void change2 (STRINGBUFFER STR2)  {// todo auto-generated  method stub//This is the transformation of the one//transformation one output is  zhangsan,nihao//because at the time of the call Change2 (str2) method   passed over Str is the object of reference, This reference holds the object's address in memory//and then into the Change2 (str2) method   is to copy the address of the object stored by STR to STR2, then STR2 has the address to the//object   You can then modify the contents of the object   the address that Str saves is always unchanged//str2.append (", Nihao");//This retrofit two//Transformation method Two   is because the use of  new this keyword   created a new object in the heap memory, naturally, will be assigned a new memory address//So STR2 has the new object memory address   This str2 is a reference to the new object, you can manipulate the new object &nbsp, and the memory//address passed in the copy is not a bit  , so the contents of the original object are not modified//So the final test result is   ZHANGSANSTR2 =new stringbuffer (); Str2.append (", Nihao");} Private static void change (Int b)  {b=12;}}


This article is from "Wish You Happiness" blog, please be sure to keep this source http://zhunixingfu.blog.51cto.com/7430537/1628938

Whether Java is a value pass or a reference pass

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.