Whether the modification results can be kept outside the method by modifying the real parameter in the method body

Source: Internet
Author: User

Package in general;/*** my summary: all arguments passed to the method will create a copy in the stack space. After the method ends, the copy will be destroyed. * @ 1 because the basic types are stored in the stack, the real parameter copy in the modification method does not affect the original basic type real parameters! * @ 2 because the object variable name is stored in the stack, but the object is stored in the heap, * Even if you modify the content in the copy of the variable name, it will affect the content in the heap! * @ 3 this method changes the object variable name direction, that is, it does not point to the original heap memory. * therefore, modifying this copy will not affect the original object! ** It can be seen that it is true that no reference is passed in Java! This is caused by ignorance of stacks and stacks ~ * For example, the subsequent changes to the String are not retained. If any object is passed as a reference, it cannot be understood! * In fact, The string constant "2333333333333" is stored in the string pool during compilation, s = "2333333333333", * That is, the copy of s in the method points to the content in the string pool, it is no longer the previous heap, so it will not affect the original heap content! ** Expansion: String s = "I bought a table last year"; how many objects have been created? There are two answers! One is the object in the string pool, and the other is the object in the heap (copy the content of "I bought a table last year" in the string pool to the heap, and assign the heap memory address to s ). ** Alas, my written test is wrong ...... * @ author Wei sama **/public class whether the real parameter can be modified outside the method by modifying the real parameter in the method body can be kept outside the method. Open {public static void main (String [] args) {int I = 10; Test t = new Test (1); t. changeANum (I); System. out. println (I); System. out. println (t. getI (); t. changeTestI (t); System. out. println (t. getI (); t. changeTest (t); System. out. println (t. getI (); String s = "I bought a table last year"; t. changes (s); System. out. println (s); // still unchanged !}} Class Test {private int I; public Test (int I) {this. I = I;} public int getI () {return I;} public void setI (int I) {this. I = I;} public void changeANum (int I) {// @ 1i = 99;} public void changeTestI (Test t) {// @ 2t. I = 2; // t is not t in main! But it points to the same heap space as t in main, so I changed in that space !} Public void changeTest (Test t) {// @ 3 t = new Test (100); // This change cannot be retained !!!} Public void changes (String s) {// @ 3 s = "2333333333333 ";}}

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.