Get the Length of a String in Java

Java treats Strings as objects not as simple data types. This article will show you how to get the length of a string in Java.

Steps

  1. Check to see if the string is null. This is usually neglected and could cause trouble as the next call will trigger an exception.
  2. Use the length() instance method, available in every string to get an integer with the length of the string you called length() from.

Code Sample

Tips

  • For the String of length n, the index for characters of the string are 0, 1,..., n-1.

Related Articles