Results 1 to 8 of 8
- 07-26-2011, 09:13 AM #1
Member
- Join Date
- Jun 2011
- Posts
- 59
- Rep Power
- 0
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.............
- 07-26-2011, 09:26 AM #2
Member
- Join Date
- Jul 2011
- Posts
- 43
- Rep Power
- 0
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"
- 07-26-2011, 09:28 AM #3
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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
This is similar(but not the same) to how an array would be made.Java Code:public class MyArray{ public int length; public MyArray(int length){ this.length = length; } }
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
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.Java Code:public class MyString{ private int length; private char[] chars; public int length(){ return length; } }
- 07-26-2011, 09:32 AM #4
Member
- Join Date
- Jun 2011
- Posts
- 59
- Rep Power
- 0
Thanks a lot............
now it clear to me.
Thanks
- 07-26-2011, 09:33 AM #5
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
You are welcome, I'm glad you understand it now.
- 07-26-2011, 09:36 AM #6
Member
- Join Date
- Jun 2011
- Posts
- 59
- Rep Power
- 0
Thank u very much.........
- 07-27-2011, 05:52 PM #7
Senior Member
- Join Date
- Jan 2011
- Location
- Rizal Province, Philippiines
- Posts
- 167
- Rep Power
- 0
We can say that length is a public variable and length() is a public method.
- 07-28-2011, 12:46 AM #8
Similar Threads
-
help with length
By ama91 in forum New To JavaReplies: 15Last Post: 02-21-2011, 01:44 AM -
String.length?
By rizowski in forum New To JavaReplies: 7Last Post: 11-20-2010, 05:33 PM -
length and excepton
By javagwcc in forum New To JavaReplies: 5Last Post: 06-28-2010, 11:06 PM -
what does num.length method does?
By kris09 in forum New To JavaReplies: 1Last Post: 08-07-2008, 10:19 PM -
Help with method length
By toby in forum New To JavaReplies: 1Last Post: 07-25-2007, 08:29 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks