Solved

Java code problem

Posted on 2013-02-01
18
318 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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 

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

Technology Partners: 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!

Question has a verified solution.

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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
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 “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Suggested Courses

627 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