File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes True or False?...... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "True or False?......" Watch "True or False?......" New topic
Author

True or False?......

sonir shah
Ranch Hand

Joined: Nov 01, 2001
Posts: 435

The above code will always print "false".(TRUE OR FALSE).
I tried running the program and the output was :
"false"
"false"
But I feel that the statement "return x! = y" is true because 2 is not 1.(hence true).
Any Comments??
Sonir
Roy Ben Ami
Ranch Hand

Joined: Jan 13, 2002
Posts: 732
first u dont check the 1 and 2 , u check the x and y. check the question.
second, i think the answer is false meaning it wont always return false. sometimes it can return true!
the synchronized doesnt help me here does it?
cause it get synchronized on an object instance and we have 2 of those. and the variables are static. so it can happen that one thread finishes doing ++ on both x and y. and then the othe thread does x++ only and then the first thread checks to see if x and y are different and its true!
am i correct Val or Rob or Kumar?
btw its pretty late here now so excuse my typos.
i think ill check your replies tomorrow
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
What part of the world are you in Roy? I'm in California, so I think we're on opposite schedules...it's late for you but afternoon for me.
Anyway, the statement "The above code will always print false" is TRUE.
You're right in noting that there is a possibility that this class is not thread safe because the setXY() call itself is not synchronized. A thread could potentially call setXY while another thread was running the setY, thus changing the Y value; meanwhile the first thread gets the lock and then updates the X value, now the object is briefly in an inconsistent state.
However, in this example, TWO different thread objects are being created, so each has it's own copy of the X and Y variables. This particular example will always print false because each thread only updates its own object, not a shared object.
Rob


Rob
SCJP 1.4
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
hi sonir,
've a look at this post
its similar issue & little explanation. the point is exactly what Rob pointed out. u r having TWO DIFF threads which unfortunately r the objects u want to run method on and unfortunately u got confused with thread objects and shared objects between threads (on which we synch).
regards
maulin.
Seany Iris
Ranch Hand

Joined: Jan 08, 2002
Posts: 54

It will give the result:
true
true
I think this can help you understand your code.
Hope it can help.


help you means help me
Mark Nolan
Greenhorn

Joined: Jan 23, 2002
Posts: 19
Well it had me head scratching but a couple of System.out.println statements had me slapping my head for not noticing it before.
I believe the problem is caused because the values 1 and 2 are never assigned to the variables. The two set methods respectively ignore the variables passed into them and merely increment x and y. Because they are class/member variables they are initialised as a default of 0 for you. After the setX and setY have been called both have been incremented to 1. Hence x != y returns false as it's testing 1 != 1, upon the second thread running both variables become 2 so again it's 2 != 2.
I "think" that makes sense, but I'm always happy to be corrected.. better to be corrected and know than to go on forever doing it wrong
Roy Ben Ami
Ranch Hand

Joined: Jan 13, 2002
Posts: 732
hi Rob , thanks for answering.
im in Israel by the way so it is the other side
anyway, im still confused. i still think that answer to the statement "The above code will always print false" is FALSE and not true.
i undersatnd your explanation but the variables x and y are STATIC. so there wont be different x and y for each object. they will be sharing the same one.
am i wrong?
Mark Nolan
Greenhorn

Joined: Jan 23, 2002
Posts: 19
Hi,
as far as the code above is concerned I don't think what's output is effected by whether the x and y variables are declared as static.
The code example at the end of this post will let you check easily to see what happens when you change between static and non static variables for this code.
The code always prints false because the flow of code is as follows ( pickingup from the run method):
setXY(1, 2); is called which calls setX and setY respectively.
setX and setY however ignore any passed in parameters. They are created as class variables and so have a default initialised value of 0. When the setX and setY methods are called you get x++ and y++ run, which sets x and y both to 1 ( when the first thread is running, and assuming there's no clashes between the threads etc. )
testXY is then called. The crux of this is the boolean return value from the statement "x != y". At this stage this equates to "1 != 1" or "is 1 not equal to 1" which is false, as 1 is equal to 1.
This is probably more easily displayed in the form of an if statement:

I hope that makes more sense..
Roy Ben Ami
Ranch Hand

Joined: Jan 13, 2002
Posts: 732
actually it doesnt!
there are 2 threads in this case runnig as 2 distinict objects.
1 thread can do the ++ on both x and y and then before it gets to checking them the other thread do ++ only at x. then the first thread checks and see it is trye (x and y are differnt).
as for the test, it is not conclusive. u dont know how will the processor acts.
thats what i think at least, but i need Rob to confirm this as he disagreed with me at first (but i think he missed the static thing).
Mark Nolan
Greenhorn

Joined: Jan 23, 2002
Posts: 19
I agree that as the code stands it isn't threadsafe and you can't say with 100% surity what the output will actually be, as stated above it's going to be dependant on the operating system.
However I think the original question was based around why x != y returned false rather than true. I guess what you expect it to return depends on what your expecting x and y to be
Now I'm really not sure if I'm making sense
Roy Ben Ami
Ranch Hand

Joined: Jan 13, 2002
Posts: 732
lol
im not either !
but the original question was:
The above code will always print "false".(TRUE OR FALSE).
i say FALSE (because of the static thingie).
Mark Nolan
Greenhorn

Joined: Jan 23, 2002
Posts: 19
I agree 100% .. I think the answer to the original question is ..
There's no way to guarantee when the comparison x != y is run what values they hold due to the code not being thread safe.
( I think I might have shot off on a tangent earlier .. hehe sorry :roll: )
R K Singh
Ranch Hand

Joined: Oct 15, 2001
Posts: 5371
Originally posted by Roy Ben Ami:
first u dont check the 1 and 2 , u check the x and y. check the question.
second, i think the answer is false meaning it wont always return false. sometimes it can return true!
the synchronized doesnt help me here does it?
cause it get synchronized on an object instance and we have 2 of those. and the variables are static. so it can happen that one thread finishes doing ++ on both x and y. and then the othe thread does x++ only and then the first thread checks to see if x and y are different and its true!
am i correct Val or Rob or Kumar?
btw its pretty late here now so excuse my typos.
i think ill check your replies tomorrow

Yes Roy .. you are right ...
once I was very much confused with this
type of questions
you can not predict output.
CMIW


"Thanks to Indian media who has over the period of time swiped out intellectual taste from mass Indian population." - Chetan Parekh
Roy Ben Ami
Ranch Hand

Joined: Jan 13, 2002
Posts: 732
thanks ravish
for the post and the link
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: True or False?......