{"id":89732,"date":"2024-07-08T18:00:12","date_gmt":"2024-07-08T12:30:12","guid":{"rendered":"https:\/\/techvidvan.com\/tutorials\/?p=89732"},"modified":"2024-07-08T12:47:12","modified_gmt":"2024-07-08T07:17:12","slug":"java-string-isempty-method","status":"publish","type":"post","link":"https:\/\/techvidvan.com\/tutorials\/java-string-isempty-method\/","title":{"rendered":"Java String isEmpty() Method with Examples"},"content":{"rendered":"<p>The &#8220;isEmpty&#8221; function is a method commonly used in programming languages to check whether a string is empty or not. It returns a boolean value, typically &#8220;true&#8221; if the string has no characters (i.e., it is empty) and &#8220;false&#8221; if the string contains one or more characters. This function is useful for validating user input, handling empty string cases, or performing conditional operations based on the presence or absence of characters in a string.<\/p>\n<h2>String isempty<\/h2>\n<p>IsEmpty() String system checks whether a String is empty or not. This system returns true if the given string is empty. Differently, it returns false. The isEmpty() system of the String class has been included in Java string since JDK1.6. In other words, you can say that this system returns true if the length of the string is 0.<\/p>\n<p><strong>Parameters<\/strong><\/p>\n<p><strong>None.<\/strong><\/p>\n<h3>Technical Details<\/h3>\n<p><strong>Returns:<\/strong> A boolean value:<\/p>\n<ul>\n<li><strong>true &#8211;<\/strong> The string is empty (length() is 0)<\/li>\n<li><strong>false &#8211;<\/strong> The string is not empty<\/li>\n<\/ul>\n<h4>Signature<\/h4>\n<p>The signature or syntax of string isEmpty() method is given below:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">public boolean isEmpty()<\/pre>\n<h4>Returns<\/h4>\n<p>true if the length is 0; otherwise, false.<br \/>\nSince<br \/>\n1.6<br \/>\nInternal implementation<\/p>\n<p><strong>Output:<\/strong><br \/>\ntrue<br \/>\nFalse<\/p>\n<h3>Java String isEmpty() Method<\/h3>\n<p><strong>Example 2<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">public class IsEmptyExample2 {  \r\npublic static void main(String[] args) {  \r\n        String s1=\"\";    \r\n        String s2=\"TechVidvan\";             \r\n        \r\n        if(s1.length()==0 || s1.isEmpty())  \r\n            System.out.println(\"String s1 is empty\");  \r\n        else System.out.println(\"s1\");        \r\n        if(s2.length()==0 || s2.isEmpty())  \r\n            System.out.println(\"String s2 is empty\");  \r\n        else System.out.println(s2);  \r\n    }  \r\n}\r\n<\/pre>\n<p><strong>Output:<\/strong><br \/>\nString s1 is empty<br \/>\nTechVidvan<\/p>\n<p><strong>Example 3<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">public class StringIsEmptyExample3   \r\n{  \r\n\/\/ main method  \r\npublic static void main(String argvs[])  \r\n{  \r\nString str = null;  \r\nif(str.isEmpty())  \r\n{  \r\nSystem.out.println(\"The string is null.\");  \r\n}  \r\nelse  \r\n{  \r\nSystem.out.println(\"The string is not null.\");  \r\n}  \r\n}  \r\n}<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p>Exception in thread &#8220;main&#8221; java.lang.NullPointerException<br \/>\nat StringIsEmptyExample3.main(StringIsEmptyExample3.java:7)<br \/>\nHere, we can use the == operator to check for the null strings.<\/p>\n<p><strong>Example 4<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">class StringIsEmptyExample4  \r\n{  \r\n\/\/ main method  \r\npublic static void main(String argvs[])  \r\n{  \r\nString str = null;  \r\nif(str == null)  \r\n{  \r\nSystem.out.println(\"The string is null.\");  \r\n}  \r\nelse  \r\n{  \r\nSystem.out.println(\"The string is not null.\");  \r\n}  \r\n}  \r\n}<\/pre>\n<p><strong>Output:<\/strong><br \/>\nThe string is null.<\/p>\n<h3>Blank Strings<\/h3>\n<p>In Java, a blank string usually refers to an empty string, which is a string that contains no characters. You can create an empty string in Java in several ways:<\/p>\n<p><strong>Using double quotes with no characters inside:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">java\r\nString blankString = \"\";<\/pre>\n<p><strong>Using the `String` class constructor with no arguments:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">java\r\nString blankString = new String();\r\n<\/pre>\n<p>Both of these methods will create a string variable `blankString` that contains no characters and is considered empty or blank.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">public class StringIsEmptyExample5   \r\n{  \r\n\/\/ main method  \r\npublic static void main(String argvs[])  \r\n{  \r\n\/\/ a blank string  \r\nString str = \"     \";  \r\nint size = str.length();  \r\n\/\/ trim the white spaces and after that  \r\n\/\/ if the string results in the empty string   \r\n\/\/ then the string is blank; otherwise, not.  \r\nif(size == 0)  \r\n{  \r\nSystem.out.println(\"The string is empty. \\n\");  \r\n}  \r\nelse if(size &gt; 0 &amp;&amp; str.trim().isEmpty())  \r\n{  \r\nSystem.out.println(\"The string is blank. \\n\");  \r\n}  \r\nelse  \r\n{  \r\nSystem.out.println(\"The string is not blank. \\n\");  \r\n}   \r\nstr = \" Welcome to TechVidvan.  \";  \r\nsize = str.length();  \r\nif(size == 0)  \r\n{  \r\nSystem.out.println(\"The string is empty. \\n\");  \r\n}  \r\nif(size &gt; 0 &amp;&amp; str.trim().isEmpty())  \r\n{  \r\nSystem.out.println(\"The string is blank. \\n\");  \r\n}  \r\nelse  \r\n{  \r\nSystem.out.println(\"The string is not blank. \\n\");  \r\n}  \r\n}  \r\n}<\/pre>\n<p><strong>Output:<\/strong><br \/>\nThe string is blank.<br \/>\nThe string is not blank.<\/p>\n<h3>Conclusion<\/h3>\n<p>In conclusion, the isEmpty() method in Java is a powerful tool that allows developers to efficiently check if a string is empty or null, simplifying the process of handling strings in programming. By leveraging this method, developers can write cleaner and more readable code, enhancing the overall quality and performance of their Java applications.<\/p>\n<p>Understanding how to use isEmpty() effectively can significantly improve the robustness of your programs, ensuring that they handle empty or null strings gracefully without encountering errors. As you continue your journey in Java programming, mastering this method will prove invaluable in creating reliable and user-friendly applications.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The &#8220;isEmpty&#8221; function is a method commonly used in programming languages to check whether a string is empty or not. It returns a boolean value, typically &#8220;true&#8221; if the string has no characters (i.e.,&#46;&#46;&#46;<\/p>\n","protected":false},"author":6,"featured_media":447405,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[183],"tags":[5656,296,284,327,250,286,287],"class_list":["post-89732","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java","tag-isempty-method","tag-java","tag-java-string-isempty-method","tag-java-tutorials","tag-learn-java","tag-string-isempty-method","tag-string-isempty-method-in-java"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Java String isEmpty() Method with Examples - TechVidvan<\/title>\n<meta name=\"description\" content=\"Java isEmpty() method is a powerful tool that allows developers to efficiently check if a string is empty or null, simplifying the process of handling strings in programming.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/techvidvan.com\/tutorials\/java-string-isempty-method\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Java String isEmpty() Method with Examples - TechVidvan\" \/>\n<meta property=\"og:description\" content=\"Java isEmpty() method is a powerful tool that allows developers to efficiently check if a string is empty or null, simplifying the process of handling strings in programming.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techvidvan.com\/tutorials\/java-string-isempty-method\/\" \/>\n<meta property=\"og:site_name\" content=\"TechVidvan\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/TechVidvan\/\" \/>\n<meta property=\"article:published_time\" content=\"2024-07-08T12:30:12+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/05\/java-String-isEmpty.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=\"@vidvantech\" \/>\n<meta name=\"twitter:site\" content=\"@vidvantech\" \/>\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=\"2 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Java String isEmpty() Method with Examples - TechVidvan","description":"Java isEmpty() method is a powerful tool that allows developers to efficiently check if a string is empty or null, simplifying the process of handling strings in programming.","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:\/\/techvidvan.com\/tutorials\/java-string-isempty-method\/","og_locale":"en_US","og_type":"article","og_title":"Java String isEmpty() Method with Examples - TechVidvan","og_description":"Java isEmpty() method is a powerful tool that allows developers to efficiently check if a string is empty or null, simplifying the process of handling strings in programming.","og_url":"https:\/\/techvidvan.com\/tutorials\/java-string-isempty-method\/","og_site_name":"TechVidvan","article_publisher":"https:\/\/www.facebook.com\/TechVidvan\/","article_published_time":"2024-07-08T12:30:12+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/05\/java-String-isEmpty.webp","type":"image\/webp"}],"author":"TechVidvan Team","twitter_card":"summary_large_image","twitter_creator":"@vidvantech","twitter_site":"@vidvantech","twitter_misc":{"Written by":"TechVidvan Team","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/techvidvan.com\/tutorials\/java-string-isempty-method\/#article","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-string-isempty-method\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/dde481bb412350cde1ed6e389bc0deaf"},"headline":"Java String isEmpty() Method with Examples","datePublished":"2024-07-08T12:30:12+00:00","mainEntityOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-string-isempty-method\/"},"wordCount":411,"commentCount":0,"publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-string-isempty-method\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/05\/java-String-isEmpty.webp","keywords":["isEmpty() method","java","java string isEmpty() method","java tutorials","Learn Java","string isEmpty() method","string isEmpty() method in java"],"articleSection":["Java Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techvidvan.com\/tutorials\/java-string-isempty-method\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techvidvan.com\/tutorials\/java-string-isempty-method\/","url":"https:\/\/techvidvan.com\/tutorials\/java-string-isempty-method\/","name":"Java String isEmpty() Method with Examples - TechVidvan","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-string-isempty-method\/#primaryimage"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-string-isempty-method\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/05\/java-String-isEmpty.webp","datePublished":"2024-07-08T12:30:12+00:00","description":"Java isEmpty() method is a powerful tool that allows developers to efficiently check if a string is empty or null, simplifying the process of handling strings in programming.","breadcrumb":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-string-isempty-method\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techvidvan.com\/tutorials\/java-string-isempty-method\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/java-string-isempty-method\/#primaryimage","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/05\/java-String-isEmpty.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/05\/java-String-isEmpty.webp","width":1200,"height":628,"caption":"java string isempty()"},{"@type":"BreadcrumbList","@id":"https:\/\/techvidvan.com\/tutorials\/java-string-isempty-method\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techvidvan.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"Java String isEmpty() Method with Examples"}]},{"@type":"WebSite","@id":"https:\/\/techvidvan.com\/tutorials\/#website","url":"https:\/\/techvidvan.com\/tutorials\/","name":"TechVidvan Blogs","description":"","publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/techvidvan.com\/tutorials\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/techvidvan.com\/tutorials\/#organization","name":"TechVidvan","url":"https:\/\/techvidvan.com\/tutorials\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","width":200,"height":50,"caption":"TechVidvan"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/TechVidvan\/","https:\/\/x.com\/vidvantech"]},{"@type":"Person","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/dde481bb412350cde1ed6e389bc0deaf","name":"TechVidvan Team"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/89732","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/comments?post=89732"}],"version-history":[{"count":4,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/89732\/revisions"}],"predecessor-version":[{"id":447530,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/89732\/revisions\/447530"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media\/447405"}],"wp:attachment":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media?parent=89732"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/categories?post=89732"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/tags?post=89732"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}