This week's book giveaway is in the Design forum.
We're giving away four copies of Building Microservices and have Sam Newman on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes What should I know about volatile for the exam ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Building Microservices this week in the Design forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "What should I know about volatile for the exam ?" Watch "What should I know about volatile for the exam ?" New topic

What should I know about volatile for the exam ?

Edisandro Bessa
Ranch Hand

Joined: Jan 19, 2006
Posts: 584
Hi guys,

Until now, I was not worried about how volatile works in details until I see the following question in K&B's Master Exam #2.

According to K&B everything we need to know for the exam about volatile is that volatile can be applied only for variables. Not for methods or classes.

So my question is : What should I really know about volatile for the exam ?

Question :

Which are true ? (Choose all that apply)

a) To implement, a class must implement two methods.

b) When an object is serialized, if it has references to other objects, those objects must be serializable.

c) A serialized object can be deserialized only by the same JVM which was used to serialize it.

d) The value in instance variables market volatile will not survive serialization and deserialization.

The correct answer is B, but I think B is not correct because if an object supposed to be serialized has references to other objects, such references may be marked as transient. So, such references must NOT be serializable.

May be I mislead this question. Can anyone please explain ?

"If someone asks you to do something you don't know how to, don't tell I don't know, tell I can learn instead." - Myself
Arno Reper
Ranch Hand

Joined: Mar 14, 2006
Posts: 286
hi ( me again lol),
normally you should only know 'volatile' is for instance variable...i passed scjp and i didn't have any problem with volatile.


The man who makes no mistakes does not usually make anything<br /> <br />>>> SCJP 5.0 >> SCJD B&S <<< In progress
Arno Reper
Ranch Hand

Joined: Mar 14, 2006
Posts: 286
ps : more explaination about volatile

it look like synchronized but for instance variable and not blocks...i think, haven't read all the articles
[ April 14, 2006: Message edited by: Arno Reper ]
Edwin Dalorzo
Ranch Hand

Joined: Dec 31, 2004
Posts: 961
Hi, Edisandro.

At first it looks like a question about serialization more than a questions about multithreading.

First, I share your assessment: saying that answer B is correct implies the supposition that none of those references are transient. So this looks like a wrong question to me.

The option d seems to be an answer intended to see if you confuse volatile with transient.

Now transient implies that field member will not be serialized. Volatile is related to multithreading.

Volatile is related to Java Memory Model, the threads are allowed to keep copies of their variables, if one thread modifies one this variables other threads may not realize of the change. Marking a variable as volatile implies that any reading or writing of the variable will be reconciled with the master copies of themselves.

Volatile also ensures that the reading and writing operations of the variables are done atomically. That means that two thread will not write the same variable concurrently, but one after the other.

This is very important for variables bigger than 32 bits, like long or double, because they would be treated as two reads or writes of 32 bits. With volatile, 64-bit writes and reads will be enforced.

Another important issue related to volatile variables is that fact that some processors may execute the sentences in different order than that expected by the developer, as long as it does not alter the final result. Many modern processors can do this to optimize code, however, it may have implications on multithreading. Volatile enforces that the operations will be executed as programmed.

Finally, it is important to understand that, although a single read and single write will be enforced to be atomic operations , a combination of both won't. Hence incrementing a variable (i++) implies reading and writing the variable. After the reading the variable could be incremented by another thread before this thread increments it. This kind of behavior has to be enforced with Synchronization or with the atomic variables now present in Tiger.
[ April 14, 2006: Message edited by: Edwin Dalorzo ]
Edisandro Bessa
Ranch Hand

Joined: Jan 19, 2006
Posts: 584
Wow Edwin ! Great explanation.

Thank you for your prompt replies and for making efforts to clarify this question for me as much as you can.

Thanks Arno,
I've noticed your full participation here. Thank you very much.

It's always good to know that once you put your doubt at JavaRanch, in a few minutes you have the right answers.
[ April 14, 2006: Message edited by: Edisandro Bessa ]
I agree. Here's the link:
subject: What should I know about volatile for the exam ?
It's not a secret anymore!