Solved

Java code problem

Posted on 2013-02-01
18
316 Views
Last Modified: 2013-02-05
What is the problem with the following Java code?

private static int value = 0;

public static int getValue()
{
    if (value == 0)
    {
        value = calculateValue();
    }
    return value;
}

a) The calculateValue method can be executed more than once
b) The calculateValue method will never execute
c) The getValue method will always return 0
d) value can be garbage collected, and the call to getValue may return garbage data
0
Comment
Question by:javaagile
  • 8
  • 6
  • 4
18 Comments
 
LVL 37

Expert Comment

by:zzynx
ID: 38843275
We're not allowed/here to do your homework. Sorry.
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 38843334
Plus the answer is staring you in the face of course.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 38843345
>>Plus the answer is staring you in the face of course.
Well..., if you know the answer that's easily said of course.

A tip to learn to understand these kind of questions and their answers is: make your hands dirty. Open your java IDE of choice, put that code in it and run it in debug mode. You would be surprised how much you learn from that.
0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:javaagile
ID: 38843357
this is not homework. I need to understand the problem. Is this garbage collection issue or the calculateValue method can be executed more than once?
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 38843544
The answer is simply to ask yourself to look at the logic. Whilst I would agree with what zzynx says, you should also be able to deduce the answer from following through the logic in your head in simple cases like this, rather than rely on testing it by running the code - which is something you can reserve for cases when the algos are more complicated.
0
 

Author Comment

by:javaagile
ID: 38843589
I know that objects can be garbage collected when no one reference to them. Can a static int be garbage collected?

Also I see the problem with this code is that if calculateValue method is calculating the value as 0 then it will be unnecessary executed all the times. Is this correct? Should a better way to code this be as below as then calculateValue will be executed only once even if it returns value 0? (Assuming calculateValue returns int)

private static Integer value = null;

public static int getValue()
{
    if (value == null)
    {
        value = calculateValue();
    }
    return value;
}
0
 
LVL 37

Accepted Solution

by:
zzynx earned 500 total points
ID: 38843607
>> if calculateValue method is calculating the value as 0 then it will be unnecessary executed all the times. Is this correct?
Yes.

>> Should a better way to code this be as below
Think you're right
0
 

Author Comment

by:javaagile
ID: 38845441
Thanks for the response.

I know that objects can be garbage collected when no one reference to them.

Can primitive values be garbage collected? (like private int b = 0, private static int b = 0)
0
 
LVL 37

Assisted Solution

by:zzynx
zzynx earned 500 total points
ID: 38850130
>> Can primitive values be garbage collected?
Sure.
0
 

Author Closing Comment

by:javaagile
ID: 38850183
Thanks!
0
 
LVL 37

Expert Comment

by:zzynx
ID: 38850223
My pleasure.
Thanx 4 axxepting
0
 

Author Comment

by:javaagile
ID: 38850230
Just confirming the answer to below question should be option (a) ?


What is the problem with the following Java code?

private static int value = 0;

public static int getValue()
{
    if (value == 0)
    {
        value = calculateValue();
    }
    return value;
}

a) The calculateValue method can be executed more than once
b) The calculateValue method will never execute
c) The getValue method will always return 0
d) value can be garbage collected, and the call to getValue may return garbage data
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 38850377
Primitive values don't get gc'd() as they are not objects and can't be set to null.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 38850382
I'm not completely sure.

But as you said:
>> if calculateValue method is calculating the value as 0 then it will be unnecessary executed all the times.
So, that seems to lead to answer a)
0
 
LVL 37

Expert Comment

by:zzynx
ID: 38850538
>> Primitive values don't get gc'd() as they are not objects and can't be set to null.
I'm afraid krakatoa is right and I was (terribly) wrong.
I was thinking about primitive type variables inside eg. methods. But they are put on the stack when you enter the method and simply removed from it when you leave the method. But that is no garbage collection. (which is done on heap space objects)
0
 

Author Comment

by:javaagile
ID: 38850567
Thanks zzynx! and Thanks krakatoa!
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 38851787
>> I'm afraid krakatoa is right and I was (terribly) wrong.

I know the feeling.

There goes a man.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 38854011
I have never met a man so ignorant that I could not learn something from him.
- Galileo Galilei -
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Oracle SQL syntax check  without executing 6 75
Java exception bubble up 2 21
Is Applet the way to go for my drag and drop system? 8 25
java example issue 5 25
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question