Java中变量的内存地址 [英] Memory address of variables in Java

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

问题描述

请看下面的图片.当我们使用 new 关键字在 java 中创建对象时,我们从操作系统获取内存地址.

Please take a look at the picture below. When we create an object in java with the new keyword, we are getting a memory address from the OS.

当我们编写 out.println(objName) 时,我们可以看到一个特殊"字符串作为输出.我的问题是:

When we write out.println(objName) we can see a "special" string as output. My questions are:

  1. 这是什么输出?
  2. 如果是操作系统给我们的内存地址:

  1. What is this output?
  2. If it is memory address which given by OS to us:

a) 如何将此字符串转换为二进制?

a) How can I convert this string to binary?

b) 如何获得一个整数变量地址?

b) How can I get one integer variables address?

推荐答案

就是类名和System.identityHashCode() 由 '@' 字符分隔.身份哈希码所代表的内容是特定于实现的.它通常是对象的初始内存地址,但随着时间的推移,VM 可以将对象移动到内存中.所以(简而言之)你不能指望它成为任何东西.

That is the class name and System.identityHashCode() separated by the '@' character. What the identity hash code represents is implementation-specific. It often is the initial memory address of the object, but the object can be moved in memory by the VM over time. So (briefly) you can't rely on it being anything.

在 Java 中获取变量的内存地址是没有意义的,因为 JVM 可以自由地实现对象并按照看起来合适的方式移动它们(您的对象可能/将在垃圾收集期间移动等)

Getting the memory addresses of variables is meaningless within Java, since the JVM is at liberty to implement objects and move them as it seems fit (your objects may/will move around during garbage collection etc.)

Integer.toBinaryString() 会给你一个二进制形式的整数.

Integer.toBinaryString() will give you an integer in binary form.

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

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