Solved

Java code problem

Posted on 2013-02-01
18
314 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
 

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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

760 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now