Java中的参考究竟是什么? [英] What exactly is a reference in Java?

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

问题描述

Java中的参考究竟是什么?它是一个内存地址吗? Java引用是否相当于解除引用的C ++指针?

What exactly is a reference in Java? Is it a memory address? Is a Java reference the equivalent of a dereferenced C++ pointer?

换句话说,给出以下内容:

In other words, given the following:

Object o1 = new Object();
Object o2 = new Object();

o1 == o2

以上比较是否相当于比较两个C ++中的指针?

Is the above comparison the equivalent of comparing two pointers in C++?

推荐答案

o1 == o2 几乎相同比较C / C ++中的两个指针,是的。

o1 == o2 is pretty much equivalent to comparing two pointers in C/C++, yes.

但是Java中的引用和C / C ++中的指针之间存在两个主要区别:

But there are a two main differences between references in Java and pointers in C/C++ that are quite important:


  • Java引用不能做指针运算:你不能添加3到引用,你只能 让它指向另一个(已知的)对象

  • 强烈输入Java引用:你不能重新解释引用另一端的内容,除非你将它重新解释为该对象实际上是的类型。

  • Java references can't do pointer arithmetic: you can't "add 3" to a reference, you can only let it point to another (known) object
  • Java references are stongly typed: you can't "reinterpret" what lies on the other end of a reference unless you reinterpret it as a type that that object actually is.

这也是关于这个词的简短说明引用:C ++的引用与C中的指针和Java中的引用完全不同(但我对C ++的了解并不足以告诉您指定的内容) ics)。

Also a short note about the word "reference": C++ has references that act quite differently from both pointers in C and references in Java (but I don't know enough about C++ to tell you the specifics).

有关此问题的详细讨论,请参阅关于programmers.SE的这个相关问题

For a thorough discussion of this, see this related question on programmers.SE.

这篇关于Java中的参考究竟是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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