Results 1 to 5 of 5
  1. #1
    Jack_Tauson_Sr is offline Senior Member
    Join Date
    Nov 2011
    Posts
    109
    Rep Power
    0

    Default What does this mean?

    " public static ArrayList<String> Consistency(String str) {
    ArrayList<String> result = new ArrayList<String>();

    // Some Code here

    return result;

    } "

    Why does the above method declaration has ArrayList<String> in the first line ("public static ArrayList<String> Consistency(String str)" ) ?

    What can't it be just like the following:

    " public static Consistency(String str) {
    ArrayList<String> result = new ArrayList<String>();

    // Some Code here

    return result;

    } "



    Please clarify !
    Last edited by Jack_Tauson_Sr; 03-16-2013 at 11:00 PM.

  2. #2
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    317
    Rep Power
    4

    Default Re: What does this mean?

    Hi Jack,

    If you are returning a value from a method you need to declare the return type within the method signature.

    Have a look at the following link for further information:
    Returning a Value from a Method (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

    Regards.

  3. #3
    Jack_Tauson_Sr is offline Senior Member
    Join Date
    Nov 2011
    Posts
    109
    Rep Power
    0

    Default Re: What does this mean?

    Hi Ronin,

    Thanks for your answer.

    Yes, I understand that it require a return type. But don't you think that defining "ArrayList<String>" as return type looks odd ? Can't we have something which could say that the method is returning the String instead of "ArrayList<String>" .

    Please clarify !


    Quote Originally Posted by Ronin View Post
    Hi Jack,

    If you are returning a value from a method you need to declare the return type within the method signature.

    Have a look at the following link for further information:
    Returning a Value from a Method (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

    Regards.

  4. #4
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    317
    Rep Power
    4

    Default Re: What does this mean?

    In the example above 'result' is of type ArrayList<String> then to declare a return type of String would cause an error.
    However, if you want to return a String, say a single element from the array, then you could use something like the following:
    Java Code:
    public static String Consistency(String str)
    { 
        ArrayList<String> result = new ArrayList<String>();
    
        // Some Code here
    
        return result.get(0);
    }
    And please enclose code in [code] tags as it makes it easier to read and reference.

    Regards.

  5. #5
    Jack_Tauson_Sr is offline Senior Member
    Join Date
    Nov 2011
    Posts
    109
    Rep Power
    0

    Default Re: What does this mean?

    Thanks for your answer Ronin. It's clear now. And yes, I'll enclose code in [code] tags from next time. I didn't know that as I'm new here. That really makes easy to read and reference.

    Quote Originally Posted by Ronin View Post
    In the example above 'result' is of type ArrayList<String> then to declare a return type of String would cause an error.
    However, if you want to return a String, say a single element from the array, then you could use something like the following:
    Java Code:
    public static String Consistency(String str)
    { 
        ArrayList<String> result = new ArrayList<String>();
    
        // Some Code here
    
        return result.get(0);
    }
    And please enclose code in [code] tags as it makes it easier to read and reference.

    Regards.

Posting Permissions

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