aspose file tools*
The moose likes Java in General and the fly likes Returning more than one value? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Returning more than one value?" Watch "Returning more than one value?" New topic
Author

Returning more than one value?

Mike Lipay
Ranch Hand

Joined: Sep 11, 2007
Posts: 171
I need to return two values back from a method, an integer and a string. Is there a way to do this?
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

You could use an array of Object, or make a custom class which holds both the integer and the String.


[My Blog]
All roads lead to JavaRanch
Mike Lipay
Ranch Hand

Joined: Sep 11, 2007
Posts: 171
Ok, thanks. I thought it wasn't as simple as

return (int, String);
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

Mike Lipay wrote:Ok, thanks. I thought it wasn't as simple as

return (int, String);

Almost. return new Object[]{myInt,MyString};
Mike Lipay
Ranch Hand

Joined: Sep 11, 2007
Posts: 171
Wow! I didn't think that was possible. What does the other side of it look like:

new Object[] {intValue, StringValue} = myMethod();

Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18978
    
  40

Mike Lipay wrote:Wow! I didn't think that was possible. What does the other side of it look like:

new Object[] {intValue, StringValue} = myMethod();




Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61606
    
  67

Messy! I'd go with abstracting the values into a class or interface.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Yeah, encapsulate the return value... pulling it out of an object array seems to somewhat defeat the purpose of static typing, no?
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

Bear Bibeault wrote:Messy! I'd go with abstracting the values into a class or interface.

I left Mike the right to choose between both approaches
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19759
    
  20

Bear Bibeault wrote:Messy! I'd go with abstracting the values into a class or interface.

I fully agree. An almost* good example of such an abstraction is java.awt.Dimension - it is used to return both a width and height from methods like Component.getSize().


* public fields? Come on!


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Mike Lipay
Ranch Hand

Joined: Sep 11, 2007
Posts: 171
Christophe Verré wrote:
Bear Bibeault wrote:Messy! I'd go with abstracting the values into a class or interface.

I left Mike the right to choose between both approaches


And I appreciate it, so long as the method is easy to enact and understand I don't see the problem. It seems to me that Java itself is rather poor in not allowing the return of multiple values from a method. There are many ways to skin a cat, some prettier than others, but so long as you understand how it's done it doesn't mater which way you choose.

I know the method.get() exists and works (and I use it in other areas), I was looking for other ways to do it, and Christophe showed another possibility. Thanks.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

No, it *does* matter which one you choose, and using an object array circumvents type safety and makes your IDE that much less useful. Not a habit I'd encourage, personally.

Java is not unique in allowing only a single return value, and it's far from being Java's greatest shortcoming.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19759
    
  20

I can't name one single programming language that has multiple return types. Python appears to have this feature but in fact it doesn't - it uses a combination of returning a tuple and splitting (is this called slicing as well?). For instance, the following Python code creates a tuple variable around two variables, then assigns the returned tuple to this tuple, then dissolved the tuple again:
This is actually the same as this:
So no, no multiple return there.
Mike Lipay
Ranch Hand

Joined: Sep 11, 2007
Posts: 171
Rob Prime wrote:I can't name one single programming language that has multiple return types.

  • Cobol
  • Fortran
  • BAL
  • PL/I
  • RPG


  • Just because you don't know them doesn't mean they don't exist.
    Rob Spoor
    Sheriff

    Joined: Oct 27, 2005
    Posts: 19759
        
      20

    I never said they didn't exist. I just said I couldn't name any
    David Newton
    Author
    Rancher

    Joined: Sep 29, 2008
    Posts: 12617

    Rugby's the same way. (That'd be Ruby; thanks Droid autocomplete.)

    Re the litany of dead/dying languages: maybe there's a reason that more recent ones haven't followed suit? It's just not something found in most OO languages, since there are ways to deal with it. Java makes it a bit more troublesome than some, but the drawbacks of making ad-hoc data structures are the same as they've always been.
    Mike Lipay
    Ranch Hand

    Joined: Sep 11, 2007
    Posts: 171
    David Newton wrote:Re the litany of dead/dying languages: maybe there's a reason that more recent ones haven't followed suit?


    I've heard that diatribe since the 80's, in the mid- large- business world they are still going strong. I've done consulting at a number of companies that are giving up SQL-based servers because of their poor speed when compared to mainframes. Oracle has released a MF version of its' DB with Cobol interfaces. IBM now has a mainframe capable of GUI interfaces via Cobol, and the next release of the language will follow suit. Having been in the business since the 60's I've seen MF and PC based languages come and go; at this point there is no clear winner in the PC arena (Java is just another player in the field) and none is as widely used as Cobol.
    David Newton
    Author
    Rancher

    Joined: Sep 29, 2008
    Posts: 12617

    "Going strong" is a matter of definition, I guess. In 25 years I've seen nothing but strong decline, and having used half the languages you listed I'm grateful. YMMV.
    Mike Simmons
    Ranch Hand

    Joined: Mar 05, 2008
    Posts: 3018
        
      10
    Rob Prime wrote:I can't name one single programming language that has multiple return types. Python appears to have this feature but in fact it doesn't - it uses a combination of returning a tuple and splitting (is this called slicing as well?).

    I'm not sure I understand the distinction being made here. It seems to me that Python does allow the programmer to return multiple values from a function, and the tuple is the underlying mechanism that allows this to happen. In that sense, I would say that Perl, Python, Ruby, Groovy, Scala, and C# all allow returning multiple values. Scala and C# even do it while preserving static typing of each element. Am missing something? Is returning a tuple significantly different from returning multiple values?
    Rob Spoor
    Sheriff

    Joined: Oct 27, 2005
    Posts: 19759
        
      20

    Only semantically.
    manuel aldana
    Ranch Hand

    Joined: Dec 29, 2005
    Posts: 308
    this is typically the field of dynamic typed languages where there is no static typing and returning by map style is common and straightforward.

    for exampl in groovy i would not hesitate to do:


    in java on the other hand i would never return a Map<String,Object>. Java is static typed so I wouldn't try to bypass the static typed information by using Object for all variables and casting around later (apart from some reflection magic, which is sometimes necessary).

    For Java I would create a dedicated custom return class:


    aldana software engineering blog & .more
    Mike Simmons
    Ranch Hand

    Joined: Mar 05, 2008
    Posts: 3018
        
      10
    manuel aldana wrote:this is typically the field of dynamic typed languages where there is no static typing...

    Yup. And with Scala and .NET it seems this could become a common practice in modern statically-typed languages as well. Just not Java.

    The comment I was really responding to was:
    David Newton wrote:maybe there's a reason that more recent ones haven't followed suit?

    It seems that plenty of more recent languages have followed suit.
    David Newton
    Author
    Rancher

    Joined: Sep 29, 2008
    Posts: 12617

    Still semantics.

    AFAIK it's not on the JDK 7 roadmap at this point.
    Mike Simmons
    Ranch Hand

    Joined: Mar 05, 2008
    Posts: 3018
        
      10
    David Newton wrote:Still semantics.

    Can you elaborate? I'm not sure what you mean here.

    David Newton wrote:AFAIK it's not on the JDK 7 roadmap at this point.

    Agreed; I've heard no indication of any plans to include it in Java. Just a few isolated pundits calling for it.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Returning more than one value?