CODE
class StringLengthTest
{
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
{
System.out.print(welcome.charAt(i));
}
}
}
String Length of Hyderabad is : 9
Welcome to Merit Campus
At LINE A
, string length of "Hyderabad"
is calculated by length
method is stored in length
variable and displayed.
At LINE B
, 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.
length
method for different strings such as "12345"
, "Hello World"
, "Ramu\t\nRavi"
, "15-08-1947 00:25:30"
etc.,length
method in other control statements as used in for
loop at LINE B
.