aspose file tools*
The moose likes Beginning Java and the fly likes Need help understanding the output Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Need help understanding the output" Watch "Need help understanding the output" New topic
Author

Need help understanding the output

Aurika Vegana
Greenhorn

Joined: May 24, 2012
Posts: 11

Hi,



I don`t get it why the output is 6.

Hope anyone can give me a hint to figure it out.

Thank you in advance
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8251
    
  23

Aurika Vegana wrote:Hope anyone can give me a hint to figure it out...

Hi. I've added indentation to your code and spaced it out a bit. See how much easier it is to read?
That's your first hint.

Second: The 'a' you ask about is the 'a' created in the previous line. The Foo constructor takes an int and a Foo.

So, now explain to me what happens when program gets to c.getV(). What will the method do?
More specifically:
  • what is the value of 'value' in c.getV()?
  • what is the value of 'other' in c.getV()?

  • Winston


    Isn't it funny how there's always time and money enough to do it WRONG?
    Articles by Winston can be found here
    Anayonkar Shivalkar
    Bartender

    Joined: Dec 08, 2010
    Posts: 1510
        
        5

    Hello Aurika Vegana,

    Welcome to CodeRanch!

    Its good to see that you've used code tags in your very first post. Also, its nice to ask for hints (instead of actual answers).

    Now, coming to your question.

    Aurika Vegana wrote:it seems like a never ending loop to me

    Please have a close look. Is it a loop at all?

    Aurika Vegana wrote:returns 3 right?

    Technically, it will create a new object of class Foo and reference 'a' will refer to that.

    Aurika Vegana wrote:what is "a" here actually? 3?

    Again, a is a reference to an object of class Foo.

    I would suggest to debug the code, and/or try to decode the algorithm/logic of getV method. What does it return? What does 'x * value' means? How a new Foo object is created (and how its members are initialized)?

    Once you get this, its not difficult to understand why the program prints that particular value.

    I hope this helps.


    Regards,
    Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
    fred rosenberger
    lowercase baba
    Bartender

    Joined: Oct 02, 2003
    Posts: 11480
        
      16

    Personally, I think that "System.out.println()" is your best friend when trying to figure out what code is doing. Stick them in all over the place. print out what variables are at different places. print out boolean conditions. print out what value and other is in each object after you create them. print out what 'other' is inside getV, print out what other.getV(); gives you when you call it...

    put some in, take some out, move around stuff...put it as the first and last line of each method to see exactly how many times each method is REALLY called...

    it's easy, it's powerful, it's fast, and it lets YOU figure out the code, which is infinitely more gratifying than having someone explain it to you...plus, you'll remember it better and learn how to debug code yourself. you won't always be able to ask someone else for help.


    There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
    Aurika Vegana
    Greenhorn

    Joined: May 24, 2012
    Posts: 11

    Thank you for your quick replies and patience =)

    I thought:
    First a new Foo class object is created with int = 3 and other= null, "a" refers to that.
    So than
    this
    public int getV() {
    int x = 1;
    if (other != null) {
    x = other.getV();
    }
    return x * value;

    will return 3?

    Than we move on to "b", with int=2 and other=a.

    Running through getV:
    if (other != null) {
    x = other.getV();
    }
    other is a, not null, so we have x= a.getV . In "a" Foo we have other = null, so return x * value ---> will be 2, as in "b" Foo value is 2.


    In "c" Foo we have int= 1, other= b.
    When going through getV:
    other is b, not null, so we have b.getV. In "b" Foo other = a , not null, so a.getV with other = null will return x *value will be 1, as in "c" value is 1.

    c.getV , with int=1, other= b :

    here I`m stucked a bit.


    I would suggest to debug the code, and/or try to decode the algorithm/logic of getV method. What does it return? What does 'x * value' means? How a new Foo object is created (and how its members are initialized)?


    How to debug/ decode with no extra software for it?

    @fred:
    I posted the code in browxy.com with System.out.println a, b etc., it returned a@1be2d65 (sthm. like this)
    Couldn`t get me further, so I posted my question here then.


    Anayonkar Shivalkar
    Bartender

    Joined: Dec 08, 2010
    Posts: 1510
        
        5

    Aurika Vegana wrote:I posted the code in browxy.com with System.out.println a, b etc., it returned a@1be2d65 (sthm. like this)

    You'll need to override toString method to get more readable output from 'println' method
    Farhat Am
    Greenhorn

    Joined: Jul 02, 2012
    Posts: 2
    The first call to getV() is made by object c where:
    other = b, value = 1 (from object c), x = b.getV()

    In b.getV():
    other = a, value = 2 (from object b), x = a.getV()


    In a.getV():
    other = null, value = 3 (from object a), x = 1
    so the value returned from a.getV() us 3 * 1 = 3

    3 will be received by b.getV() in x. There value = 2 and returned value is 3 so new value to be returned becomes 3 * 2 = 6

    6 will be received by c.getV() in x. There value = 1 and returned value is 6 so new value to be returned becomes 6 * 1 = 6

    Hope it helps
    Anayonkar Shivalkar
    Bartender

    Joined: Dec 08, 2010
    Posts: 1510
        
        5

    Hello F Am,

    Welcome to CodeRanch!

    Please UseRealName.

    Also, please note that this is NotACodeMill, and LetThemDoTheirOwnHomework
    Farhat Am
    Greenhorn

    Joined: Jul 02, 2012
    Posts: 2
    As I am new to this forum, may b I went too far in explaining the solution. I will be careful next time.
    Thanks
    Anayonkar Shivalkar
    Bartender

    Joined: Dec 08, 2010
    Posts: 1510
        
        5

    Farhat Am wrote:As I am new to this forum, may b I went too far in explaining the solution. I will be careful next time.
    Thanks

    That is fine. Just make sure that you go through FAQs before asking/answering questions.
    Aurika Vegana
    Greenhorn

    Joined: May 24, 2012
    Posts: 11

    Farhat Am,

    thank you very much for you detailed solution, now I understand it finally and can move on.

    ;))


    Aurika
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Need help understanding the output