Passing Java parameters by value? Pass by reference

Source: Internet
Author: User

Passing Java parameters by value? Pass by reference

Sometimes I want to know whether to pass by value or by reference when calling a method. Some people say that the basic data type is passed by value, and the reference type is passed by reference. At that time, it seems to make sense.

I will not investigate the ability to identify and read words here. I will explain my understanding of this issue. I don't want to say whether it is passing by value or by reference. I just want to understand it myself, after all, java is the king.

First, let's look at the following code:

 
 
  1. Package shb. java. testmemory;
  2.  
  3. Public class TestMeo {
  4.  
  5. /** Pass the basic data type and reference type parameters by value
  6. * @ Description:
  7. * @ Author shaobn
  8. * @ Param args
  9. * @ Date: 7:53:56 AM
  10. */
  11. Public static void main (String [] args ){
  12. // TODO Auto-generated method stub
  13. TestInt ();
  14. TestStr ();
  15. TestPack ();
  16. TestObj ();
  17. TestObj_2 ();
  18. }
  19. // NO1. Test Basic Data Type
  20. Public static void testInt (){
  21. Int num1 = 12;
  22. System. out. println ("Before change:" + num1 );
  23. ChangeInt (num1 );
  24. System. out. println ("After change:" + num1 );
  25. }
  26. // Test the string type
  27. Public static void testStr (){
  28. String str = "helloworld ";
  29. System. out. println ("Before change:" + str );
  30. ChangeStr (str );
  31. System. out. println ("After change:" + str );
  32. }
  33. // Test the packaging type
  34. Public static void testPack (){
  35. Integer integer = new Integer (42 );
  36. System. out. println ("Before change:" + integer );
  37. ChangePack (integer );
  38. System. out. println ("After change:" + integer );
  39. }
  40. // Test the reference type
  41. Public static void testObj (){
  42. Person person = new Person ();
  43. System. out. println ("Before change:" + person. age );
  44. ChangeObj (person );
  45. System. out. println ("After change:" + person. age );
  46. }
  47. // Method 2 of the test reference type
  48. Public static void testObj_2 (){
  49. Person person = new Person ();
  50. System. out. println ("Before change:" + person. age );
  51. ChangeObj_2 (person );
  52. System. out. println ("After change:" + person. age );
  53. }
  54. Public static void changeInt (int num ){
  55. Num = 21;
  56. }
  57. Public static void changeStr (String str ){
  58. Str = "hellobeijing ";
  59. }
  60. Public static void changePack (Integer integer ){
  61. Integer = new Integer (89 );
  62. }
  63. Public static void changeObj (Person person ){
  64. Person. age = 87;
  65. }
  66. Public static void changeObj_2 (Person person ){
  67. Person = new Person ();
  68. Person. age = 78;
  69. }
  70. }
  71. // Reference type test class
  72. Class Person {
  73. Public int age = 78;
  74. }

Look NO1:

 

Note: The two images I have drawn above are not exactly the same, so we can only do this. Let's analyze: when the data is of the basic data type, only a Copy of the real parameter is passed to the form parameter). Of course, due to the Features shared by the stack memory variables, these two variables point to the value of this variable.

When we change the form parameter, first, we will find whether new variable values exist in the stack memory. If yes, it points to the new variable value to reflect the characteristics of stack memory data sharing ). If no, open up a space in the stack memory to store new variable values. Meanwhile, the variable points to the new variable value.

At this time, we found that the variable value has nothing to do with the variable of the real parameter, two independent variables. Therefore, the variable value changed after the function is irrelevant to the previous one, so the previous variable value is output.

In addition, we can see that when passing an object reference, the person reference variable stores the memory address of the Person object in the heap memory, therefore, the memory address is transmitted as a string of numbers ). At this time, the two parameters have a common memory address value, so they point to the same memory object. We observe

It is found that when we change the attribute values of an object, we feel like a whole body. As long as you change this object, this object will be changed, but there is no other concept to open up an object except the String type and the packaging type ).

PS: I haven't finished writing it yet. I'm at work. Try again at night!

In case of any errors, please help us to correct them.

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.