+ 3

Can we print address of an object in java? If yes, then how is it possible?

3rd Sep 2016, 5:42 PM
Aditya Patil
Aditya Patil - avatar
9 Réponses
+ 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
4th Sep 2016, 2:51 PM
WPimpong
WPimpong - avatar
+ 1
i cant find hasCode method .
4th Sep 2016, 5:22 PM
bayram akdemir
bayram akdemir - avatar
0
we can't print.
3rd Sep 2016, 6:00 PM
Mahender
Mahender - avatar
0
unfortunately no.
3rd Sep 2016, 8:40 PM
bayram akdemir
bayram akdemir - avatar
0
We can also use hashCode() method to print the address of an object. Simply do "obj_name.hashCode();"
4th Sep 2016, 5:12 PM
Aditya Patil
Aditya Patil - avatar
0
Do this👇 class A { public static void main(String [] ar){ A a1 = new A(); System.out.println(a1.hashCode()); } }
4th Sep 2016, 5:26 PM
Aditya Patil
Aditya Patil - avatar
0
thanks when i restart ide ,done.it was ide's problem .
4th Sep 2016, 5:29 PM
bayram akdemir
bayram akdemir - avatar
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.
23rd Sep 2020, 8:28 AM
Yogesh
Yogesh - avatar
0
Can we print address of string in java
27th Jan 2022, 1:06 PM
Akash Manjre