Whether Java is a value or a reference

Source: Internet
Author: User
Tags stringbuffer

pointers, references, handles in 1,c/c++

definition of an object in C++primer: An object is a piece of memory that can store data and have some kind of space, an object A, which has a value and an address of &a.

pointer:p is also an object, it also has the address &p and stored value p, except that p stores the data type as the memory address of the data.

The object has a constant (const) and a variable, since the pointer itself is an object, then the address stored by the pointer also has a constant and variable, the constant pointer refers to the pointer that the object is stored in the address can not be changed, and point to the constant the pointer means that the object pointed to by this pointer cannot be changed by this pointer.

Reference: A reference is an alias to an object and can be viewed as a constant pointer, but more secure than a normal pointer function (one advantage of a reference is that it must not be empty). When defining a reference, the program binds the reference to its initial value, rather than copying it. The computer must be initialized at the same time that R is declared, and when R is declared, it can no longer be bound together with other objects.

Handle: A pointer to a pointer , the handle is actually a data, is a long (full-length) of the data. A handle is an identifier that identifies an object or item as if it were our name.

Windows is a virtual memory-based operating system. In this system environment, the Windows Memory manager often moves objects back and forth in memory to meet the memory needs of various applications. The object being moved means that its address has changed. If the address always changes so, where should we go to find the object ? To solve this problem, the Windows operating system frees up some internal storage addresses for each application to specifically register the address changes in memory for each application object, and the address (the location of the storage unit) itself is unchanged. When the Windows Memory manager moves the object's location in memory, it stores the address of the new object to the handle.

references and handles in 2,java

1) 3 Types of data in Java:
Basic data types (numeric (integer type (int,short,byte,long), floating-point type (float,double)), character type (char), Boolean type (Boolean));

Reference data type (Class), Interface (interface), array ([]));

NULL type;

2) references and handles in Java

Handle : to distinguish the variable identifier of the reference type and the variable identifier of the base data type, we specifically (deliberately) use handle to address the variable identifier of the reference type.

The handle is actually a variable, which is different from the key of the basic variable, it is an indirect addressing method.

Reference : The reference to the object is the return value when the object was created (the reference is the return value of the new expression).

Example: New A (); The object is actually created here, but we do not have the handle to hold the reference (hold, take, save). Handle is a variable, and reference is a variable value.

(1) The handle a--is common as a;

(2) Create Object--new A (); This is the real object to create. Objects are typically created with new expressions. The whole process of calculating the value of the new expression, the creation of the object and the initialization of the object's own member variables are done on the microscopic level, and the value of the new expression is obtained on the macro, which is called the reference of the novel object;

(3) Reference: The value of new A (). A reference can be simply regarded as the address of an object occupying memory space , and it can be conveniently distinguished from other objects by reference to the object's unique identity.

(4) Initialization of the handle: Handle = reference, which assigns a reference to a handle, such as a statement a=new A (), and after the initialization of the handle, the object can be remotely controlled with a handle.

Reference:

Http://www.cnblogs.com/lsjwzh/archive/2010/05/08/1730480.html

3, the origin of the problem of transmitting value and transmitting reference

When you pass a data as a parameter to a function in C, there are two ways: pass a value, or pass a pointer, and the difference between them can be explained by a simple example:

1 voidSwapvalue (intAintb) {2     intt =A;3A =b;4b =T;5 }6 voidSwappointer (intAint*b) {7     intt = *A;8* A = *b;9* B =T;Ten } One voidMain () { A     intA =0, B =1; -printf"1:a =%d, B =%d\n", A, b); - Swapvalue (A, b); theprintf"2:a =%d, B =%d\n", A, b); -Swappointer (&a, &b); -printf"3:a =%d, B =%d\n", A, b); -}

Operation Result:

1:a = 0, B = 1
2:a = 0, B = 1
3:a = 1, b = 0

As you can see clearly, passing parameters by pointer can easily modify the values passed in through the parameters, but not by value.

In Java:

Using a method like Swapvalue still cannot change the value of a simple data type passed in by argument, but if it is an object, it may change its members arbitrarily.

This is much like the problem of passing a value/passing pointer in C language. But there are no pointers in Java, so a lot of people turn this problem into a value-for-pass/reference problem.

Parameter passing in the 4,java
1  Public classTest {2      Public Static voidTestBooleanTestintAint[] b, String C,stringbuffer D) {3Test =!test;4A = 10;5         inttemp = b[0];6B[0] = b[1];7B[1] =temp;8         c = "CC"; 9         d.append ("word"); TenSystem.out.println ("in Test (Boolean): Test =" +test); OneSystem.out.println ("in A (int): a =" +a); ASystem.out.println ("in B (int[]): b =" + b[0] + b[1]); -System.out.println ("in C (String): c =" +c); -System.out.println ("in D (stringbuffer): D =" +d); the System.out.println ();  -     } -      Public Static voidMain (string[] args) { -         BooleanTest =true; +         intA = 1; -         int[] B = {6,7}; +String C =NewString ("C"); AStringBuffer d =NewStringBuffer ("Hello"); atSystem.out.println ("Before Test (Boolean): Test =" +test); -System.out.println ("before A (int): a =" +a); -System.out.println ("Before B (int[]): b =" + b[0] + b[1]); -System.out.println ("Before C (String): c =" +c); -System.out.println ("Before D (stringbuffer): D =" +d); - System.out.println ();  in test (test,a,b,c,d); -System.out.println ("After Test (Boolean): Test =" +test); toSystem.out.println ("after a (int): a =" +a); +System.out.println ("After B (int[]): b =" + b[0] + b[1]); -System.out.println ("After C (String): c =" +c); theSystem.out.println ("After D (stringbuffer): D =" +d); *     } $}

Look at the online there are many debates on this issue, I think the main object, reference, handle these concepts of the understanding of different causes.

Based on the above concepts and experimental results, we conclude that:

1) parameter passing for the base data type is passed by value , that is, a copy of the base data type in the incoming method.

For example, in the experiment, the Boolean type of test and the int type A, the incoming method changes its value, the source value does not change.

2) parameters for reference data types are passed by reference (the object's stored address value) , which is a copy of an instance (object) that is not a reference data type passed in to the method.

So why did the string type C in the above experiment not change the source object?

Let's take a closer look at the first step of passing a reference to the "C" object to the handle in the method C (where the handle (variable) c is in the method is not related to the handle C in the main () function);

In the second step, the reference to the "CC" object is assigned to the handle C in the method. (The key is that the assignment here makes the handle C in the method has nothing to do with the object in the main () function)

So, the reference to the handle C in the method is the address of the "CC" Object, and the reference to the handle C in the main () function is the address of the "C" object.

Reference:

Http://old.bccn.net/Article/kfyy/java/jszl/200601/3069.html

Whether Java is a value or a reference

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.