what is difference between length and length()?

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


Please Help Members By Posting Answers For Below Questions

what is the purpose of "virtual"?

562


What is the properties class in java programming?

540


What is the functionality of the stub?

565


What is a byte string?

611


What is 32 bit float?

551






Explain access specifiers?

653


What is default locale java?

530


Which object oriented concept is achieved by using overloading and overriding?

545


What causes memory leaks in java?

483


Why is a constant variable important?

491


What happens if an exception is not handled in a program?

620


What is the purpose of assert keyword used in jdk1.4.x?

554


Does string is thread-safe in java?

584


What is constructor in java ?

627


What is maximum size of arraylist in java?

486