Results 1 to 18 of 18
  1. #1
    rakeshkool27 is offline Member
    Join Date
    Nov 2013
    Posts
    12
    Rep Power
    0

    Default Confusion about using length method

    Hello, I am recently trying to learn Java and now learning the String part of Java. I have noticed the way of using length method of object is different for String object and an array of String. In case of pure Strings the "length" is invoked as "str.length()" but in case of array of Strings it is invoked as "str.length". I guess length is a method and if it is so, why the braces "()" are omitted in case of array of Strings.

    The case may be seen in this code:

    Java Code:
    class CmdLineDemo{
    	public static void main(String args[]){
    		//command line arguments
    		System.out.println("There are "+ args.length +" command line arguments");
    		System.out.println("The arguments are:	");
    		for(int i=0;i<args.length;i++)
    		{
    			System.out.print(args[i]+"\t");
    		}
    		
    		//array of strings
    		String strs[] = { "This", "is", "a", "test." };
    		System.out.println("\n\nArray of String: ");
    		for(String s : strs)
    		System.out.print(s + " ");
    		System.out.println("\n");
    		System.out.println("The length of the array is: " + strs.length);
    		
    		//pure Strings
    		String str1 = new String("Java is a powerful programming language.");
    		String str2 = "Java is object oriented.";
    		String str3 = new String(str1);
    		
    		System.out.println("\n\n"+str1);
    		System.out.println(str2);
    		System.out.println(str3);
    		
    		System.out.println("Length of str1 is "+str1.length());
    		System.out.println("Length of str2 is "+str2.length());
    		System.out.println("Length of str3 is "+str3.length());
    	}
    }

  2. #2
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    946
    Rep Power
    2

    Default Re: Confusion about using length method

    String.length() is a method where strs[].length is a public field, at least that's what the JLS says:
    The public final field length, which contains the number of components of the array. length may be positive or zero.
    (Source: Chapter*10.*Arrays)
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  3. #3
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default Re: Confusion about using length method

    It is NOT different however, as the first one is the length of the String and the second one is the length of the array that contains the Strings, not the length of the Strings themselves.

  4. #4
    rakeshkool27 is offline Member
    Join Date
    Nov 2013
    Posts
    12
    Rep Power
    0

    Default Re: Confusion about using length method

    Hmmm.....so length is an attribute in case of arrays and it is a method in case of Strings....Got it.

    Thanks. Java is fun.......

  5. #5
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    946
    Rep Power
    2

    Default Re: Confusion about using length method

    Yeah, and for Collection-related classes, there is size(). You'll get used to that ;)
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  6. #6
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,016
    Rep Power
    6

    Default Re: Confusion about using length method

    Quote Originally Posted by rakeshkool27 View Post
    Hmmm.....so length is an attribute in case of arrays and it is a method in case of Strings....Got it.
    If only there was a way to look that stuff up huh?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  7. #7
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,524
    Blog Entries
    7
    Rep Power
    20

    Default Re: Confusion about using length method

    The only reason for 'length' being an attribute for arrays (and being a method elsewhere) is that arrays don't have methods and classes don't have final attributes for the public while the should be alterable internally ... I personally find it a bit ugly language wise ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,016
    Rep Power
    6

    Default Re: Confusion about using length method

    Quote Originally Posted by JosAH View Post
    The only reason for 'length' being an attribute for arrays (and being a method elsewhere) is that arrays don't have methods and classes don't have final attributes for the public while the should be alterable internally ... I personally find it a bit ugly language wise ...

    kind regards,

    Jos
    What exactly? The length property? If so: I contribute that myself to Java being an older language already. At this point its not possible to turn the length property into a method because that breaks backwards compatibility.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  9. #9
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,524
    Blog Entries
    7
    Rep Power
    20

    Default Re: Confusion about using length method

    Quote Originally Posted by gimbal2 View Post
    What exactly? The length property? If so: I contribute that myself to Java being an older language already. At this point its not possible to turn the length property into a method because that breaks backwards compatibility.
    Having both a property and a method for conceptually the same thing; it's those arrays that are sort of stupid: they don't go well with generics either ... but they're 'understandable' to people new to the language and they're quite efficient ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,016
    Rep Power
    6

    Default Re: Confusion about using length method

    Right. Some of the earlier design decisions often frowned upon today were actually made to make the transition from C/C++ programming to Java easier if I remember correctly.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  11. #11
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,524
    Blog Entries
    7
    Rep Power
    20

    Default Re: Confusion about using length method

    Quote Originally Posted by gimbal2 View Post
    Right. Some of the earlier design decisions often frowned upon today were actually made to make the transition from C/C++ programming to Java easier if I remember correctly.
    Yep, I remember the same thing; and C++ wants to be backward compatible with C and C has a lot of bcpl remnants ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  12. #12
    rakeshkool27 is offline Member
    Join Date
    Nov 2013
    Posts
    12
    Rep Power
    0

    Default Re: Confusion about using length method

    Quote Originally Posted by JosAH View Post
    The only reason for 'length' being an attribute for arrays (and being a method elsewhere) is that arrays don't have methods and classes don't have final attributes for the public while the should be alterable internally ... I personally find it a bit ugly language wise ...

    kind regards,

    Jos
    Yup JosAH...I also find it a bit ugly and uncomfortable using the same term both as a method and a field...

    Have to mind it the next time using "length".

  13. #13
    rakeshkool27 is offline Member
    Join Date
    Nov 2013
    Posts
    12
    Rep Power
    0

    Default Re: Confusion about using length method

    Hmmm SurfMan...okay....not reached till those topics about collections-related classes...will be more intersting I guess

    Thanks anyway.

  14. #14
    rakeshkool27 is offline Member
    Join Date
    Nov 2013
    Posts
    12
    Rep Power
    0

    Default Re: Confusion about using length method

    Quote Originally Posted by gimbal2 View Post
    Right. Some of the earlier design decisions often frowned upon today were actually made to make the transition from C/C++ programming to Java easier if I remember correctly.
    Hmmm......

  15. #15
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,016
    Rep Power
    6

    Default Re: Confusion about using length method

    Quote Originally Posted by rakeshkool27 View Post
    Yup JosAH...I also find it a bit ugly and uncomfortable using the same term both as a method and a field...
    That's just because you're new to the language and platform. Once you memorize a bit more and you start to use the more advanced tooling, it all becomes a trivial matter. You'll run into plenty more things that you won't like; as I said, Java has been around for quite a while now and the standard API has collected plenty of unfortunate design choices over the years. There is little point in complaining about them now, they're here to stay. Unless Oracle one day decides that the next release of Java is allowed to completely and utterly destroy backwards compatibility.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  16. #16
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,524
    Blog Entries
    7
    Rep Power
    20

    Default Re: Confusion about using length method

    Blame it on the concept of arrays; they are not supposed to have methods and they can't be extended; nowadays they could use some of the non-inherited methods concept like in Enums; they could've implemented the length() method; but in the 'old' days, when Java was as slow as molassis, there were no Enums; basically, arrays are a pita; but they are so comfy to have around ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  17. #17
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,016
    Rep Power
    6

    Default Re: Confusion about using length method

    Quote Originally Posted by JosAH View Post
    when Java was as slow as molasses
    He he, that word always cracks me up :) For the longest time I thought them Duke boys were talking about mole asses.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  18. #18
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,524
    Blog Entries
    7
    Rep Power
    20

    Default Re: Confusion about using length method

    Quote Originally Posted by gimbal2 View Post
    He he, that word always cracks me up :) For the longest time I thought them Duke boys were talking about mole asses.
    The nicest saying I ever heard was (in Dutch, in that 'old The Hague accent'): "zo traag als paardenstront door 'n apothekers-trechter". ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Confusion about main method
    By JohnPringle83 in forum New To Java
    Replies: 9
    Last Post: 05-11-2011, 04:18 AM
  2. Replies: 8
    Last Post: 03-19-2009, 09:38 PM
  3. [SOLVED] calling a boolean method, confusion!!
    By AngrYkIdzrUlE in forum New To Java
    Replies: 18
    Last Post: 03-15-2009, 10:23 AM
  4. what does num.length method does?
    By kris09 in forum New To Java
    Replies: 1
    Last Post: 08-07-2008, 10:19 PM
  5. Help with method length
    By toby in forum New To Java
    Replies: 1
    Last Post: 07-25-2007, 08:29 PM

Posting Permissions

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