Java中的指针 [英] Pointers in Java

查看:181
本文介绍了Java中的指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C ++支持指针,而Java则不支持。但是当许多程序员质疑如何在没有指针的情况下工作时,发起人开始说限制指针。所以我们可以说Java支持限制指针?

C++ supports pointers whereas Java does not. But when many programmers questioned how you can work without pointers, the promoters began saying "Restricted pointers." So we can say Java supports Restricted pointers?

推荐答案

这里的术语非常模糊。

Java支持它所谓的引用。引用很多像指针一样 C / C ++ - 类似语言。的行为与引用在这些语言中的工作方式相同。

Java supports what it calls "references". References act a lot like pointers in C/C++-like languages. They don't act the same way "references" work in those languages.

major C中的指针和Java中的引用之间的区别是:

The major differences between a pointer in C and a reference in Java are:


  • 你不能用Java做指针算法(即您不能从Java引用中添加或减去,您只能 取消引用它或将其与另一个进行比较。)

  • 你可以' t将它转换为不兼容的类型:Java是强类型安全的,你不能重新解释内存中的字节作为其他对象。

  • You can't do pointer arithmetic in Java (i.e. you can't "add" or "subtract" from a Java reference, you can only dereferencere it or compare it with another one).
  • You can't cast it to an incompatible type: Java is strongly type-safe, you can't "re-interpret" the bytes in memory as some other object.

对于使用pointe的一些这没有实际效果(例如链接列表在两种语言中的工作方式基本相同),对于其他人来说差异非常大(C中的数组只是花哨的指针算术,在Java中它们的工作方式完全不同)。

For some uses of pointers this has no real effect (for example linked lists work pretty much the same in both languages), for others the difference is quite major (arrays in C are just fancy pointer arithmetic, in Java they work quite differently).

所以在某种程度上Java引用可以称为限制指针。

So in a way Java references could be called "restricted pointers".

维基百科将指针定义为

Wikipedia defines a pointer as


...一种编程语言数据类型,其值直接指向(或指向)另一个值

... a programming language data type whose value refers directly to (or "points to") another value

强调我的。根据这个 strict 定义,Java没有指针。

Emphasis mine. According to this strict definition, Java doesn't have pointers.

更通用 reference 是指针的超类,但也包含更多抽象的东西,如文件句柄甚至URL。

The more general reference is the superclass of pointers, but also contrains more abstract things like file handles or even URLs.

这篇关于Java中的指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆