return values of multiple datatype

Discussion in 'Java' started by Mike, Jun 5, 2007.

  1. Mike

    Mike Guest

    Hi

    I am a newbie in java.
    By using return in function, we can return a value of some variable or
    an array of some special data type.
    I am wondering if there are multiple datatype, e.g. String and int,
    can I return simultaneously both of them?
    Could we declare an array with different datatype?

    Thank you.

    Mike
     
    Mike, Jun 5, 2007
    #1
    1. Advertising

  2. Mike

    phi Guest

    Mike schrieb:
    > Hi
    >
    > I am a newbie in java.
    > By using return in function, we can return a value of some variable or
    > an array of some special data type.
    > I am wondering if there are multiple datatype, e.g. String and int,
    > can I return simultaneously both of them?
    > Could we declare an array with different datatype?
    >
    > Thank you.
    >
    > Mike
    >


    Hi

    First Question
    ****************

    No, you can not return two types simultaneously. If you want to return
    an int and a String simultaneously, you will have to generate a class
    (type):

    eg:
    public class IntStr { int i; String s;}

    your funcion might look like this

    public IntStr xyz() {
    IntStr result = new IntStr();
    result.i = 42;
    result.s = "Hello Universe";
    return result; }



    Second question:
    *******************

    No as well: an array has all its values the same type. You may specify

    Object myArray[];

    and then assign any type (Object) to your array. Because every Type is a
    subclass of "java.lang.Object" this will work.
    But this is probably not what you was looking for ?

    phi
     
    phi, Jun 5, 2007
    #2
    1. Advertising

  3. Mike

    Twisted Guest

    On Jun 5, 1:54 am, phi <> wrote:
    > public IntStr xyz() {
    > IntStr result = new IntStr();
    > result.i = 42;
    > result.s = "Hello Universe";
    > return result; }


    Personally, I'd make IntStr have a suitable constructor so that the
    above method body can be written in one line:

    return new IntStr(42, "Hello Universe");

    And I'd give it a better name.

    When you want different kinds of things in an array, usually you are
    either really looking to put a common supertype they're specialized
    from in the array, or you're looking to have a fixed-size bunch of
    different things used differently, which is better implemented as
    instances of a class with a bunch of fields.

    There are also other ways to return multiple things from a function.

    In an either/or case you can (but it's evil) make all but one of them
    a exception types, or objects wrapped in a exceptions, and throw them.
    Sort them out in the caller with a try and bunch of catch clauses.

    Since that's evil, you can also use a "union" as if from C, but it
    will take the form of a Java class with a bunch of different fields, a
    constructor for each field that sets it to a parameter and the others
    to null, and methods to query the fields. The caller finds the non-
    null to find out what type was in it.

    Another possibility is mutating fields in an object, or the members of
    a collection or array, passed as a parameter to the function. This can
    lead to unclear code if it isn't carefully documented that the method
    modifies the parameter, or if it's done cack-handedly enough. This is
    useful if the function has a "return value" that is used immediately
    when it returns (just return this) and also needs to communicate
    something out-of-band (this uses the mutable collection or whatever).
    The Builder pattern also passes as parameters objects that get
    modified, sometimes, with methods that perform construction steps on a
    data structure.

    On the other hand, if a group of values is the logical return value of
    the function, rather than a single value, then the function should
    return a composite of some kind. An array, collection, or a custom
    class with named fields for the components. It may be there's a design
    problem whereby the method is trying to do multiple responsibilities
    and should be divided and otherwise refactored, also; or it may be
    that a logical extension of the way things are developing is for the
    return type container object to become a "real" class with its own
    behavior beyond querying the fields (or just making them public
    final).

    Use your design intuition to decide which approach to use in each
    individual case where you find yourself wanting multiple returned
    objects or composite returns.
     
    Twisted, Jun 5, 2007
    #3
  4. Mike

    Mike Guest

    On Jun 5, 1:54 pm, phi <> wrote:
    > Mike schrieb:
    >
    > > Hi

    >
    > > I am a newbie in java.
    > > By using return in function, we can return a value of some variable or
    > > an array of some special data type.
    > > I am wondering if there are multiple datatype, e.g. String and int,
    > > can I return simultaneously both of them?
    > > Could we declare an array with different datatype?

    >
    > > Thank you.

    >
    > > Mike

    >
    > Hi
    >
    > First Question
    > ****************
    >
    > No, you can not return two types simultaneously. If you want to return
    > an int and a String simultaneously, you will have to generate a class
    > (type):
    >
    > eg:
    > public class IntStr { int i; String s;}
    >
    > your funcion might look like this
    >
    > public IntStr xyz() {
    > IntStr result = new IntStr();
    > result.i = 42;
    > result.s = "Hello Universe";
    > return result; }
    >
    > Second question:
    > *******************
    >
    > No as well: an array has all its values the same type. You may specify
    >
    > Object myArray[];
    >
    > and then assign any type (Object) to your array. Because every Type is a
    > subclass of "java.lang.Object" this will work.
    > But this is probably not what you was looking for ?
    >
    > phi


    thank you phi.
     
    Mike, Jun 5, 2007
    #4
  5. Mike

    Roedy Green Guest

    On Mon, 04 Jun 2007 19:48:31 -0700, Mike <> wrote,
    quoted or indirectly quoted someone who said :

    >I am a newbie in java.
    >By using return in function, we can return a value of some variable or
    >an array of some special data type.
    >I am wondering if there are multiple datatype, e.g. String and int,
    >can I return simultaneously both of them?
    >Could we declare an array with different datatype?


    There are two methods:

    1. the official way is to invent a new type with two fields a String
    and int with getters and setters. You return one of those. Sun does
    this with Point, Dimension and Location.

    2. the quick and dirty way:
    return new Object[] { theString, new Integer( theInt )};

    then you have cast to get the String back and unbox the Integer to get
    the int back out -- automatic in JDK 1.5+.
    --
    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
     
    Roedy Green, Jun 15, 2007
    #5
  6. Mike

    Roedy Green Guest

    On Mon, 04 Jun 2007 19:48:31 -0700, Mike <> wrote,
    quoted or indirectly quoted someone who said :

    >I am a newbie in java.
    >By using return in function, we can return a value of some variable or
    >an array of some special data type.
    >I am wondering if there are multiple datatype, e.g. String and int,
    >can I return simultaneously both of them?
    >Could we declare an array with different datatype?


    I have written two essays on this:

    http://mindprod.com/jgloss/return.html
    http://mindprod.com/jgloss/multiplereturn.html
    --
    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
     
    Roedy Green, Jun 15, 2007
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steve
    Replies:
    9
    Views:
    524
    Dietmar Kuehl
    Jan 3, 2005
  2. Dirk Hagemann

    Datatype of non-negative values

    Dirk Hagemann, Feb 9, 2005, in forum: Python
    Replies:
    2
    Views:
    322
    Dirk Hagemann
    Feb 10, 2005
  3. Greenhorn
    Replies:
    15
    Views:
    831
    Keith Thompson
    Mar 6, 2005
  4. Jeff
    Replies:
    2
    Views:
    1,154
    Steve C. Orr [MCSD, MVP, CSM, ASP Insider]
    Apr 16, 2007
  5. Chris Rebert
    Replies:
    1
    Views:
    683
    Bobby
    May 28, 2009
Loading...

Share This Page