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
- StringBuffer insert() in Java
- StringBuffer class in Java
- StringBuffer subSequence() in Java with Examples
- StringBuffer setLength() in Java with Examples
- A Java Random and StringBuffer Puzzle
- StringBuffer codePointBefore() method in Java with Examples
- StringBuffer reverse() Method in Java with Examples
- StringBuffer trimToSize() method in Java with Examples
- StringBuffer offsetByCodePoints() method in Java with Examples
- StringBuffer replace() Method in Java with Examples
- StringBuffer delete() Method in Java with Examples
- StringBuffer codePointAt() 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.