Skip to Content

Java String Length Example

  1. /*
  2.   Java String Length Example
  3.   This example shows how to get a length of a given String object.
  4. */
  5.  
  6. public class StringLengthExample {
  7.  
  8.   public static void main(String[] args) {
  9.     //declare the String object
  10.     String str = "Hello World";
  11.    
  12.     //length() method of String returns the length of a String.
  13.     int length = str.length();
  14.     System.out.println("Length of a String is : " + length);
  15.   }
  16. }
  17.  
  18. /*
  19. Output of a program would be:
  20. Length of a String is : 11
  21. */