Consider the following codes in java:
The output is false for the first example and true for the second example. In second example, parameter to equals() belongs String class, while in second example it to StringBuffer class. When an object of String is passed, the strings are compared. But when object of StringBuffer is passed references are compared because StringBuffer does not override equals method of Object class.
For example, following first program prints false and second prints true.
- String vs StringBuilder vs StringBuffer in Java
- Sorting collection of String and StringBuffer in Java
- Matcher appendReplacement(StringBuffer, String) method in Java with Examples
- Collator equals(String, String) method in Java with Example
- StringBuffer class in Java
- StringBuffer insert() in Java
- StringBuffer subSequence() in Java with Examples
- StringBuffer setLength() in Java with Examples
- A Java Random and StringBuffer Puzzle
- StringBuffer substring() method in Java with Examples
- StringBuffer codePointCount() method in Java with Examples
- StringBuffer lastIndexOf() method in Java with Examples
- StringBuffer codePointBefore() method in Java with Examples
- StringBuffer deleteCharAt() Method in Java with Examples
- StringBuffer getChars() method in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.