Java object, object reference relationship and parameter passing discussion

Source: Internet
Author: User

In the process of learning Java, it is easy to confuse objects and object references, and we usually communicate for the sake of convenience is just the object, how to object, but many times we operate, basically is the object, but the direct operation is the object reference.

Post a blog post, very visually explain the relationship between object and object reference

    • . The Java language uses object references to manipulate objects, one thing to be aware of is that objects and object references are stored in different places because objects tend to occupy a larger space, so objects are stored in the heap, and for ease of operation, the object's references are stored on the stack.
    • Object obj = new Object (); This action allows you to create an object in Java, which actually contains 4 actions:

      • New object, which creates an object in the heap as a template for the object class
      • New Object (), calling the construction method of the object class to initialize the object that was just generated
      • The "Object obj" on the left creates a reference variable for the object class, that is, the obj variable is a reference to an object that can point to the object.
      • The "=" operator references obj to the object that was just created
    • It is possible to approximate the reference to an object in Java as a pointer in C, where the object reference is stored in the stack by the address of the object it points to in the heap.

    • The basic type in Java is not created by new, although the underlying type variable is stored on the stack, but it is not a reference variable, and the variable stores the value directly, thus making it more efficient. In addition, because Java requires object-oriented, the basic types will have corresponding wrapper classes. The value operations on these wrapper classes are actually implemented by their corresponding basic type operations.

There are two main uses of wrapper classes:
A, as the basic data type corresponding to the class type exists, convenient to involve the operation of the object.
b, contains the relevant attributes for each basic data type, such as maximum, minimum, and related methods of operation.
C, note that the basic type of Java and its corresponding wrapper type of the size of the storage space, and not as in most other languages as the machine hardware architecture changes, which makes the Java program more portable.

    • The reference to an object in Java is stored in the stack, but the object is stored in the heap, because the object is manipulated by reference to the object, so if an object does not have a reference variable to it, the object becomes "garbage" and requires a garbage collection by the JVM.

    • Java has only one way of passing parameters: that is, passing by value, that is, passing everything in Java is a pass value. If the incoming method is something of the base type (or its corresponding package type), you get a copy of this basic type. If you are passing a reference, you get the referenced copy, which points to the same address space.
      Basic types in Java and their corresponding package types:

      Parameter passing of these types of variables is a value pass, and although the string type is not the base type, it can be treated as a wrapper class of char[], which explains why a variable of type string is the reason for the value passing when the method argument is passed. This is why when the manipulation of strings is implemented in different ways, it is recommended that you use StringBuffer for real reasons.

 Public classMain { Public Static void OP1(StringBuffer s) {S.append ("world!"); System. out. println ("s:"+s); } Public Static void OP2(StringBuffer s) {s =NewStringBuffer ("Java"); System. out. println ("s:"+s); } Public Static void Main(string[] args) {//TODO auto-generated method stubStringBuffer str; str =NewStringBuffer ("Hello"); System. out. println ("STR:"+STR);        OP1 (str); System. out. println ("STR:"+STR);        OP2 (str); System. out. println ("STR:"+STR); }}

To understand the operating mechanism of value passing in Java, it is best to understand from the perspective of the allocation of Java memory space, after all, objects and objects are referenced in different locations, so the Java object and the reference relationship can be said to be interrelated, but independent of each other. The main manifestation of each other's independence is that the reference can be changed, and it can point to other objects.

Operation Result:

Analysis:

OP1 Operation:

OP2 operation: Str,s is a two independent reference variable, S is a newly created variable in Java in memory, when the S point to "Java", the original STR still points to "Hello World".

When a parameter is passed, if it is a reference to an object, copy the object reference so that it points to the same address space.

 Public classMain { Public Static void OP1(String s) {s + ="world!"; System. out. println ("s:"+s); } Public Static void OP2(String s) {s =NewString ("Java"); System. out. println ("s:"+s); } Public Static void Main(string[] args) {//TODO auto-generated method stubString str; str =NewString ("Hello"); System. out. println ("STR:"+STR);        OP1 (str); System. out. println ("STR:"+STR);        OP2 (str); System. out. println ("STR:"+STR); }}

Operation Result:

Analysis: You can see that the external method has no effect on STR because the Java language specifies that the string is immutable, and any manipulation of the string results in a new object.

In Java, "=" cannot be considered an assignment statement, it does not assign an object to another object, its execution essentially passes the address of the right object to the left reference, so that the left reference points to the object on the right. There is no pointer on the Java surface, but its reference is essentially a pointer, and the reference is not the object, but the address of the object, so that the reference points to the object. In Java, the "=" statement should not be translated into an assignment statement because it does not perform a process of assignment, but rather a process of passing an address, and being translated into an assignment statement can cause many misunderstandings and inaccurate translations.
In summary, when initializing, the "=" statement to the left is a reference, and the right new is the object.
When the "=" statement is referenced around the back, the reference to the left and right refers to the object to which the reference is pointing.
When a parameter is passed, especially when a reference to an object is passed, it is important to note that the "formal parameter" and "argument" are independent, and that the formal parameter does exist in the stack, so if you point the parameter to another object in the calling method, the argument still points to the original object. Null is also an "object", in Java the parameter value is null, and the value of the argument is not equal to NULL.

Java has only one way of passing parameters: that is, passing by value, that is, passing everything in Java is a pass value. If the incoming method is a basic type of thing, you get a copy of this basic type. If you are passing a reference, you get the referenced copy, and the variable (formal parameter) that receives the reference, although it points to the same block of memory as the argument, gets the same result when the other variable accesses the object operation, but if the parameter points to another object in this external method, Then formal parameters and arguments are independent, and the next operation will not have any effect on the other! For example, if you want to set a variable to NULL, do not implement it through the method SetNull of the class, because that would simply make the formal parameter null, without any effect on the argument, and Null would be assigned directly to NULL.

Java object, object reference relationship and parameter passing discussion

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.