[Reference differences] References in c ++ differ from references in java.

Source: Internet
Author: User

[Reference differences] References in c ++ differ from references in java.
Summary:

In Java, the class instance declaration and construction are separated. "T a;" is a declaration, and "a = new T ();" is a construction. The reference description is the same as that of C ++. However, the mechanism of Java is different from that of C ++. A reference in Java is also called a handle, or a handle is its real name. The class declares a handle. Calling the constructor will direct the handle to the class instance. Therefore, Java does not have a replication function similar to C ++, Because Java copies the handle content directly. For example, "T B = a;" only means that the handle of a is copied (assigned) to B, and B also points to the class instance pointed to by. We can see that Java and C ++ are different here. Java still has only one instance, while C ++ has two instances.

Therefore, in a function, all Java form parameters are input parameter handle copies, and are shortest copies (only the handle is copied, rather than the next handle pointed by the handle ). Therefore, in a function, directly modifying the form parameter cannot change the input parameter. However, if you modify the handle of the next layer of the object to which the form parameter points, the input parameter is modified. Therefore, Java does not have the same Swap function as C/C ++. The Return Value of the function. If a local variable class instance is constructed in a function, it can be returned to the outside. Of course, the local variable handle does not exist.

To copy objects in Java, You need to reload the clone function, and distinguish between the shortest copy and the deep copy (a new object is constructed completely, and the internal data of the two is different from that of the instance ).

 

Comparison between c ++ and java references:

The address referenced in c ++ does not change, but the content directed to the address is changed. However, the address referenced in java is changing !!
If you have to compare them, the "Reference" in Java is more like the pointer of C/C ++, which is different from the "Reference" of C ++.

Java removes the pointer concept and uses references...
Java:
A a = new A (1 );
A B = new A (2 );
B =;
No problem. a and B reference the same object A (2). The original A (1) is not referenced. The garbage collection mechanism will take out A (1) at A later time.

C ++ does not. The reference of C ++ is semantically an alias [essentially a const pointer, also called a pointer constant], rather than a pointer usage:
A a = A (1 );
A B = A (2 );
A & c = B; // c is the alias of B.
C = a; // not c references a, but c. operator = ()

For the language mechanism, java references are used to manage and name objects;
However, the reference mechanism of C ++ is pure and just an alias.

The features of each language are an integral part.

We know that java's reference mechanism is a complicated mechanism. He must distinguish between "Basic Object" and "composite object". You can imagine how to replicate an object if there is no basic object in it? The only solution is to provide two equal signs, or use constructors all... but in summary, he and garbage collection form a perfect combination.

The reference mechanism of C ++ greatly supports Operator overloading. The reference of C ++ is the fundamental requirement for simulating basic objects using classes. If C ++ uses the reference of java, it is difficult to implement the beautiful operator [] and proxy class. Furthermore, the operator overloading of C ++ provides strong support for the template mechanism of c ++.

In c ++, the reference is only an alias for a variable. Once defined, it cannot be modified, that is, it cannot point to other variables, such as in a program, any operation referenced by a is equivalent to an operation of.

The reference of java definition is not like this. In java, references are equivalent to pointers, which are different from pointers in c:First, address operations cannot be performed by referencingSuch as the add-on operation of the array, which means that the reference only points to a copy of the data, rather than the data itself. This avoids changing the values of other variables due to incorrect address operations, even endangering the security of the system.Second, references in java can only point to objects.The system directly generates a reference when instantiating an object. Therefore, common data types cannot be referenced and defined, if you want to pass the address when calling a function for a common data type (that is, passing a reference in java), you must encapsulate the data in the class.
Java makes it possible to implement the same function as the pointer in c when passing java functions or class parameters.

 

Specific applications:

Pointers and references are often used in C ++, but many beginners are not familiar with the differences between them. Let's talk about the differences and usage between them.

1. Definitions and Properties of pointers and references:

(1) pointer: the pointer is a variable, but the variable stores an address and points to a memory storage unit. The reference is essentially the same thing as the original variable, it is just an alias of the original variable. For example:

Int a = 1; int * p = &;

Int a = 1; int & B =;

An integer variable and a pointer Variable p are defined above. The pointer variable points to the storage unit of a, that is, the value of p is the address of the storage unit of.

The following two sentences define an integer variable a and the reference B of this integer a. In fact, a and B are the same thing and occupy the same storage unit in the memory.

(2) A const pointer can be provided, but no const reference is provided;

(3) the pointer can have multiple levels, but the reference can only be a level 1 (int ** p; valid while int & a is illegal)

(4) the pointer value can be NULL, but the referenced value cannot be NULL, and the reference must be initialized during definition;

(5) the pointer value can be changed after initialization, that is, it points to other storage units, and the reference will not change after initialization.

(6) "sizeof reference" gets the size of the variable (object) pointed to, while "sizeof Pointer" gets the size of the pointer;

(7) pointer and reference auto-increment (++) operations have different meanings;

2. What is the difference between a pointer and a reference passed as a function parameter.

(1) the pointer is passed as a parameter:

#include
 
  using namespace std;void swap(int *a,int *b){  int temp=*a;  *a=*b;  *b=temp;}int main(void){  int a=1,b=2;  swap(&a,&b);  cout<
 

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.