can be defined as a total number of Java Character
present in a string object. For example, the string length of
is 9 including the space character.
Methods used to obtain information about an object are known as accessor methods. One such accessor method is
method, which returns the number of characters contained in the
public static void main(String arg)
String place = "Hyderabad";
int length = place.length(); // LINE A
System.out.println("String Length of " + place + " is : " + length);
String welcome = "Welcome to Merit Campus";
for (int i = 0; i < welcome.length(); i++) // LINE B
String Length of Hyderabad is : 9
Welcome to Merit Campus
LINE A, string length of
"Hyderabad" is calculated by
length method is stored in
length variable and displayed.
for loop executes 23 times as the length of
"Welcome to Merit Campus" string object is 23 and displays every character from beginning to end.
THINGS TO TRY
length method for different strings such as
"15-08-1947 00:25:30" etc.,
length method in other control statements as used in
for loop at