JAVA的对象内存地址好像获得不了...
JAVA又不能操作内存
要地址干嘛?
每个对象都内部有个唯一标识(虚拟机的伪地址)
JAVA又不能操作内存
要地址干嘛?
每个对象都内部有个唯一标识(虚拟机的伪地址)
解决方案 »
- 关于如何定位一个HTML/HML页面内容的问题
- CommPortIdentifier 中open()方法乍么没有法返回串口
- 鼠标事件有哪些?
- 用JAVA怎样判断一个地址是IPv4还是IPv6
- !!!!!jdbc连接access时的错误 急 在线等
- 艺术生的困惑:一年的时间能学好java嘛????
- 一个很菜的问题!我是新手!
- java面试要注意什么呢?
- 我用Cloudscape4.0的cloudview建立数据库为何不成功?高手请进!!!
- 启动时报bean没找到错,项目右键maven“Update Project”后就好了是为什么?
- jdbc 连接 Orcale :ORA-03115: 不支持的网络数据类型或表示法
- 请教一个有关java集合的面试题
-1hashCode只是和地址有一定关系,但绝不等于地址!
import java.util.ArrayList;
import java.util.List;public class Prove
{
public static void main(String[] args)
{
List<Integer> list = new ArrayList<Integer>();
for (int i = 0; i < 3000; i++)
{
Prove p = new Prove();
if (!list.contains(p.hashCode()))
list.add(p.hashCode());
}
System.out.println(list.size());
}
}
我生成了3000个Prove对象,应该新分配3000个内存地址,最后却只有2998个hashCode
只有hashcode和内存地址无关
-------------------------------
java菜鸟学堂QQ群144648357
这个输出的不就是个地址码吗?
JAVA中获得内存地址没有必要,因为没有一些内存相关的操作
System.out.println(对象.toString());这里的 toString() 方法是在所有类的基类 Object 中定义的,它的源代码如下:
public String toString() {
return getClass().getName() + "@" + Integer.toHexString(hashCode());
}也就是说,如果一个类没有重写 Object.toString() 方法,那么打印语句得到的都将是:类名+"@"+十六进制表示的对象的哈希值
String b = new String("abc");
System.out.println( a == b ); //false
System.out.println( a.equals(b) ); //true
System.out.println( a.hashCode() == b.hashCode() ); //true
a和b的内存地址肯定是不同的,但是其hashcode却一样
String的hashCode函数。当然不同的对象有不同的hashCode函数,如果override了Object类的hashCode的话。
hashCode
public int hashCode()Returns a hash code for this string. The hash code for a String object is computed as
s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
using int arithmetic, where s[i] is the ith character of the string, n is the length of the string, and ^ indicates exponentiation. (The hash value of the empty string is zero.) Overrides:
hashCode in class Object
Returns:
a hash code value for this object.
See Also:
Object.equals(java.lang.Object), System.identityHashCode(java.lang.Object)
hashCode的值跟内存地址有关,但是也有冲突的时候。
貌似有jmap之后在jhat可以看到地址,不太清楚。