C/C++指针和Java引用

C/C++指针 vs Java引用

原文地址:http://www.geeksforgeeks.org/is-there-any-concept-of-pointers-in-java/

Java没有指针,只有引用。

引用:引用是一个指代其他事物的变量,它可以用来作为该事物的别名。
指针:指针是一个存储内存地址的变量,其目的是用来指代存储在该地址中的事物。

Reference: A reference is a variable that refers to something else and can be used as an alias for that something else.
Pointer: A pointer is a variable that stores a memory address, for the purpose of acting as an alias to what is stored at that address.

因此,指针是引用,但引用不一定是指针。指针是引用的一种特殊实现,它更趋向于使用在能使你直接访问内存地址的语言中。
So, a pointer is a reference, but a reference is not necessarily a pointer. Pointers are a particular implementation of the concept of a reference, and the term tends to be used only for languages that give you direct access to the memory address.

接下来,我们讨论C/C++和Java上下文中指针和引用的一些关键点。
Let’s discuss some keypoints about pointers and references in context of C/C++ and Java:

C/C++有指针运算,而Java(的引用)没有:指针一词带有浓烈的C/C++指针色彩,它是存储内存地址的变量,而且这些变量可以通过算术运算来使其指向其它任意地址。
C/C++ allows pointer arithmetic but Java Pointers (References) not: The term “pointer” is strongly associated with the C/C++ concept of pointers, which are variables which store memory addresses and can be modified arithmetically to point to arbitrary addresses.

而在Java语言中,指针仅存在于引用的实现细节里。当引用的副本被拷贝至被调函数的栈中,它和调用函数中的引用指向了相同的对象,所以你可以操作这个对象。但是 ,你无法改变调用函数指向的对象。
In Java, pointers only exist as an implementation detail for References. A copy of the reference is copied to the stack of a called function, pointing to the same object as the calling function and allowing you to manipulate that object. However you cannot change the object the calling function refers to.

Java没有显示的指针,但存在隐含指针:Java内部使用指针来进行引用操作,但不支持在外部使用。任何Java语言隐含的内部操作都是不可见的。
Java doesn’t support pointer explicitly, But java uses pointer implicitly: Java use pointers for manipulations of references but these pointers are not available for outside use. Any operations implicitly done by the language are actually NOT visible.

指针支持算术运算,引用不可以:通过指针运算来访问内存是极其不安全的,为了安全保护,Java建立了健壮的安全模型,基于此,Java不允许指针运算。任何情况下用户都不被允许操作指针就是一个实例。
Pointers can do arithmetic, References can’t: Memory access via pointer arithmetic is fundamentally unsafe and for safe guarding, Java has a robust security model and disallows pointer arithmetic for this reason. Users cannot manipulate pointers no matter what may ever is the case.

指向的对象:在C中,我们可以通过指针的加减来改变指针指向(加加减减可以使得指针访问到多个对象)。在Java中,引用只能指向一个。你可以让变量保存不同的引用,但是这样的C指针操作是不可能的。
Pointing objects: In C, we can add or subtract address of a pointer to point to things. In Java, a reference points to one thing only. You can make a variable hold a different reference, but such c manipulations to pointers are not possible.

引用是强类型的:Java对引用类型的控制要比C对指针类型的控制更严格。在C中,你仅仅只需要重新解释内存(re-interpret the memory)就可以强制将int*转换为char*。但是,这种重新解释内存在Java中是不行的,你只能在另一端将对象重新解释为它已经是的对象(比如,当一个Object引用指向了一个String,你才可以将Object引用强制转换为String引用)。
References are strongly typed: Type of a reference is much more strictly controlled in Java than the type of a pointer is in C. In C you can have an int* and cast it to a char* and just re-interpret the memory at that location. That re-interpretation doesn’t work in Java: you can only interpret the object at the other end of the reference as something that it already is (i.e. you can cast a Object reference to String reference only if the object pointed to is actually a String).

指针操作是危险的:一方面,让用户操作指针是好事且很灵活,但这也被证明了是危险的。如果不正确地使用指针会造成极大的资源问题,而且也相当容易就会使用不正确。
Manipulation of pointers can be dangerous: On one hand, it can be good and flexible to have control over pointers by user but it may also prove to be dangerous. They may turn out to be big source of problems, because if used incorrectly they can easily break assumptions that your code is built around. And it’s pretty easy to use them incorrectly.

因此,Java没有指针(C/C++意义上的),因为Java不需要用指针来实现通用目的的OOP编程。甚至,加入指针会影响安全性和健壮性,而且也会使得Java语言变得更复杂。

So overall Java doesn’t have pointers (in the C/C++ sense) because it doesn’t need them for general purpose OOP programming. Furthermore, adding pointers to Java would undermine security and robustness and make the language more complex.

你可能感兴趣的:(Java,java,C++,指针,引用)