{"comment":{"url":"https://api.github.com/repos/zielonyuspokaja/JavaSE-Tests/comments/12016648","html_url":"https://github.com/zielonyuspokaja/JavaSE-Tests/commit/33edae7d3cf08fed68ae0cbbbec9fd1cf8651c28#commitcomment-12016648","id":12016648,"user":{"login":"zielonyuspokaja","id":11321286,"avatar_url":"https://avatars.githubusercontent.com/u/11321286?v=3","gravatar_id":"","url":"https://api.github.com/users/zielonyuspokaja","html_url":"https://github.com/zielonyuspokaja","followers_url":"https://api.github.com/users/zielonyuspokaja/followers","following_url":"https://api.github.com/users/zielonyuspokaja/following{/other_user}","gists_url":"https://api.github.com/users/zielonyuspokaja/gists{/gist_id}","starred_url":"https://api.github.com/users/zielonyuspokaja/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/zielonyuspokaja/subscriptions","organizations_url":"https://api.github.com/users/zielonyuspokaja/orgs","repos_url":"https://api.github.com/users/zielonyuspokaja/repos","events_url":"https://api.github.com/users/zielonyuspokaja/events{/privacy}","received_events_url":"https://api.github.com/users/zielonyuspokaja/received_events","type":"User","site_admin":false},"position":null,"line":null,"path":"","commit_id":"33edae7d3cf08fed68ae0cbbbec9fd1cf8651c28","created_at":"2015-07-06T13:52:58Z","updated_at":"2015-07-06T13:52:58Z","body":"It turns out there are two different kinds of equality in Java - **reference equality** and **logical equality**. To explore the differences between these two kinds of equality examine the following code.\r\n\r\nThe default implementation of the equals() method in the Object class compares the references of the object being passed as the parameter and the object on which the method is called.\r\n\r\n_public boolean equals(Object obj) {\r\n    return (this == obj);\r\n}_\r\n\r\nWe can achieve the same result by directly comparing two points using \"==\" operator. In this case we check for the reference equality (address in memory) and in order to get logical equality we need to reimplement the equals() method of the Object class.\r\n\r\nWhat happens if we add our object to a collection?\r\n\r\n    public boolean contains(Object o) {\r\n        return indexOf(o) >= 0;\r\n    }\r\n\r\n    public int indexOf(Object o) {\r\n        if (o == null) {\r\n            for (int i = 0; i < size; i++)\r\n                if (elementData[i]==null)\r\n                    return i;\r\n        } else {\r\n            for (int i = 0; i < size; i++)\r\n                if (o.equals(elementData[i]))\r\n                    return i;\r\n        }\r\n        return -1;\r\n    }"}}