{"id":1043,"date":"2012-04-19T11:25:36","date_gmt":"2012-04-19T17:25:36","guid":{"rendered":"http:\/\/bateru.com\/news\/?p=1043"},"modified":"2012-04-19T11:25:36","modified_gmt":"2012-04-19T17:25:36","slug":"a-quick-comparison-between-vs-equals-in-java","status":"publish","type":"post","link":"https:\/\/bateru.com\/news\/2012\/04\/a-quick-comparison-between-vs-equals-in-java\/","title":{"rendered":"A quick comparison between == vs equals in Java"},"content":{"rendered":"<p><code><\/p>\n<pre lang=\"java\">\r\n\/**\r\n@author Larry Battle <bateru.com\/news>\r\n@date April 19, 2012\r\n@purpose To show a simple comparison between == and equals().\r\n*\/\r\n\r\npublic class SameObjectTest {\r\n\tpublic static boolean areAssertsEnabled(){\r\n\t\tboolean isEnabled = false;\r\n\t\ttry{\r\n\t\t\tassert false: \"Yes asserts are enabled.\";\r\n\t\t}catch( AssertionError e ){\r\n\t\t\tisEnabled = true;\r\n\t\t}\r\n\t\treturn isEnabled;\r\n\t}\r\n\tpublic static void main(String[] args ){\r\n\t\t\/\/ All asserts should be without an error.\r\n\t\tString message = \"Test Completed with no errors.\";\r\n\t\tint[] ints = new int[]{ 10, 10, 20 };\r\n\t\tString[] strings = new String[]{ new String( \"Duck\" ), new String( \"Duck\" ), new String( \"Goose!\" ) };\r\n\t\tWorker[] workers = new Worker[]{ new Worker( \"Working\" ), new Worker( \"Working\" ), new Worker( \"Sleeping\" ) };\r\n\t\t\r\n\t\tassert ints[0] == ints[1] : \"10 is 10\";\r\n\t\tassert ints[1] != ints[2] : \"10 is not 20\";\r\n\t\t\/\/ Primative data types can't use ints[i].equals( ints[j] ) because they don't have methods.\r\n\t\t\r\n\t\t\/\/ Strings are a little bit more tricky. Go here for more inforamtion. http:\/\/www.janeg.ca\/scjp\/lang\/strLiteral.html\r\n\t\tassert strings[0] == strings[0]: \"An equality check, ==, on objects compares the references, not the values.\";\r\n\t\tassert strings[0] != strings[1]: \"strings[0] and strings[1] do not have the same reference point. In otherwords, that don't have the same Class and hashCodes.\";\r\n\t\tassert strings[0].equals( strings[1] ): \"String equals methods is predefined to compare the value of the string objects.\";\r\n\t\tassert !strings[0].equals( strings[2] ): \"the string duck should not equal the string goose!\";\r\n\t\t\r\n\t\t\/\/ You have to override the equals methods for user-defined objects.\r\n\t\tassert workers[0] != workers[1]: \"workers[0] and workers[1] have two different hash values.\";\r\n\t\tassert workers[0].equals( workers[1] ): \"However workers[0] and workers[1] are equivalent to eachother, according to the equals method.\";\r\n\t\tassert !workers[1].equals( workers[2] ): \"But this is not the case for workers[1] and workers[2].\";\r\n\t\t\r\n\t\tmessage = ( areAssertsEnabled() ) ? message : \"Asserts are disabled! Please enable with the switch -ea, ex. java -ea \";\r\n\t\tSystem.out.println( message );\r\n\t}\r\n}\r\nclass Worker{\r\n\tpublic String status;\r\n\tpublic Worker( String status ){\r\n\t\tthis.status = ( status != null ) ? status : \"Unknown\";\r\n\t}\r\n\tpublic boolean equals( Object obj ){\r\n\t\tboolean areSame = false;\r\n\t\tWorker x;\r\n\t\tif( obj != null  ){\r\n\t\t\tif( obj == this ){\r\n\t\t\t\tareSame = true;\r\n\t\t\t}\r\n\t\t\tif( this.getClass() == obj.getClass() ){\r\n\t\t\t\tx = (Worker) obj;\r\n\t\t\t\tif( x != null && x.status.equals( this.status ) ){\r\n\t\t\t\t\tareSame = true;\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t}\r\n\t\treturn areSame;\r\n\t}\r\n}\r\n\r\n<\/pre>\n<p><\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\/** @author Larry Battle @date April 19, 2012 @purpose To show a simple comparison between == and equals(). *\/ public class SameObjectTest { public static boolean areAssertsEnabled(){ boolean isEnabled = false; try{ assert false: &#8220;Yes asserts are enabled.&#8221;; }catch( AssertionError e ){ isEnabled = true; } return isEnabled; } public static void main(String[] args ){ &hellip; <a href=\"https:\/\/bateru.com\/news\/2012\/04\/a-quick-comparison-between-vs-equals-in-java\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">A quick comparison between == vs equals in Java<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[172,16],"class_list":["post-1043","post","type-post","status-publish","format-standard","hentry","category-tutorials","tag-java","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/posts\/1043","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/comments?post=1043"}],"version-history":[{"count":1,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/posts\/1043\/revisions"}],"predecessor-version":[{"id":1044,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/posts\/1043\/revisions\/1044"}],"wp:attachment":[{"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/media?parent=1043"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/categories?post=1043"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/tags?post=1043"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}