Java method, the parameters of transmission

  Java always use of the language of the call. Methods have all parameter values is a copy. Method can not change the parameters so the contents of variables. Look at a section of the code below: 

  Int i = 10; 

  Sword.change (i); 

  Whether change () method is how to achieve, method invocation, the value of variable i is still 10. Let us take a closer look into the situation. Assume that we now use one of the ways to increase the value of the variable x three times.: 

  Public void addValue (double x) ( 

  X * x = 3; 

  ) 

  We then call this method: 

  Double perent = 10; 

  Zhangsan.addValue (perent); 

  However, we found that the method invocation, the value is still perent 10, and did not reach double purpose. Let us look at the process of implementation of this procedure: 

  1, x is initialized to a value of the copy perent 

  2, x value * 3 into 30, but the value is still perent 10. 

  3, methods after x 

  Below with a chart to show that: 

  Java in the way there are two kinds of parameters: 

  1, 2 basic types, the object reference 

  It has been that way can not change the basic types of parameters, but for the parameters, the situation is different. 

  Now, we have achieved one of the ways to increase the salaries of their employees three times 

  Public void tripleSalary (Employee x )…{ 
  X.raiseSalary (200); / / x enhance the value of 200% 
  ) 

  Calling implementation 

  Harry = new Employee (); 

  TripeSalary (harry); 

  Specific implementation plans are as follows: 

  Can see the value of x is harry a copy of this x and harry all point to the same object, so x raisesaraly call (200), x and harry all point to the object that the salary was increased by 200 per cent. Methods After x are no longer used, but to continue to harry salaries are increasing at three times that object. 

  Evidently, the method is a copy of the object reference, 2, all point to the same object. 

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Facebook
  • DotNetKicks
  • DZone
  • Netvouz
  • Propeller

Tags: ,

Releated Java Articles

Comments

Leave a Reply