Differences between Java reference and C pointer

Source: Internet
Author: User
Tags java reference

First, in Java, there is no reference transfer (that is, address transfer). All values are passed. Naturally, when using objects, they are actually referenced.

The Java language clearly indicates that the pointer is removed, because the pointer is often convenient and also the root cause of code insecurity, and also makes the program very complex and hard to understand, abuse pointer writing code is no less than using the notorious "GOTO" statement. It is absolutely wise for Java to discard the pointer concept. However, there is no explicit pointer definition in the Java language. In essence, every new statement returns a pointer reference, but in most cases, Java does not need to care about how to operate this "Pointer ", don't be as scared as if you were operating on the C ++ pointer.

Also, the basic type of data is stored in the Stack and the data is stored. When an object is generated, only the object reference is stored in the stack to point to an object, and the object itself is stored in Heap.

In addition, the parameters passed during method calling and the temporary variables created in the call are saved in the Stack, which is faster. Other variables, such as static variables and instance variables, are created in Heap, which is slow. In addition, local variables may be further optimized depending on the specific compiler/JVM.

In terms of implementation, the reference can be understood as a restricted pointer. The underlying layer of the reference can be implemented using a pointer or a handle. Early JVMs generally use the handle method. Now the jvm, such as sun and IBM, is generally implemented using pointers.

However, from the language perspective, there is no need to compare the java reference with the C pointer.

The pointer can be added or subtracted from an integer. The two pointers can also be compared and subtracted.

No reference is allowed. Only values can be assigned.

Of course there are differences

A a = new ();

Can you use a = a + 1? Pointer.

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.