Solved

Java code problem

Posted on 2013-02-01
18
317 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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 

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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Java: The Public Class Main 4 66
web project error add remove 1 84
maven not picking latest jar instead picking old jar from .m2 12 74
netbeans can not run self signed app 8 37
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…
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 will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

739 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