Java 변수의 메모리 주소
아래 그림을 참조하십시오. new키워드를 사용하여 java에서 객체를 만들면 OS에서 메모리 주소를 얻습니다.
우리가 쓸 때 out.println(objName)"특별한"문자열을 출력으로 볼 수 있습니다. 내 질문은 :
- 이 결과는 무엇입니까?
OS가 우리에게 제공 한 메모리 주소 인 경우 :
a)이 문자열을 이진수로 어떻게 변환 할 수 있습니까?
b) 하나의 정수 변수 주소를 어떻게 얻을 수 있습니까?

클래스 이름과 System.identityHashCode () 는 '@'문자로 구분됩니다. 아이덴티티 해시 코드가 나타내는 것은 구현에 따라 다릅니다. 종종 개체의 초기 메모리 주소이지만 시간이 지남에 따라 VM에서 개체를 메모리에서 이동할 수 있습니다. 따라서 (간결하게) 아무것도 아닌 것에 의존 할 수 없습니다.
JVM이 객체를 구현하고 적절하게 보이는대로 자유롭게 움직일 수 있기 때문에 변수의 메모리 주소를 얻는 것은 의미가 없습니다 (가비지 수집 중에 객체가 움직일 수 있음).
Integer.toBinaryString () 은 이진 형식의 정수를 제공합니다.
그것은 가능합니다 sun.misc.Unsafe: @ Peter Lawrey의 훌륭한 답변을보십시오-> 참조 주소를 얻는 방법이 있습니까?
printAddresses () 코드를 사용하여 :
public static void printAddresses(String label, Object... objects) {
System.out.print(label + ": 0x");
long last = 0;
int offset = unsafe.arrayBaseOffset(objects.getClass());
int scale = unsafe.arrayIndexScale(objects.getClass());
switch (scale) {
case 4:
long factor = is64bit ? 8 : 1;
final long i1 = (unsafe.getInt(objects, offset) & 0xFFFFFFFFL) * factor;
System.out.print(Long.toHexString(i1));
last = i1;
for (int i = 1; i < objects.length; i++) {
final long i2 = (unsafe.getInt(objects, offset + i * 4) & 0xFFFFFFFFL) * factor;
if (i2 > last)
System.out.print(", +" + Long.toHexString(i2 - last));
else
System.out.print(", -" + Long.toHexString( last - i2));
last = i2;
}
break;
case 8:
throw new AssertionError("Not supported");
}
System.out.println();
}
이 테스트를 설정했습니다.
//hashcode
System.out.println("Hashcode : "+myObject.hashCode());
System.out.println("Hashcode : "+System.identityHashCode(myObject));
System.out.println("Hashcode (HEX) : "+Integer.toHexString(myObject.hashCode()));
//toString
System.out.println("toString : "+String.valueOf(myObject));
printAddresses("Address", myObject);
출력은 다음과 같습니다.
Hashcode : 125665513
Hashcode : 125665513
Hashcode (HEX) : 77d80e9
toString : java.lang.Object@77d80e9
Address: 0x7aae62270
결론 :
- 해시 코드! = 주소
- toString = class @ HEX (해시 코드)
이것이 Object의 "toString ()"구현 결과입니다. 클래스가 toString ()을 재정의하면 완전히 다른 것을 인쇄합니다.
입니다 메모리 주소가 아닌 이것은 클래스 명에 @ 해시 코드
어디
classname = full qualified name or absolute name (ie package name followed by class name)
hashcode = hexadecimal format (System.identityHashCode(obj) or obj.hashCode() will give you hashcode in decimal format)
Like Sunil said, this is not memory address.This is just the hashcode
To get the same @ content, you can:
If hashCode is not overridden in that class:
"@" + Integer.toHexString(obj.hashCode())
If hashCode is overridden, you get the original value with:
"@" + Integer.toHexString(System.identityHashCode(obj))
This is often confused with memory address because if you don't override hashCode(), the memory address is used to calculate the hash.
What you are getting is the result of the toString() method of the Object class or, more precisely, the identityHashCode() as uzay95 has pointed out.
"When we create an object in java with new keyword, we are getting a memory address from the OS."
It is important to realize that everything you do in Java is handled by the Java Virtual Machine. It is the JVM that is giving this information. What actually happens in the RAM of the host operating system depends entirely on the implementation of the JRE.
In Java when you are making an object from a class like Person p = new Person();, p is actually an address of a memory location which is pointing to a type of Person.
When use a statemenet to print p you will see an address. The new key word makes a new memory location containing all the instance variables and methods which are included in class Person and p is the reference variable pointing to that memory location.
this is useful to know about hashcode in java :
http://eclipsesource.com/blogs/2012/09/04/the-3-things-you-should-know-about-hashcode/
참고 URL : https://stackoverflow.com/questions/1961146/memory-address-of-variables-in-java
'Programming' 카테고리의 다른 글
| virtualenv-사이트 패키지 없음 및 pip가 여전히 글로벌 패키지를 찾고 있습니까? (0) | 2020.07.07 |
|---|---|
| Math.random () 대 Random.nextInt (int) (0) | 2020.07.07 |
| 하이퍼 링크에서 다른 포트 번호에 대한 상대 URL? (0) | 2020.07.07 |
| HTML 요소의 컨텐츠가 오버 플로우되는지 판별 (0) | 2020.07.07 |
| Git에서 원격 브랜치 리베이스 (0) | 2020.07.07 |