Results 1 to 9 of 9
  1. #1
    Zelaine is offline Senior Member
    Join Date
    Aug 2013
    Location
    Sweden
    Posts
    157
    Rep Power
    2

    Lightbulb Returning Values

    In C++ you could use pointers to return more than one value from a function/method. How does one do that in Java?

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,963
    Rep Power
    8

    Default Re: Returning Values

    Wrap whatever data you want in an Object and return an instance of that instead.

    For example, to return both an X and a Y value, you can return a Point instance.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Zelaine is offline Senior Member
    Join Date
    Aug 2013
    Location
    Sweden
    Posts
    157
    Rep Power
    2

    Default Re: Returning Values

    I don't really understand what you mean, since I'm not very familiar with Java terms yet. Object? Instance? Point instance? If you have time, could you please give me a code example? It would be really helpful :)

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,609
    Rep Power
    5

    Default Re: Returning Values

    Those terms are basic to understanding Java. I suggest you check out the Oracle tutorials in my signature below.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    Zelaine is offline Senior Member
    Join Date
    Aug 2013
    Location
    Sweden
    Posts
    157
    Rep Power
    2

    Default Re: Returning Values

    Well, I'm Swedish, so I know them in Swedish but not in English. So I just thought that I would understand it better if someone could give me a code example :)

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,609
    Rep Power
    5

    Default Re: Returning Values

    Any instantiation of a class is an object.

    E.g. Point point1 = new Point(); // point1 is an instance of Point
    Point point2 = new Point(); // point2 is an instance of Point

    Both point1 and point2 are also objects.

    In general:

    SomeClass someClass = new SomeClass();
    someClass is an instance of that class and also an object.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  7. #7
    Zelaine is offline Senior Member
    Join Date
    Aug 2013
    Location
    Sweden
    Posts
    157
    Rep Power
    2

    Default Re: Returning Values

    So basically one can't return more than one value from a method without objects?

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,609
    Rep Power
    5

    Default Re: Returning Values

    Correct! Unlike C and its variants, you can't pass pointers in the argument list and then reassign them. You could pass mutable "wrapper" object references and then assign a value to one or more fields. So you're changing the fields and not the object itself. Or even pass an empty array and fill it. But if you want to return a value via the return keyword you are limited to one object or primitive.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  9. #9
    Zelaine is offline Senior Member
    Join Date
    Aug 2013
    Location
    Sweden
    Posts
    157
    Rep Power
    2

    Default Re: Returning Values

    So I guess I will learn how to use objects then, thanks :)

Similar Threads

  1. Result set not returning values
    By karthikeyan_raju in forum Advanced Java
    Replies: 5
    Last Post: 04-14-2011, 05:24 AM
  2. Problem returning Array values Please help
    By drgnfire25 in forum New To Java
    Replies: 4
    Last Post: 01-12-2011, 12:53 AM
  3. Method Arrays's: Returning multiple values
    By noobForever in forum New To Java
    Replies: 6
    Last Post: 11-02-2010, 12:07 PM
  4. Returning HexaDecimal Values to an Array???
    By stylishkishore in forum Advanced Java
    Replies: 5
    Last Post: 12-18-2009, 08:04 AM
  5. problem on returning values..
    By kulangotski in forum Advanced Java
    Replies: 11
    Last Post: 03-07-2009, 02:13 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •