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 19 of 19

Thread: Return statement.

  1. #1
    Member
    Join Date
    Mar 2010
    Posts
    271
    Thanks
    21
    Thanked 7 Times in 7 Posts

    Question Return statement.

    Hello everyone.

    I am working on an assignment involving the return statement. What I have to do is create a private variable called newSeconds and make methods to change it's value only if it is greater than sixty and less than one hundred.

    As you can see in the HR11Virus(Below) code I used the return and the System.out.println statements to show the value of the newSeconds variable. I originally used the return statement; that didn't print the value of the newSeconds variable so I used the System.out.println statement. It worked, so all is well.

    But it made me wonder why the return statement didn't work. And secondly what are the differences of the return and System.out.println statements. From what I know System.out.print does everything the return statement does and more, if so why is there the return statement? And if the return statement's job is the return the value of a variable, why didn't it return the value of newSeconds?

    public class HR11Virus {
        //Hour eleven, workshop one.
        private int newSeconds = 60;
     
        int getSeconds(int newValue){
            if (newValue > 60){
                newSeconds = newValue;
                System.out.println(newSeconds + " ");
            }
             return newSeconds;
        }
     
    }

    Here is the program I used to test it.

    class test {
        public static void main(String[] args) {
            HR11Virus virus = new HR11Virus();
            virus.getSeconds(78);
        }
    }

    Hope I was clear enough.

    Thanks.
    -Mel


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Return statement.

    The return statement returns values to be used in other code. println simply displays them to the console.

    ex.:
    // calculate x factorial
    public static int factorial(int x)
    {
        int result = 1;
        for(int i = 1; i <= x; ++i)
        {
            result *= i;
        }
        return result;
    }

    Now, say you had the following calculation you needed to make:

    y = 5!+3!

    If you simply used a println instead of a return statement, you wouldn't be able to internally use the actual computed value from the factorial method, but a return statement allows you to do that. If you want to display the resulting value, simply put the println statement in the code which calls the method.
    public static void main(String[] args)
    {
        int y = factorial(5) + factorial(3);
        System.out.println("y = " + y);
    }

  3. #3
    Member
    Join Date
    Mar 2010
    Posts
    271
    Thanks
    21
    Thanked 7 Times in 7 Posts

    Default Re: Return statement.

    So if I used a println statement and the method doesn't have a return statement, I wont be able to print anything?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Return statement.

    Try it and see what happens? Be sure to report back what happens.

  5. #5
    Member
    Join Date
    Mar 2010
    Posts
    271
    Thanks
    21
    Thanked 7 Times in 7 Posts

    Default Re: Return statement.

    class JavaObject {
     
        public static int factorial(int x) {
            int result = 1;
            for(int i = 1; i <= x; ++i) {
                result *= i;
            }
            return result;
        }
     
    }

    class JavaClass {
        public static void main(String[] args) {
     
            JavaObject test = new JavaObject();
     
            int y = test.factorial(5) + test.factorial(3);
            System.out.println("y = " + y);
     
        }
    }

    Output:
    y = 126
    BUILD SUCCESSFUL (total time: 1 second)


    Must be doing something wrong. :/

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Return statement.

    Must be doing something wrong
    Why is that? Did it work ok?

    For static methods, you don't need to create an object. You could reference your static methods this way:
    int y = JavaObject.factorial(5) + JavaObject.factorial(3);

    See the Math class which has many static methods.
    Last edited by Norm; June 16th, 2011 at 08:17 PM.

  7. #7
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Return statement.

    Why do you say that. Y = 126 is the correct output.

  8. #8
    Member
    Join Date
    Mar 2010
    Posts
    271
    Thanks
    21
    Thanked 7 Times in 7 Posts

    Default Re: Return statement.

    That actually is the output I got.

  9. #9
    Member
    Join Date
    Mar 2010
    Posts
    271
    Thanks
    21
    Thanked 7 Times in 7 Posts

    Default Re: Return statement.

    class JavaClass {
        public static void main(String[] args) {
     
            int y = JavaObject.factorial(5) + JavaObject.factorial(3);
            System.out.println("y = " + y);
     
        }
    }

    run:
    y = 126
    BUILD SUCCESSFUL (total time: 0 seconds)


    That is the output Netbeans shows.

  10. #10
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Return statement.

    So what is your problem? The factorial of 5 is 120. The factorial of 3 is 6. 120 + 6 = 126. The output is correct. If there is something you do not understand then ask a question. At the moment we have no idea what you are trying to say.

  11. #11
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: Return statement.

    Just to clarify, the 'return' statement allows a method to pass a value back to the code it was called from. The 'System.out.println' statement allows you to print a line of text to the console. The two statements have nothing in common.

  12. The Following User Says Thank You to dlorde For This Useful Post:

    Melawe (June 19th, 2011)

  13. #12
    Member
    Join Date
    Jun 2011
    Location
    Rhode Island
    Posts
    69
    My Mood
    Bored
    Thanks
    11
    Thanked 7 Times in 6 Posts

    Default Re: Return statement.

    Quote Originally Posted by Melawe View Post
    I am working on an assignment involving the return statement. What I have to do is create a private variable called newSeconds and make methods to change it's value only if it is greater than sixty and less than one hundred.

    As you can see in the HR11Virus(Below) code I used the return and the System.out.println statements to show the value of the newSeconds variable. I originally used the return statement; that didn't print the value of the newSeconds variable so I used the System.out.println statement. It worked, so all is well.

    But it made me wonder why the return statement didn't work. And secondly what are the differences of the return and System.out.println statements. From what I know System.out.print does everything the return statement does and more, if so why is there the return statement? And if the return statement's job is the return the value of a variable, why didn't it return the value of newSeconds?

    public class HR11Virus {
        //Hour eleven, workshop one.
        private int newSeconds = 60;
     
        int getSeconds(int newValue){
            if (newValue > 60){
                newSeconds = newValue;
                System.out.println(newSeconds + " ");
            }
             return newSeconds;
        }
     
    }

    Here is the program I used to test it.

    class test {
        public static void main(String[] args) {
            HR11Virus virus = new HR11Virus();
            virus.getSeconds(78);
        }
    }

    Hope I was clear enough.

    Thanks.
    -Mel
    getting Back on the topic, yes your code is fine for your return statement however, you need to do something with this section.
      virus.getSeconds(78);

    you need to set the returning variable to something:: like:
    int results = virus.getSeconds(78);
    System.out.println("my results are: " + results);

  14. #13
    Member
    Join Date
    Mar 2010
    Posts
    271
    Thanks
    21
    Thanked 7 Times in 7 Posts

    Default Re: Return statement.

    My bad misunderstood the code. I still don't understand what exactly return's job really is.

  15. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Return statement.

    One simple thing a return statement allows you to do in a program is to put a method call on the right hand side of an assignment statement.

    variable = aMethod(args); // here the method aMethod() returns a value that is assigned to variable.

    ...

    variabl2 = aMethod(moreArgs); // aMethod() called with different args can return a different value

  16. #15
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Return statement.

    methods are there to help abstract your code. They follow the mentality of the "black box": I give you something, you give me something back. How you go from what I give you to what you give me I don't care about, as long as it gets done. Additionally, anything else you use (local variables) to get from start to finish I don't care about, and can be junked at the end (i.e. go out of scope).

    Return statements are the mechanism by which methods give me back what I want (aka. it's "returning" something). Without them, your code can only work with side effects, which is a bad design mechanism because it's usually difficult to quickly determine what methods with side effects are suppose to do.

    Take the previous factorial code for example:

    I know that calling the factorial method will compute the factorial of a number x. How it gets done doesn't matter (it could be recursive, iterative, or some other fancy method), just as long as it gets done and I get the results. There are only two ways I can get back the results: side effects, or via return statements.

    So what would the factorial code look like if I used side effects?

    public class TestIt
    {
        public static int factorialResult;
     
        public static void factorial(int x)
        {
            factorialResult = 1;
            for(int i = 1; i <= x; ++i)
            {
                factorialResult *= i;
            }
        }

    Now what would me code using this method look like?

    public static void main(String[] args)
    {
        TestIt.factorial(5);
        int y = TestIt.factorialResult;
        TestIt.factorial(3);
        y += TestIt.factorialResult;
        System.out.println("5! + 3! = " + y);
    }

    That looks a lot messier than just using a simple return statement, especially when you start getting a lot of methods which all return various values or when you are trying to encapsulate data, not to mention the horrendous memory requirements of that design.

  17. The Following User Says Thank You to helloworld922 For This Useful Post:

    Melawe (June 19th, 2011)

  18. #16
    Member
    Join Date
    Mar 2010
    Posts
    271
    Thanks
    21
    Thanked 7 Times in 7 Posts

    Default Re: Return statement.

    So the return statement gives back a value but doesn't print it.
    Last edited by Melawe; June 19th, 2011 at 04:12 AM.

  19. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Return statement.

    There is no relationship between returning a value and printing a value. See post #11

  20. #18
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Return statement.

    Correct, printing is considered a "side effect".

  21. #19
    Member
    Join Date
    Mar 2010
    Posts
    271
    Thanks
    21
    Thanked 7 Times in 7 Posts

    Default Re: Return statement.

    Thanks everyone for taking the time to help!

Similar Threads

  1. Missing return statement
    By mrroberts2u in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 10th, 2011, 06:11 AM
  2. Missing return statement
    By Stn in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 1st, 2011, 08:03 PM
  3. what is wrong with my return statement??????
    By amr in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 13th, 2010, 07:55 PM
  4. Prime number generator program missing return statement
    By 03EVOAWD in forum What's Wrong With My Code?
    Replies: 13
    Last Post: September 10th, 2009, 09:17 AM
  5. Error of "Class has no return statement"
    By mdstrauss in forum What's Wrong With My Code?
    Replies: 7
    Last Post: August 2nd, 2009, 12:00 PM

Tags for this Thread