Java String length() Method Last Updated : 23 Dec, 2024 Comments Improve Suggest changes Like Article Like Report String length() method in Java returns the length of the string. In this article, we will see how to find the length of a string using the String length() method.Example: Java class Geeks { public static void main(String[] args){ String s1 = "abcd"; System.out.println(s1.length()); String s2 = ""; System.out.println(s2.length()); String s3 = "a"; System.out.println(s3.length()); } } Output4 0 1 Important Points:The length() method returns the number of characters present in the string. Suitable for string objects but not for arrays. Can also be used for StringBuilder and StringBuffer classes. It is a public member method. Any object of the String class, StringBuilder class, and StringBuffer class can access the length() method using the ( . ) dot operator.public int length()Return Type: The return type of the length() method is int.Examples of Java String length() Method1. Use of length() method to find size of String Java public class Geeks { public static void main(String[] args){ // Here str is a string object String str = "GeeksforGeeks"; System.out.println(str.length()); } } OutputThe size of the String is 13 2. Compare the size of two Strings Java // whether the length of two strings is // equal or not using the length() method import java.io.*; class Geeks { public static void main(String[] args) { String s1 = "abc"; String s2 = "xyz"; // storing the length of both the // strings in int variables int len1 = s1.length(); int len2 = s2.length(); // checking whether the length of // the strings is equal or not if (len1 == len2) { System.out.println("The length of both the strings"+ " are equal and is "+ len1); } else { System.out.println("The length of both the strings"+ " are not equal"); } } } OutputThe length of both the strings are equal and is 3 Comment More info C code_r Follow Improve Article Tags : Java Java-String-Programs Explore Java BasicsIntroduction to Java4 min readJava Programming Basics9 min readJava Methods7 min readAccess Modifiers in Java6 min readArrays in Java9 min readJava Strings8 min readRegular Expressions in Java7 min readOOP & InterfacesClasses and Objects in Java10 min readAccess Modifiers in Java6 min readJava Constructors10 min readJava OOP(Object Oriented Programming) Concepts10 min readJava Packages7 min readJava Interface11 min readCollectionsCollections in Java12 min readCollections Class in Java13 min readCollection Interface in Java6 min readIterator in Java5 min readJava Comparator Interface6 min readException HandlingJava Exception Handling8 min readJava Try Catch Block4 min readJava final, finally and finalize4 min readChained Exceptions in Java3 min readNull Pointer Exception in Java5 min readException Handling with Method Overriding in Java4 min readJava AdvancedJava Multithreading Tutorial3 min readSynchronization in Java10 min readFile Handling in Java4 min readJava Method References9 min readJava 8 Stream Tutorial7 min readJava Networking15+ min readJDBC Tutorial5 min readJava Memory Management4 min readGarbage Collection in Java6 min readMemory Leaks in Java3 min readPractice JavaJava Interview Questions and Answers15+ min readJava Programs - Java Programming Examples8 min readJava Exercises - Basic to Advanced Java Practice Programs with Solutions5 min readJava Quiz | Level Up Your Java Skills1 min readTop 50 Java Project Ideas For Beginners and Advanced [Update 2025]15+ min read Like