Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Java code problem

Posted on 2013-02-01
18
Medium Priority
?
319 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 2000 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 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
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.
Suggested Courses

721 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