Description of value passing in Java

Source: Internet
Author: User

The concept of value passing is only involved in method invocation! When passing parameters in Java for method invocation, follow the principle of value passing: 1) The basic data type, passing the copy of the data 2) refers to the data type, passing the copy of the referenced address passed, instead of the object itself the landlord's problem involves too much content, First, the following conceptual parameters are understood: parameter variables when method declarations are used to receive arguments passed when calling a method such as: void f (int i) {}   or  void f (String s) {} arguments: What is actually passed when a method call is used to assign a parameter to a parameter such as: F (1   or  F ("abc") variables are divided into 2 categories according to different data types: 1) basic data type variables: 4 classes 8 basic types, byte,short,char,int,long,float,double,boolean such as: int i = 0 ; 2) Reference data type variable: class, interface, array such as: String s = "abc"; note: in Java, except for 4 classes of 8 basic data types, all of the reference data type variables are divided into 2 classes by their declared locations: 1) member variables: Outside the method, Variables declared inside a class 2) Local variables: variables declared inside a method, parameters belonging to the initialization of a local variable variable: 1) member variable: If it is not displayed for initialization, then Java initializes it with the default value 2) local variable: not assigned value, Can not use it seems that the landlord of the concept of references and objects is not a special understanding of the implementation of memory management: Different operating systems are not the same, but the general memory is divided into 4 areas: 1) Heap (heap): Storage of new objects 2) stack (stack): Store local variables 3) data Segment (data area): Static variable and string constant 4) code segment (code area): Store code example: public class Test {void f (int j) {System.out.println (j);} void FF (String ss) {SYSTEM.OUT.PRINTLN (ss);} public static void Main (stirng[] args) {int i = 100; String s = "Hello"; Test T = new test (); T.f (i); T.ff (s);}} When the Mian method starts executing, int i = 100; The stack allocates a piece of space, holds the variable I, the value is 100, the basic data type occupies only one space string s = "Hello"; "Hello" is a string constant, assigned to the data area, the characterString constant is an object of string class, S point to this "Hello" object, which is the reference data type, in memory accounted for two space, a bit of thinking in the image: A reference type, is a piece of memory pointing to another piece of memory s can be called: reference, reference variable, reference address, in fact, S is a pointer, In this case, you know that s is a reference, S value is an address, according to this address can find an object is OK, Test t = new Test (); Similarly, the reference T in the stack points to the new test object in the heap. When passing parameters in Java for a method call, follow the principle of value passing: 1) The basic data type, passing the copy of the data 2) refers to the data type, passing the copy of the referenced address, not the object itself T.F (i); The parameter of the method belongs to the local variable, so when the F method is called, the stack memory is allocated an int type of variable J, the value of I as an argument passed the past, I is the value of 100, now 100 copy to J, then the value of J is 100, which is the landlord said "value transfer", then print At the end of the method, the local variables allocated for the method are emptied immediately, then the J in the stack disappears t.ff (s); When the FF method is called, the stack memory allocates a string-type variable SS, passing the value of S as an argument in the past, and s pointing to the "Hello" object. s value is an address, now copy this address to the SS, then the SS also point to the "hello" this is the landlord said "reference pass", now s and SS two references point to the "Hello" object, and then print SS, the last method ends, This SS in the stack is cleared now that the main method is executed, the local variables for the main method are cleared, the i,s,t all disappears, the string constants of the data area are "Hello" and the test object of the heap memory, because there is no reference to them, it will be exhausted by garbage collector.

Description of value passing in Java

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.