Title: Java Compare Strings Example - Java Code Geeks
Open Graph Title: Java Compare Strings Example - Java Code Geeks
Description: Interested to learn more about Java? Then check out our detailed example on Java Compare Strings!By comparing we mean to check if their values are equal.
Open Graph Description: Interested to learn more about Java? Then check out our detailed example on Java Compare Strings!By comparing we mean to check if their values are equal.
Mail addresses
support@javacodegeeks.com
Opengraph URL: https://examples.javacodegeeks.com/java-compare-strings-example/
Generator: WordPress 6.9.4
Domain: examples.javacodegeeks.com
{"@context":"https://schema.org","@graph":[{"@type":"Article","@id":"https://examples.javacodegeeks.com/java-compare-strings-example/#article","isPartOf":{"@id":"https://examples.javacodegeeks.com/java-compare-strings-example/"},"author":{"name":"Nikos Maravitsas","@id":"https://examples.javacodegeeks.com/#/schema/person/686a494fbb577a8d1231862b139cda6b"},"headline":"Java Compare Strings Example","datePublished":"2014-03-06T11:04:01+00:00","dateModified":"2020-06-16T08:00:00+00:00","mainEntityOfPage":{"@id":"https://examples.javacodegeeks.com/java-compare-strings-example/"},"wordCount":610,"commentCount":0,"publisher":{"@id":"https://examples.javacodegeeks.com/#organization"},"image":{"@id":"https://examples.javacodegeeks.com/java-compare-strings-example/#primaryimage"},"thumbnailUrl":"https://examples.javacodegeeks.com/wp-content/uploads/2012/12/java-logo.jpg","articleSection":["String"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https://examples.javacodegeeks.com/java-compare-strings-example/#respond"]}]},{"@type":"WebPage","@id":"https://examples.javacodegeeks.com/java-compare-strings-example/","url":"https://examples.javacodegeeks.com/java-compare-strings-example/","name":"Java Compare Strings Example - Java Code Geeks","isPartOf":{"@id":"https://examples.javacodegeeks.com/#website"},"primaryImageOfPage":{"@id":"https://examples.javacodegeeks.com/java-compare-strings-example/#primaryimage"},"image":{"@id":"https://examples.javacodegeeks.com/java-compare-strings-example/#primaryimage"},"thumbnailUrl":"https://examples.javacodegeeks.com/wp-content/uploads/2012/12/java-logo.jpg","datePublished":"2014-03-06T11:04:01+00:00","dateModified":"2020-06-16T08:00:00+00:00","description":"Interested to learn more about Java? Then check out our detailed example on Java Compare Strings!By comparing we mean to check if their values are equal.","breadcrumb":{"@id":"https://examples.javacodegeeks.com/java-compare-strings-example/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https://examples.javacodegeeks.com/java-compare-strings-example/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https://examples.javacodegeeks.com/java-compare-strings-example/#primaryimage","url":"https://examples.javacodegeeks.com/wp-content/uploads/2012/12/java-logo.jpg","contentUrl":"https://examples.javacodegeeks.com/wp-content/uploads/2012/12/java-logo.jpg","width":150,"height":150,"caption":"Bipartite Graph"},{"@type":"BreadcrumbList","@id":"https://examples.javacodegeeks.com/java-compare-strings-example/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://examples.javacodegeeks.com/"},{"@type":"ListItem","position":2,"name":"Java Development","item":"https://examples.javacodegeeks.com/category/java-development/"},{"@type":"ListItem","position":3,"name":"Core Java","item":"https://examples.javacodegeeks.com/category/java-development/core-java/"},{"@type":"ListItem","position":4,"name":"lang","item":"https://examples.javacodegeeks.com/category/java-development/core-java/lang/"},{"@type":"ListItem","position":5,"name":"String","item":"https://examples.javacodegeeks.com/category/java-development/core-java/lang/string/"},{"@type":"ListItem","position":6,"name":"Java Compare Strings Example"}]},{"@type":"WebSite","@id":"https://examples.javacodegeeks.com/#website","url":"https://examples.javacodegeeks.com/","name":"Java Code Geeks","description":"Java Examples and Code Snippets","publisher":{"@id":"https://examples.javacodegeeks.com/#organization"},"alternateName":"JCG","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://examples.javacodegeeks.com/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https://examples.javacodegeeks.com/#organization","name":"Exelixis Media P.C.","url":"https://examples.javacodegeeks.com/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https://examples.javacodegeeks.com/#/schema/logo/image/","url":"https://examples.javacodegeeks.com/wp-content/uploads/2022/06/exelixis-logo.png","contentUrl":"https://examples.javacodegeeks.com/wp-content/uploads/2022/06/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https://examples.javacodegeeks.com/#/schema/logo/image/"},"sameAs":["https://www.facebook.com/javacodegeeks","https://x.com/javacodegeeks"]},{"@type":"Person","@id":"https://examples.javacodegeeks.com/#/schema/person/686a494fbb577a8d1231862b139cda6b","name":"Nikos Maravitsas","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https://examples.javacodegeeks.com/#/schema/person/image/","url":"https://examples.javacodegeeks.com/wp-content/uploads/2013/10/Nikos-Maravitsas-80x96.jpg","contentUrl":"https://examples.javacodegeeks.com/wp-content/uploads/2013/10/Nikos-Maravitsas-80x96.jpg","caption":"Nikos Maravitsas"},"description":"Nikos has graduated from the Department of Informatics and Telecommunications of The National and Kapodistrian University of Athens. During his studies he discovered his interests about software development and he has successfully completed numerous assignments in a variety of fields. Currently, his main interests are system’s security, parallel systems, artificial intelligence, operating systems, system programming, telecommunications, web applications, human – machine interaction and mobile development.","sameAs":["http://www.javacodegeeks.com/"],"url":"https://examples.javacodegeeks.com/author/nikos-maravitsas/"}]}
{"@context":"http:\/\/schema.org","@type":"BreadcrumbList","@id":"#Breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"item":{"name":"Home","@id":"https:\/\/examples.javacodegeeks.com\/"}},{"@type":"ListItem","position":2,"item":{"name":"Java Development","@id":"https:\/\/examples.javacodegeeks.com\/category\/java-development\/"}},{"@type":"ListItem","position":3,"item":{"name":"Core Java","@id":"https:\/\/examples.javacodegeeks.com\/category\/java-development\/core-java\/"}},{"@type":"ListItem","position":4,"item":{"name":"lang","@id":"https:\/\/examples.javacodegeeks.com\/category\/java-development\/core-java\/lang\/"}},{"@type":"ListItem","position":5,"item":{"name":"String","@id":"https:\/\/examples.javacodegeeks.com\/category\/java-development\/core-java\/lang\/string\/"}}]}
{"@context":"http:\/\/schema.org","@type":"Article","dateCreated":"2014-03-06T13:04:01+02:00","datePublished":"2014-03-06T13:04:01+02:00","dateModified":"2020-06-16T11:00:00+03:00","headline":"Java Compare Strings Example","name":"Java Compare Strings Example","keywords":[],"url":"https:\/\/examples.javacodegeeks.com\/java-compare-strings-example\/","description":"Today we are going to focus on how you can compare Strings in Java. By comparing, in this case, we mean to check if their values are equal. In the previous example, we talked generally about the Java","copyrightYear":"2014","articleSection":"String","articleBody":"\nToday we are going to focus on how you can compare Strings in Java. By comparing, in this case, we mean to check if their values are equal. \n\n\n\nIn the previous example, we talked generally about the Java String Class. We stated that Java lets you initialize a String like a primitive, and use the '+' operator like you would in a primitive, but here used to concatenate Strings together.\n\n\n\nThe similarities stop here. A String is in no way a primitive type. It is a classic Java Object. So comparing a String is no different from comparing any other Java Object.\n\n\n\n1. The '==' operator\n\n\n\nIt is strongly (and correctly) advised that you should never use '==' to compare any two Objects. Let's see an example\n\n\n\nEquals Operator\n\/\/Objects a and b are of the same non primitive type\nif(a==b)\n System.out.println(\"The to objects are equal\");\n\n\n\n\nIn the above example we are comparing two references, not objects. If the two references are equal, it simply means that they are pointing to the same Object instance. Consequently, a and b are equal, as they are exactly the same object.\n\n\n\nBut in most cases, you have two different discrete object of the same type that have equal contents. And it is the contents that matter in an equality check. This is where you use the equals method.\n\n\n\n2. Java Compare Strings - Using equals\n\n\n\nequals is a member of the Object class, so any class in Java can override it an create its own customized equality check.\n\n\n\nHere is how you can use it in Strings:\n\n\n\nEquals\nString a = \"Java is great!\";\nString b = \"Java is great!\";\nif(a.equals(b))\n System.out.println(\"The strings are equal\");\n\n\n\n\nThe output of the above is:\n\n\n\nThe strings are equal\n\n\n\nDue to String pooling that we've talked about in the previous example, the '==' operator also works:\n\n\n\nEquals Operator\nString a = \"Java is great!\";\nString b = \"Java is great!\";\nif(a == b)\n System.out.println(\"The strings are equal\");\n\n\n\n\nThe output of the above is:\n\n\n\nThe strings are equal\n\n\n\nThat's because literals with the same value are the same object exactly.\n\n\n\nNow, take a look at this :\n\n\n\nEquals With object\nString a = \"Java is great!\";\nString b = new String(\"Java is great!\");\nif(a == b)\n System.out.println(\"The strings are equale\");\nelse\n System.out.println(\"The two strings are not the same Object\");\nif(a.equals(b))\n System.out.println(\"But they hold the same string\");\n\n\n\n\nThe output of the above is:\n\n\n\nThe two strings are not the same Object\nBut they hold the same string\n\n\n\nSo you see why it is important to use equals for string comparison.\n\n\n\nLet's see the following snippet:\n\n\n\nEquals With different Case\nString a = \"Java is great!\";\nString b = \"Java Is Great!\"; \nif (a.compareTo(b) == 0)\n System.out.println(\"Strings are equal\");\n else\n System.out.println(\"Strings are NOT equal\");\n\n\n\n\nThe above prints out :\n\n\n\nStrings are NOT equal\n\n\n\nThis is because Strings in Java are case-sensitive, no matter the platform you are working on.\n\n\n\n3. Using equalsIgnoreCase\n\n\n\nIf you don't want to make case sensitive comparison, aka for you, the strings \"abcd\" and \"AbCD\" are the equal, then you can use equalsIgnoreCase:\n\n\n\nequalsIngoreCase\nString a = \"Java is great!\";\nString b = \"Java Is Great!\"; \nif (a.compareTo(b) == 0)\n System.out.println(\"Strings are equal\");\n else\n System.out.println(\"Strings are NOT equal\");\n\n\n\n\nThis will print out:\n\n\n\nStrings are equal\n\n\n\n4. Using compareTo\n\n\n\nThis is useful to make lexicographical comparison between two strings. This generates the Unicode value of each character in the string and compares with the Unicode value of other string. \n\n\n\nCompareTo\nString a = \"Java is great!\";\nString b = \"Java Is Great!\"; \nif (a.compareTo(b) == 0)\n System.out.println(\"Strings are equal\");\n else\n System.out.println(\"Strings are NOT equal\");\nSystem.out.println(\"a>b:\"+a.compareTo(b));\n\n\n\n\nIn the example above, it does character by character comparison. As soon as it reaches the character \"I\", the comparison ends. The Unicode value for 'i' is 105 while 'I' is 73. So result is returned as 32 and no further comparison proceeds. Result of running this program is indicated below\n\n\n\nStrings are NOT equal\na>b:32\n\n\n\n\n5. Using contentEquals\n\n\n\nTo understand contentEquals, we have to understand CharSequence interface. This was introduced after the concrete implementation of String. This method allows a String to be compared with any other implementations of CharSequence such as StringBuilder and StringBuffer. It does a character by character comparison and if any of the characters do not match, it returns false.\n\n\n\nCompareTo\nString a = \"Java is great!\";\nString b = \"Java Is Great!\"; \nif (a.contentEquals(new StringBuilder(b)))\n System.out.println(\"Strings are equal\");\n else\n System.out.println(\"Strings are NOT equal\");\n\n\n\n\nThis will print out:\n\n\n\nStrings are NOT equal\n\n\n\n6. Using comparison with literals\n\n\n\nAs we've said in the previous tutorial, literals are implemented as instances of String. We can compare literals like they were string objects.\n\n\n\nLet's see:\n\n\n\nLiteral Comparison-1\n String a = \"abc\";\n String b = \"aBc\";\n System.out.println(a.equals(\"abc\"));\n System.out.println(b.equalsIgnoreCase(\"abc\"));\n System.out.println(a.compareTo(\"abc\"));\n System.out.println(b.contentEquals(\"abc\"));\n\n\n\n\nYou can also do it like so:\n\n\n\nLiteral Comparison-2\n String a = \"abc\";\n String b = \"aBc\";\n System.out.println(\"abc\".equals(a));\n System.out.println(\"abc\".equalsIgnoreCase(b));\n System.out.println(\"abc\".compareTo(a));\n System.out.println(\"abc\".contentEquals(b));\n\n\n\n\nThis way you can avoid NullPointerException. But be careful because the absence of NullPointerException doesn't make the program correct.\n\n\n\nThis was an example on how to compare Strings in Java.\n\n\n\n7. Download the Source code\n\n\n\nThat was an example on Java Compare Strings.\n\n\n\nDownload\nYou can download the full source code of this example here: Java Compare Strings Example\n\n\n\nLast updated on Sept. 23, 2019\n","publisher":{"@id":"#Publisher","@type":"Organization","name":"Examples Java Code Geeks","logo":{"@type":"ImageObject","url":"\/wp-content\/uploads\/2013\/05\/JavaCodeGeeks-logo.png"},"sameAs":["https:\/\/feeds.feedburner.com\/examplesjavacodegeeks","https:\/\/www.facebook.com\/javacodegeeks","https:\/\/twitter.com\/javacodegeeks","https:\/\/www.linkedin.com\/groups\/3810709\/","https:\/\/www.youtube.com\/channel\/UCxoUc7Rar2q90Gu0nT2ffuQ","https:\/\/github.com\/javacodegeeks\/"]},"sourceOrganization":{"@id":"#Publisher"},"copyrightHolder":{"@id":"#Publisher"},"mainEntityOfPage":{"@type":"WebPage","@id":"https:\/\/examples.javacodegeeks.com\/java-compare-strings-example\/","breadcrumb":{"@id":"#Breadcrumb"}},"author":{"@type":"Person","name":"Nikos Maravitsas","url":"https:\/\/examples.javacodegeeks.com\/author\/nikos-maravitsas\/"},"image":{"@type":"ImageObject","url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg","width":1200,"height":150}}
| og:locale | en_US |
| og:type | article |
| og:site_name | Examples Java Code Geeks |
| article:publisher | https://www.facebook.com/javacodegeeks |
| article:published_time | 2014-03-06T11:04:01+00:00 |
| article:modified_time | 2020-06-16T08:00:00+00:00 |
| og:image | https://examples.javacodegeeks.com/wp-content/uploads/2012/12/java-logo.jpg |
| og:image:width | 150 |
| og:image:height | 150 |
| og:image:type | image/jpeg |
| author | Nikos Maravitsas |
| twitter:card | summary_large_image |
| twitter:creator | @javacodegeeks |
| twitter:label1 | Written by |
| twitter:data1 | Nikos Maravitsas |
| twitter:label2 | Est. reading time |
| twitter:data2 | 5 minutes |
| None | IE=edge |
| msapplication-TileImage | https://examples.javacodegeeks.com/wp-content/uploads/2013/05/JavaCodeGeeks-favIcon.png |
Links:
Viewport: width=device-width, initial-scale=1.0
Robots: index, follow, max-image-preview:large, max-snippet:-1, max-video-preview:-1