Java String length() Method Last Updated : 23 Dec, 2024 Summarize Comments Improve Suggest changes Share 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 infoAdvertise with us Next Article Year length() method in Java with Examples C code_r Follow Improve Article Tags : Java Java-String-Programs Practice Tags : Java Similar Reads StringJoiner length() method in Java The length() of StringJoiner is used to find out the length of the StringJoiner in characters. It returns the length of the String representation of this StringJoiner. Note that if no add methods have been called, then the length of the String representation (either prefix + suffix or emptyValue) wi 2 min read Month length() method in Java The length() method is a built-in method of the Month ENUM which is used to get the number of days in this month instance. The number of days in a month can be 28, 30 or 31. Number of days in February in a leap year is 29. This method accepts a boolean flag variable which indicates whether this Year 1 min read Month maxLength() method in Java The maxLength() method is a built-in method of the Month ENUM which is used to get the maximum length of this month in number of days. For example, February can have both 28 days and 29 days depending on whether this year is a leap year or not. Therefore, this method will return 29 for February as m 1 min read YearMonth lengthOfMonth method() in Java The lengthOfMonth() method of YearMonth class in Java is used to return the length of month of this year-month object's value in number of days. The value of length of month is in the range of 1 to 31. This method also take cares of leap years. Syntax: public int lengthOfMonth() Parameter: This meth 2 min read Year length() method in Java with Examples The length() method of Year class in Java is used to return the length of this year object's value in number of days. There can be only two possible lengths of Year, 365(Non-leap years) and 366(Leap years). Syntax: public int length() Parameter: This method does not accepts any parameter. Return Val 1 min read LinkedList size() Method in Java The Java.util.LinkedList.size() method is used to get the size of the Linked list or the number of elements present in the linked list. Syntax: LinkedList.size()Parameters: This method does not take any parameter. Return Value: This method returns the size or the number of elements present in the Li 2 min read Like