+ 3
Can we print address of an object in java? If yes, then how is it possible?
9 Antworten
+ 5
Actually address can be obtained with sun.misc.Unsafe
But it is really very unsafe.
Because the JVM often moves objects around in memory when doing garbage collection and to improve security. So you can't use raw pointers to trash memory or access nonexistent objects.
Note: summary from stackoverflow
+ 1
i cant find hasCode method .
0
we can't print.
0
unfortunately no.
0
We can also use hashCode() method to print the address of an object.
Simply do "obj_name.hashCode();"
0
Do this👇
class A
{
public static void main(String [] ar){
A a1 = new A();
System.out.println(a1.hashCode());
}
}
0
thanks when i restart ide ,done.it was ide's problem .
0
Yes you can print the address of the object on the output screen , There are two methods . 1. Using the reference variable name 2. Hash code method
I will explain you the easiest way of getting the address of an object with an example .
Step 1: Create an object for your class to access the non static members
Step 2: Display the address of the object on the output screen
Example
From the following example program you can create an object with the class Demo
Demo ref = new Demo(); //This will create an object with reference variable named "ref".
System.out.println(ref);//This will display the address of the object.
0
Can we print address of string in java