

{"id":126622,"date":"2024-07-18T18:00:08","date_gmt":"2024-07-18T12:30:08","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=126622"},"modified":"2026-05-18T12:12:30","modified_gmt":"2026-05-18T06:42:30","slug":"java-string-length-method","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/java-string-length-method\/","title":{"rendered":"Java String length() Method with Examples"},"content":{"rendered":"<p>The length() method is an important method in the Java String class that returns the number of characters present in a string. Since strings in Java are composed of Unicode characters, the length returned by the length() method is equal to the number of Unicode code units in the string.<\/p>\n<p>The String class in Java implements the CharSequence interface, which defines the length() method. So, all Java strings inherit the length() method from the CharSequence interface.<\/p>\n<h3>Returns and Internal Implementation of Java length() method<\/h3>\n<p>The length() method returns an int value, which is the number of Unicode code units in the string. This value ranges from 0 for an empty string to 2,147,483,647 for a string containing the maximum number of Unicode code units.<\/p>\n<p>Internally, the Java String class maintains a character array (char[]) to store the contents of the string. The length variable of this character array indicates the current length of the string. When length() is called on a string, it simply returns this length variable from the internal character array.<\/p>\n<p>The character array representation with the length variable allows Java strings to be mutable in nature, unlike strings in other languages, like Python, which are immutable.<\/p>\n<h3>Java String length() method with examples<\/h3>\n<p><strong>Example 1<\/strong><\/p>\n<p>In the program shown below, the length() method returns an integer value by counting the number of characters in the string.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">public class StringLengthExample {\r\n    public static void main(String[] args) {\r\n        String s1 = \"Hello\";\r\n        String s2 = \"World\";\r\n\r\n        int len1 = s1.length();\r\n        int len2 = s2.length();\r\n\r\n        System.out.println(\"Length of s1: \" + len1);\r\n        System.out.println(\"Length of s2: \" + len2);\r\n    }\r\n}<\/pre>\n<p><strong>Output:<\/strong><br \/>\n<strong>Length of s1:<\/strong> 5<br \/>\n<strong>Length of s2:<\/strong> 5<\/p>\n<p><strong>Example 2<\/strong><\/p>\n<p>Using the length() method to check if a string is empty or not. The count is equal to zero, which means the string is empty.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">public class CheckEmptyString {\r\n    public static void main(String[] args) {\r\n        String s = \"\";\r\n\r\n        if (s.length() == 0) {\r\n            System.out.println(\"String is empty\");\r\n        } else {\r\n            System.out.println(\"String is not empty\");\r\n        }\r\n    }\r\n}<\/pre>\n<p><strong>Output:<\/strong><br \/>\nString is empty<\/p>\n<p><strong>Example 3<\/strong><\/p>\n<p>A program for reversing a string using the length method in Java.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">public class ReverseStringUsingLength {\r\n    public static void main(String[] args) {\r\n        String s = \"Hello\";\r\n\r\n        \/\/ Reverse string using length\r\n        for (int i = s.length() - 1; i &gt;= 0; i--) {\r\n            System.out.print(s.charAt(i));\r\n        }\r\n    }\r\n}<\/pre>\n<p><strong>Output:<\/strong><br \/>\nolleH<\/p>\n<p><strong>Example 4<\/strong><\/p>\n<p>If the string contains a space inside the quotes, it is treated as a character, and it will count that space. Let\u2019s understand with an example.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">public class CountSpacesInString {\r\n    public static void main(String[] args) {\r\n        String s = \"This string has spaces\";\r\n        int spaces = 0;\r\n\r\n        for (int i = 0; i &lt; s.length(); i++) {\r\n            if (s.charAt(i) == ' ') {\r\n                spaces++;\r\n            }\r\n        }\r\n\r\n        System.out.println(\"Number of spaces: \" + spaces);\r\n    }\r\n}<\/pre>\n<p><strong>Output:<\/strong><br \/>\n<strong>Number of spaces:<\/strong> 3<\/p>\n<h3>Conclusion<\/h3>\n<p>In summary, the Java String length() method is a crucial feature for handling strings, providing a simple way to determine the number of characters (Unicode code units) in a string. Its implementation in the CharSequence interface ensures its availability for all Java strings. Internally, it efficiently retrieves the string&#8217;s length from the character array, allowing Java strings to be mutable.<\/p>\n<p>The practical examples presented in this article illustrate its versatility in tasks such as checking for empty strings, reversing strings, or counting character occurrences. Understanding and using the length() method is essential for Java developers, simplifying common string operations.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The length() method is an important method in the Java String class that returns the number of characters present in a string. Since strings in Java are composed of Unicode characters, the length returned&#46;&#46;&#46;<\/p>\n","protected":false},"author":86671,"featured_media":134331,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[32],"tags":[7345,31221,31217,31220,31078,8152,31223,31222,31219,31218],"class_list":["post-126622","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java","tag-java","tag-java-length-method","tag-java-string-length-method","tag-java-string-length-method-with-examples","tag-java-tutorials","tag-learn-java","tag-length-method","tag-length-method-in-java","tag-string-length-method","tag-string-length-method-in-java"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Java String length() Method with Examples - DataFlair<\/title>\n<meta name=\"description\" content=\"The Java String length() method is a crucial feature for handling strings. Let&#039;s study the String length() method with examples.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/java-string-length-method\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Java String length() Method with Examples - DataFlair\" \/>\n<meta property=\"og:description\" content=\"The Java String length() method is a crucial feature for handling strings. Let&#039;s study the String length() method with examples.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/java-string-length-method\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2024-07-18T12:30:08+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-05-18T06:42:30+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/Java-string-length.webp\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/webp\" \/>\n<meta name=\"author\" content=\"TechVidvan Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"TechVidvan Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Java String length() Method with Examples - DataFlair","description":"The Java String length() method is a crucial feature for handling strings. Let's study the String length() method with examples.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/data-flair.training\/blogs\/java-string-length-method\/","og_locale":"en_US","og_type":"article","og_title":"Java String length() Method with Examples - DataFlair","og_description":"The Java String length() method is a crucial feature for handling strings. Let's study the String length() method with examples.","og_url":"https:\/\/data-flair.training\/blogs\/java-string-length-method\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2024-07-18T12:30:08+00:00","article_modified_time":"2026-05-18T06:42:30+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/Java-string-length.webp","type":"image\/webp"}],"author":"TechVidvan Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"TechVidvan Team","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/java-string-length-method\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/java-string-length-method\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/0e594f928e31fc96628ac40f6ae74f49"},"headline":"Java String length() Method with Examples","datePublished":"2024-07-18T12:30:08+00:00","dateModified":"2026-05-18T06:42:30+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/java-string-length-method\/"},"wordCount":416,"commentCount":0,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/java-string-length-method\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/Java-string-length.webp","keywords":["Java","java length() method","java string length() method","java string length() method with examples","java tutorials","Learn Java","length() method","length() method in java","string length() method","string length() method in java"],"articleSection":["Java Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/java-string-length-method\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/java-string-length-method\/","url":"https:\/\/data-flair.training\/blogs\/java-string-length-method\/","name":"Java String length() Method with Examples - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/java-string-length-method\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/java-string-length-method\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/Java-string-length.webp","datePublished":"2024-07-18T12:30:08+00:00","dateModified":"2026-05-18T06:42:30+00:00","description":"The Java String length() method is a crucial feature for handling strings. Let's study the String length() method with examples.","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/java-string-length-method\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/java-string-length-method\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/java-string-length-method\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/Java-string-length.webp","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/Java-string-length.webp","width":1200,"height":628,"caption":"Java string length()"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/java-string-length-method\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"Java Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/java\/"},{"@type":"ListItem","position":3,"name":"Java String length() Method with Examples"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/0e594f928e31fc96628ac40f6ae74f49","name":"TechVidvan Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/c89190da3d4010c71ba476b618ab10fdc2335c82cdfa0ad5002d98d0f2473444?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/c89190da3d4010c71ba476b618ab10fdc2335c82cdfa0ad5002d98d0f2473444?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/c89190da3d4010c71ba476b618ab10fdc2335c82cdfa0ad5002d98d0f2473444?s=96&d=mm&r=g","caption":"TechVidvan Team"},"description":"TechVidvan Team provides high-quality content &amp; courses on AI, ML, Data Science, Data Engineering, Data Analytics, programming, Python, DSA, Android, Flutter, full stack web dev, MERN, and many latest technology.","url":"https:\/\/data-flair.training\/blogs\/author\/test001\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/126622","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/86671"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=126622"}],"version-history":[{"count":6,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/126622\/revisions"}],"predecessor-version":[{"id":148327,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/126622\/revisions\/148327"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/134331"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=126622"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=126622"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=126622"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}