Answer Posted / abel
length and length( )
A) length variable:
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.
int student[] = new int[5];
System.out.println(student.length); // prints 5
length() method:
It is a method defined in String class. It gives the number of characters present in the string.
String str = "hello";
System.out.println(str.length()); // prints 5
Notice that in the first example 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.
It is like comparing two strings with == and equals(). equals() is a method call which takes more time than executing == operator.
| Is This Answer Correct ? | 0 Yes | 0 No |
Post New Answer View All Answers
What is parsing in grammar?
What is an immutable class?
What is structure of java heap? What is perm gen space in heap?
What is the instance of an object?
What is arrays aslist in java?
What are the advantages of functions?
Explain wait() method of object class ?
Why do we use variables?
What are java packages? What's the significance of packages?
enlist some features of jdk.
Whats the difference between notify() and notifyall()?
What is the difference between I ++ and ++ I in java?
What about abstract classes in java?
What is treeset in java collection?
What is the use of flag?