wood burning stoves
The moose likes Java in General and the fly likes Reference variable access Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Reference variable access" Watch "Reference variable access" New topic

Reference variable access

Eddie Menuek

Joined: Jul 11, 2006
Posts: 3
I have the following scenario, where x & y are ref variables, and A and B are objects:


If I now set x=y, what happens if a piece of code tries to use x *whilst* the address of B is being copied to it?

Perhaps this is an OS level issues rather than JVM, any advice appreciated.

Thanks in advance!
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199


Welcome to JavaRanch!

Reads and writes to reference variables, as well as to all variables of four bytes or fewer are guaranteed by the Java language spec to be atomic -- in other words, no other thread will see such a variable in a partially-assigned state. The same is not true for 8-byte variables of types double and long.

There are other issues besides consistency to consider; in the absence of synchronization, if one thread writes to a variable, others may not immediately see the new value, but will continue to see the old.

[Jess in Action][AskingGoodQuestions]
Eddie Menuek

Joined: Jul 11, 2006
Posts: 3
Thanks a lot for your answer, exactly what I wanted to hear

Do you by any chance have a link to an online spec that I can quote in my design decisions doc?

Thanks again!
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
The JLS would be a good place to start. Keep in mind the memory model changed drastically for the better in the latest edition, which applies to 1.5+.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15092

The JLS is the Java Language Specification, in case that wasn't clear.

Also the Java Virtual Machine Specification might be of interest.
[ July 13, 2006: Message edited by: Jesper Young ]

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
I agree. Here's the link: http://aspose.com/file-tools
subject: Reference variable access
It's not a secret anymore!