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

Thread: Returning multiple values from a method.

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    15
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Question Returning multiple values from a method.

    Hi there!!

    Just wanted to ask please if this is applicable to return multiple values from a method in java as it is in php's functions for instance.

    Thanks in advance!!

    Atar.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Returning multiple values from a method.

    Quote Originally Posted by atar View Post
    Hi there!!

    Just wanted to ask please if this is applicable to return multiple values from a method in java as it is in php's functions for instance.

    Thanks in advance!!

    Atar.
    How would you do this in php? As far as I know, and unless the php docs are out of date, you cannot
    PHP: Returning values - Manual
    A function can not return multiple values
    In java you could effectively do this just as you would in php - by wrapping the values in something else, for instance an array, a Collection, or wrap the values within a particular object.

  3. #3
    Junior Member
    Join Date
    May 2012
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Please elaborate...

    What are you trying to do? I'm sure there is a way, I just don't know what you're asking.

  4. #4
    Junior Member
    Join Date
    Apr 2012
    Posts
    15
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Returning multiple values from a method.

    @copeg

    Thank you about your quick response!!

    Can you post here a little example about how to do so with arrays, collections that you spoke about?

    Your help is appreciated!!

    Atar.
    Last edited by atar; July 31st, 2012 at 09:56 AM.

  5. #5
    Member
    Join Date
    Jul 2012
    Posts
    90
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Re: Returning multiple values from a method.

    What copeg said is something like this.Imagine that you want to return 5 values,e.g. 0,1,2,3,4 then you would write something like this

    static void f(int[] a)
        {
            for(int i=0 ; i<a.length ; i++)
                a[i]=i;
        }
        public static void main(String[] args) {
            int []a = new int[5];
            f(a);
        }

    If you do not understand something ask

  6. #6
    Junior Member
    Join Date
    Apr 2012
    Posts
    15
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Returning multiple values from a method.

    @Samaras

    All right, but how should I return those multiple values? There's not any reference to that in your code.

  7. #7
    Member
    Join Date
    Jul 2012
    Posts
    90
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Re: Returning multiple values from a method.

    Now array a holds these values.So you have access to them via array a.Try writing in main after f(a)
    for (int i = 0; i < a.length; i++) {
                System.out.println(a[i]);
    This should output 0,1,2,3,4.So you have access in these values that were created inside function f.Isn't that what you wanted,or i understood wrongly? :/

  8. #8
    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: Returning multiple values from a method.

    @Samaras: that's not necessarily returning a value, you're simply modifying one. I know it's common practice in C, but there are a few caveats with this method.

    The first is you can't actually change what array you have, so the array you're passed is the array you have to modify.

    For example:

    public static void main(String[] args)
    {
        int[] a = new int[5];
        doIt(a);
        for(int i = 0; i < a.length; ++i)
        {
            System.out.println(a[i]);
        }
    }
     
    public static void doIt(int[] var)
    {
        var = new int[5];
        for(int i = 0; i < var.length; ++i)
        {
            var[i] = 3;
        }
    }

    When you run this code, you'll notice that the program prints out five 0's, not five 3's.

    The better method is to instantiate and return said array:

    public static void main(String[] args)
    {
        int[] a = doIt();
        for(int i = 0; i < a.length; ++i)
        {
            System.out.println(a[i]);
        }
    }
     
    public static int[] doIt()
    {
        var = new int[5];
        for(int i = 0; i < var.length; ++i)
        {
            var[i] = 3;
        }
        return var;
    }

    Note that this code still only returns one value: it's returning an array. However, an array is composed of multiple elements, which can be extracted.

    Now if you have to return a bunch of values who's type do not correlate, the best practice is to create a class which holds all of the necessary components. For example, take the following class:

    public class Customer
    {
        public String name;
        public int id;
        public double pay;
    }

    Using a similar method as above, you can create and instantiate a Customer object and return it. Again, technically you're only returning one value: a customer object. However, inside that object there is a name, id, and pay fields.

  9. #9
    Member
    Join Date
    Jul 2012
    Posts
    90
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Re: Returning multiple values from a method.

    Ok if you return the array or pass it as an argument does not make much difference at this point i would say.

    However the example you gave me shocked me a bit.If i comment this line var = new int[5]; ,then the output will be 3's and no 0's.If i uncomment it the output is going to be as you said.WHY?

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

    Default Re: Returning multiple values from a method.

    if you return the array or pass it as an argument does not make much difference
    What if the called method wants to set the size of the array it returns?
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Member
    Join Date
    Jul 2012
    Posts
    90
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Re: Returning multiple values from a method.

    .I run it and i will say that if passed as an argument the size won't change.If it is been returned the size is going to change.WHY?
    In java i have been told that the arguments are passed as a reference..Is that true?And somehow is this link to this somehow?
    Remember i am a BEGINNER˛

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

    Default Re: Returning multiple values from a method.

    If it is been returned the size is going to change.
    There might not be an array passed to the method. It all depends on what the method is supposed to do.
    I would change that to say the size of the returned array would be set by the called method.

    Args are passed by value.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Member
    Join Date
    Jul 2012
    Posts
    90
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Re: Returning multiple values from a method.

    ok i agree,but my question was not answered yet i think :/

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

    Default Re: Returning multiple values from a method.

    Start your own thread if you have questions. This one was started by atar.
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Member
    Join Date
    Jul 2012
    Posts
    90
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Re: Returning multiple values from a method.

    Quote Originally Posted by Norm View Post
    Start your own thread if you have questions. This one was started by atar.
    You are tottaly right!

Similar Threads

  1. Program returning wrong values.
    By cam25 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: June 11th, 2012, 11:59 PM
  2. Replies: 1
    Last Post: April 26th, 2012, 10:06 AM
  3. [SOLVED] Help with method returning a double
    By Mike_Chase in forum What's Wrong With My Code?
    Replies: 9
    Last Post: July 22nd, 2011, 01:09 AM
  4. Method returning 0 for everything
    By JJTierney in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 4th, 2010, 08:51 PM
  5. [SOLVED] Java Beginner: Help with methods and returning values (hailstone program)
    By alf in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 14th, 2010, 06:28 PM

Tags for this Thread