Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Thursday, December 31, 2015

Memory address of variables in Java

Memory address of variables in Java


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

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

  1. What is this output?
  2. If it is memory address which given by OS to us: a) How can I convert this string to binary? b) How can I get one integer variables address?

alt text

Answer by Brian Agnew for Memory address of variables in Java


That is the object 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.

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() will give you an integer in binary form.

Answer by Paul Tomblin for Memory address of variables in Java


That is the output of Object's "toString()" implementation. If your class overrides toString(), it will print something entirely different.

Answer by James Poulson for Memory address of variables in Java


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.

Answer by Sunil Kumar Sahoo for Memory address of variables in Java


This is not memory address This is classname@hashcode

where

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)

Answer by JBE for Memory address of variables in Java


It is possible using sun.misc.Unsafe : see this great answer from @Peter Lawrey -> Is there a way to get a reference address?

Using its code for 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();  }  

I set up this test :

    //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);  

Here is the output :

Hashcode :       125665513  Hashcode :       125665513  Hashcode (HEX) : 77d80e9  toString :       java.lang.Object@77d80e9  Address: 0x7aae62270  

Conclusion :

  • hashcode != address
  • toString = class@HEX(hashcode)

Answer by user3665019 for Memory address of variables in Java


this is useful to know about hashcode in java :

http://eclipsesource.com/blogs/2012/09/04/the-3-things-you-should-know-about-hashcode/

Answer by Panduka Bupendra for Memory address of variables in Java


In java when you are making an object from a class as below Person p=new Person();

p is actually an address of a memory location which is pointing to a type of Person. when u put a printing statement on p you will see an address.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.


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.