Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>By "override" I mean to "delete" first object, ant to fill the memory with the second one. Is it possible?</p> </blockquote> <p>Yes and no.</p> <p><strong>Yes</strong> - If you allocate some memory with Unsafe and write a <a href="http://robaustin.wikidot.com/how-to-write-to-direct-memory-locations-in-java" rel="nofollow">long</a>, then write another long in it (for example), then yes, you have deleted the first object and filled the memory with a second object. This is similar to what you can do with <a href="http://download.oracle.com/javase/6/docs/api/java/nio/ByteBuffer.html" rel="nofollow">ByteBuffer</a>. Of course, long is a primitive type, so it is probably not what you mean by 'object'.</p> <p>Java allows this, because it has the control on allocated memory. </p> <p><strong>No</strong> - Java works with references to objects and only provides references to these objects. Moreover, it tends to move objects around in memory (i.e., for garbage collection).</p> <p>There is no way to get the 'physical address' and move memory content from one object address to another, if that's what you are trying. Moreover, you can't actually 'delete' the object, because it may be referenced from elsewhere in the code.</p> <p>However, there is always the possibility of having reference A point to another objectB instead of objectA with <code>A = objectB;</code> You can even make this atomic with <code>Unsafe.compareAndSwapObject(...)</code>.</p> <p><strong>Workaround</strong> - Now, let's imagine that reference A1, A2, A3 point to the same objectA. If you want all of them to suddently point to objectB, you can't use <code>Unsafe.compareAndSwapObject(...)</code>, because only A1 would point to objectB, while A2 and A3 would still point to objectA. It would not be atomic.</p> <p>There is a workaround:</p> <pre><code>public class AtomicReferenceChange { public static Object myReference = new Object(); public static void changeObject(Object newObject) { myReference = newObject; } public static void main(String[] args) { System.out.println(AtomicReferenceChange.myReference); AtomicReferenceChange.changeObject("333"); System.out.println(AtomicReferenceChange.myReference); } } </code></pre> <p>Instead of having multiple references to the same object, you could define a public static reference and have your code use <code>AtomicReferenceChange.myReference</code> everywhere. If you want to change the referenced object atomically, use the static method <code>changeObject(...)</code>.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload