Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Unsolved Core Java Program

  1. #1
    Junior Member
    Join Date
    May 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Java Question

    public class AA {


    int add(int i) {
    int y = i;
    y += 20;
    System.out.println("*.y= " + y);

    if (y <= 100) {
    y +=30;
    System.out.println("**.y= " + y);
    add(y); // second call
    System.out.println("***.y= " + y);

    }else {System.out.println("*****.y= " + y);}

    System.out.println("Final Value of y : " + y);
    return y;
    }

    public static void main(String args[]) {
    AA obj = new AA();


    System.out.println("At Last x: " + obj.add(5));

    }

    }
    Please explain me the output


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Java Question

    Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for new members.

    Please post your code correctly per the above link.

    We are less likely to give you answers like "explain the output" than we are to discuss your explanation. You tell us what you think and why, and we'll gladly discuss your thoughts.

  3. #3
    Junior Member
    Join Date
    May 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Unsolved Core Java Program

    public class AA {


    int add(int i) {
    int y = i;
    y += 20;
    System.out.println("*.y= " + y);

    if (y <= 100) {
    y +=30;
    System.out.println("**.y= " + y);
    add(y); // second call
    System.out.println("***.y= " + y);

    }else {System.out.println("*****.y= " + y);}

    System.out.println("Final Value of y : " + y);
    return y;
    }

    public static void main(String args[]) {
    AA obj = new AA();


    System.out.println("At Last x: " + obj.add(5));

    }

    }

    Here I can't get why obj.add(5) returns 55 instead of 125. What's the role of return statement here. Please clear it .

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Unsolved Core Java Program

    Please post your code correctly and don't post duplicate threads.

    Threads merged.

Similar Threads

  1. question from the Core Java II book
    By willemjar in forum Java Theory & Questions
    Replies: 3
    Last Post: July 9th, 2013, 07:35 AM
  2. core java
    By abinaya.cs in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 30th, 2013, 04:10 AM
  3. Core Java
    By abinaya.cs in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 28th, 2013, 07:39 AM
  4. core java begineer
    By vinod in forum Object Oriented Programming
    Replies: 3
    Last Post: November 19th, 2011, 06:20 AM
  5. Replies: 1
    Last Post: June 4th, 2011, 11:22 AM