Results 1 to 8 of 8
Like Tree1Likes
  • 1 Post By sunde887

Thread: Difference between length() and length

  1. #1
    mitra is offline Member
    Join Date
    Jun 2011
    Posts
    59
    Rep Power
    0

    Default Difference between length() and length

    Hi guys.........

    public class Compile18 {
    public static void main(String args[]){
    String s = "super market";
    System.out.println(args.length);
    System.out.println(args[0]);
    System.out.println(args);
    System.out.println(s.length());
    }
    }

    this is command line program, we must take input from command line.
    So,
    at line 4 when we read the length of args array, we just write (args.lenght), and it shows correct output, but if try to read the length of the args array by (args.length()), it shows error,
    whereas at line 7 we must write (s.length()) to read the length of the string type variable.
    So I can't understand what is the difference between these two -- length and length() ???

    Thank u guys.............

  2. #2
    fakepics500 is offline Member
    Join Date
    Jul 2011
    Posts
    43
    Rep Power
    0

    Default

    length is the property of array

    it tells the length of a given array

    eg. int a[]=new int[5];

    a.length will give 5;

    length() is a methds defied in string libaray
    it tell the length of given string;
    String s="ram";
    s.length() will give 3

    String s[]={"one","two","three"};
    s.length will be 3 as there are 3 elements in s(String type array)
    but
    s[2].length() will be 5 as the length of string at postion 2 is 5 i.e "three"

  3. #3
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    length is an instance variable of type array. Array is an object, just like string, Object, JFrame, etc. The array class has an instance variable called length, when you say arrayName.length, you are getting the length of an array. You can picture it as looking like this
    Java Code:
    public class MyArray{
      public int length;
      public MyArray(int length){ this.length = length; }
    }
    This is similar(but not the same) to how an array would be made.

    When finding the length of a string, you use a method call which gets the length of the string. It can be seen to look like this
    Java Code:
    public class MyString{
      private int length;
      private char[] chars;
      public int length(){ return length; }
    }
    Notice that in the first example I made the length variable public, it can be accessed anywhere. In the second example it is private and cannot be accessed directly, so we provide a method which allows us to view the length variable.
    qwerty53 likes this.

  4. #4
    mitra is offline Member
    Join Date
    Jun 2011
    Posts
    59
    Rep Power
    0

    Default

    Thanks a lot............
    now it clear to me.
    Thanks

  5. #5
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    You are welcome, I'm glad you understand it now.

  6. #6
    mitra is offline Member
    Join Date
    Jun 2011
    Posts
    59
    Rep Power
    0

    Default

    Thank u very much.........

  7. #7
    RichersooN is offline Senior Member
    Join Date
    Jan 2011
    Location
    Rizal Province, Philippiines
    Posts
    167
    Rep Power
    0

    Default

    We can say that length is a public variable and length() is a public method.

  8. #8
    Junky's Avatar
    Junky is online now Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,784
    Rep Power
    7

Similar Threads

  1. help with length
    By ama91 in forum New To Java
    Replies: 15
    Last Post: 02-21-2011, 01:44 AM
  2. String.length?
    By rizowski in forum New To Java
    Replies: 7
    Last Post: 11-20-2010, 05:33 PM
  3. length and excepton
    By javagwcc in forum New To Java
    Replies: 5
    Last Post: 06-28-2010, 11:06 PM
  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

Tags for this Thread

Posting Permissions

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