What is the reference type?

Source: Internet
Author: User

I believe that those who use C ++ will not be unfamiliar with the reference type. However, Do you think pointers seem more important? What can be done by referencing? Can pointers be done in the same way? In addition, the function is more powerful and the usage is more flexible. So how does reference work? Father of C ++: Why did Dr. bengiani strauster lup create a reference type? Isn't it because the pointer is the parent son?

First, let's compare the differences between the reference type and the pointer type:
1. The reference type cannot be null, but the pointer can
2. the referenced implementations must be based on the currently available variables. They are just aliases of existing variables. they cannot survive independently. the reference type is like a biological virus. If you leave the host, they cannot survive alone. pointer types are different. They are independent data types. You can create and use them freely like int type.
3. The reference type is always bound with the variable and cannot be changed to its host. How much freedom does the pointer have? I think you understand.
4. Do not assign values to each other for the reference type, and you can do anything with the pointer as you like. Nothing else can be done.
5. the address of the reference type itself cannot be obtained. You cannot view the address of the reference type itself. What you can see is always the address of the variable referenced by the reference type. however, the pointer can easily display the address of the pointer.
 
Next, let's take a look at java.
I don't know what the reference type of Java looks like, does it look like C ++? It looks like this on the surface. But what about it?
All Java users know that its memory model is much simpler than C ++. objects in Java can only be placed in the heap. you can only use pointers to access objects. you may ask, where is the Java pointer? I want anyone who has some understanding of Java to understand, right. it is actually a reference type in Java. only James Gauss and his friends changed the Java pointer to a reference. the name is different.
Java can only access objects through pointers, which also promotes its syntax to omit the "*" number when accessing objects, that is, the reference type in Java.
Therefore, the reference type of Java is different from that of C ++. In essence, C ++ has references and pointers, while Java only has pointers. the most obvious example is that in C ++, values are not allowed between reference types, but you will find that you can do this in Java. the root cause is here.
Java removes the pointer operation function for the sake of security. c ++ can, of course, perform operations on pointers, such as auto-increment, auto-increment, and auto-increment. the high cost of C ++ freedom is not safe enough, because once pointer movement is out of control, a wild pointer will appear. therefore, if you use C ++, you must force yourself to become a master rather than just use the language. c ++ has higher requirements on programmers. It is a sharp sword, and you can control it. see your opinion and wisdom.
Here, we also want to talk about not always comparing languages, but not necessarily. in fact, C ++ can do anything in Java to develop general software functions, but what Java cannot do is also because of the C language functions in C ++. rather than simply C ++. I hope you can figure it out.

 

What is the reference type?

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.