

{"id":126051,"date":"2024-08-28T18:00:16","date_gmt":"2024-08-28T12:30:16","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=126051"},"modified":"2024-08-28T18:14:59","modified_gmt":"2024-08-28T12:44:59","slug":"java-string-equals-method","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/java-string-equals-method\/","title":{"rendered":"Java String equals() Method with Examples"},"content":{"rendered":"<p>The equals() method in Java is a fundamental operation when it comes to string comparison and equality checks.<\/p>\n<p>Understanding how this method functions is paramount in Java programming. It serves as a cornerstone for determining whether two strings are identical, character by character, in the exact same order. Present within the String class equals() meticulously overrides the equals() method in the Object class. Accepting a string parameter empowers developers to scrutinize a string&#8217;s equality with the current string object.<\/p>\n<p>By default, this method is case-sensitive, returning true only when the strings are a perfect match and false when they differ.<\/p>\n<p>This article delves into the equals() method, exploring its signature, internal workings, and practical examples that vividly illustrate the string comparison and equality verification process in the Java programming language.<\/p>\n<h2>Method Signature<\/h2>\n<p><strong>The method signature of Java equals() is:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">public boolean equals(Object anotherObject)<\/pre>\n<p>It accepts an Object type parameter called anotherObject, which is the string with which to compare the current string. The method returns a boolean value &#8211; true if the strings are equal or false if they are not.<\/p>\n<h3>Internal Implementation<\/h3>\n<p>Internally, the equals() method compares the characters of both strings. It checks if the parameter string and the current string have the same length. If yes, it then compares characters one by one from the start to the end of both strings.<\/p>\n<p>By default, the comparison is case-sensitive. If any character mismatches, the method returns false immediately. Only if all characters match does it return true.<\/p>\n<p>The equals() method properly handles different data types. If the parameter is not a string, it simply returns false.<\/p>\n<h3>Java String equals() Method Examples<\/h3>\n<p>Let&#8217;s look at a few examples to better understand the usage of equals().<\/p>\n<p><strong>Example 1:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">public class StringEqualityExample {\r\n\r\n  public static void main(String[] args) {\r\n\r\n    String str1 = \"Hello\";\r\n    String str2 = \"Hello\";\r\n    String str3 = \"World\";\r\n\r\n    System.out.println(str1.equals(str2)); \/\/ true\r\n    System.out.println(str1.equals(str3)); \/\/ false\r\n\r\n  }\r\n\r\n}<\/pre>\n<p><strong>Output:<\/strong><br \/>\ntrue<br \/>\nfalse<\/p>\n<p>Here we have declared three string variables str1, str2, and str3. We compare str1 and str2 using equals(), which prints true since both refer to the same string &#8220;Hello&#8221;.<\/p>\n<p>When str1 and str3 are compared, it prints false as the strings are not equal.<\/p>\n<p><strong>Example 2:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">public class CaseSensitiveComparisonExample {\r\n\r\n  public static void main(String[] args) {\r\n\r\n    String str1 = \"Hello\";\r\n    String str2 = \"hello\";\r\n\r\n    if (str1.equals(str2)) {\r\n      System.out.println(\"Strings are equal\");\r\n    } else {\r\n      System.out.println(\"Strings are not equal\"); \r\n    }\r\n\r\n  }\r\n\r\n}<\/pre>\n<p><strong>Output:<\/strong><br \/>\nStrings are not equal<\/p>\n<p>In this example, str1 and str2 contain the same text but with different cases.<\/p>\n<p>When compared using equals(), it prints &#8220;Strings are not equal&#8221; since the comparison is case-sensitive by default.<\/p>\n<p><strong>Example 3:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">import java.util.ArrayList;\r\n\r\npublic class ArrayListStringComparisonExample {\r\n\r\n  public static void main(String[] args) {\r\n\r\n    ArrayList&lt;String&gt; list = new ArrayList&lt;&gt;();\r\n    list.add(\"Hello\");\r\n    list.add(\"World\");\r\n\r\n    String str = \"Hello\";\r\n\r\n    if (list.contains(str)) {\r\n      System.out.println(str + \" found in ArrayList\");\r\n    } else {\r\n      System.out.println(str + \" not found in ArrayList\");\r\n    }\r\n\r\n  }\r\n\r\n}<\/pre>\n<p><strong>Output:<\/strong><br \/>\nHello found in ArrayList<\/p>\n<p>Here, we declare an ArrayList of strings and check if a given string str is present in the list using contains().<\/p>\n<p>Since the list contains &#8220;Hello&#8221;, it prints &#8220;Hello found in ArrayList&#8221;. The contains() method internally uses equals() to compare the elements.<\/p>\n<p><strong>Example 4:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">public class ObjectTypeComparisonExample {\r\n\r\n  public static void main(String[] args) {\r\n\r\n    String str1 = \"Hello\";\r\n\r\n    Integer num = 5;\r\n\r\n    System.out.println(str1.equals(num)); \/\/ false\r\n\r\n    String str2 = \"World\";\r\n\r\n    System.out.println(str1.equals(str2)); \/\/ false\r\n\r\n  }\r\n\r\n}<\/pre>\n<p><strong>Output:<\/strong><br \/>\nfalse<br \/>\nfalse<\/p>\n<p>This example demonstrates how equals() handles a different data type. When str1 is compared with integer num, it returns false.<\/p>\n<p>Similarly, when two different string objects, str1 and str2, are compared, equals() returns false.<\/p>\n<h3>Conclusion<\/h3>\n<p>In summary, Java&#8217;s equals() method serves as a reliable sentinel in the quest for string equality. It meticulously inspects strings character by character, rigorously adhering to a case-sensitive criterion, thereby affirming their identical nature.<\/p>\n<p>This article has provided a comprehensive view of the equals() method, elucidating its signature, inner workings, and practical applications through illustrative examples.<\/p>\n<p>Whether comparing strings, inspecting ArrayLists filled with strings, or exploring comparisons with other object types, the equals() method is an invaluable asset in your Java programming toolkit.<\/p>\n<p>By effectively harnessing this method, you can confidently and accurately determine the equality of strings, enriching your ability to craft robust and precise Java programs.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The equals() method in Java is a fundamental operation when it comes to string comparison and equality checks. Understanding how this method functions is paramount in Java programming. It serves as a cornerstone for&#46;&#46;&#46;<\/p>\n","protected":false},"author":86671,"featured_media":126073,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[32],"tags":[31164,31166,7345,31165,31160,31163,31078,8152,31162,31161],"class_list":["post-126051","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java","tag-equals-method","tag-equals-method-in-java","tag-java","tag-java-equals-method","tag-java-string-equals-method","tag-java-string-equals-method-with-examples","tag-java-tutorials","tag-learn-java","tag-string-equals-method","tag-string-equals-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 equals() Method with Examples - DataFlair<\/title>\n<meta name=\"description\" content=\"Java String equals() method is case-sensitive, returning a value of true only when the strings are a perfect match and false when they differ.\" \/>\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-equals-method\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Java String equals() Method with Examples - DataFlair\" \/>\n<meta property=\"og:description\" content=\"Java String equals() method is case-sensitive, returning a value of true only when the strings are a perfect match and false when they differ.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/java-string-equals-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-08-28T12:30:16+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-08-28T12:44:59+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/java-string-quals.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 equals() Method with Examples - DataFlair","description":"Java String equals() method is case-sensitive, returning a value of true only when the strings are a perfect match and false when they differ.","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-equals-method\/","og_locale":"en_US","og_type":"article","og_title":"Java String equals() Method with Examples - DataFlair","og_description":"Java String equals() method is case-sensitive, returning a value of true only when the strings are a perfect match and false when they differ.","og_url":"https:\/\/data-flair.training\/blogs\/java-string-equals-method\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2024-08-28T12:30:16+00:00","article_modified_time":"2024-08-28T12:44:59+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/java-string-quals.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-equals-method\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/java-string-equals-method\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/0e594f928e31fc96628ac40f6ae74f49"},"headline":"Java String equals() Method with Examples","datePublished":"2024-08-28T12:30:16+00:00","dateModified":"2024-08-28T12:44:59+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/java-string-equals-method\/"},"wordCount":571,"commentCount":0,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/java-string-equals-method\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/java-string-quals.webp","keywords":["equals() method","equals() method in java","Java","java equals() method","java string equals() method","java string equals() method with examples","java tutorials","Learn Java","string equals() method","string equals() method in java"],"articleSection":["Java Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/java-string-equals-method\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/java-string-equals-method\/","url":"https:\/\/data-flair.training\/blogs\/java-string-equals-method\/","name":"Java String equals() Method with Examples - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/java-string-equals-method\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/java-string-equals-method\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/java-string-quals.webp","datePublished":"2024-08-28T12:30:16+00:00","dateModified":"2024-08-28T12:44:59+00:00","description":"Java String equals() method is case-sensitive, returning a value of true only when the strings are a perfect match and false when they differ.","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/java-string-equals-method\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/java-string-equals-method\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/java-string-equals-method\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/java-string-quals.webp","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/java-string-quals.webp","width":1200,"height":628,"caption":"java-string-quals()"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/java-string-equals-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 equals() 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\/126051","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=126051"}],"version-history":[{"count":5,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/126051\/revisions"}],"predecessor-version":[{"id":143262,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/126051\/revisions\/143262"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/126073"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=126051"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=126051"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=126051"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}