Way2Java

contentEquals Java

Use equals() to compare two strings but to compare a string with StringBuffer use contentEquals ().

Following both are the methods of String class returning boolean values.

public boolean contentEquals(java.lang.StringBuffer);
public boolean contentEquals(java.lang.CharSequence);
The next code explains contentEquals Java

.

public class Demo
{
  public static void main(String args[])
  {              
    String str1 = "hello";    
    String str2 = "hello";

    StringBuffer sb1 = new StringBuffer("hello");
    StringBuffer sb2 = new StringBuffer("world");

    boolean b1 = str1.equals(str2);        // works nice and returns true

    boolean b2 = str1.equals(sb1);         // works nice and returns false

    boolean b3 = str1.contentEquals(sb1);  // works nice and returns true

    boolean b4 = str1.contentEquals(sb2);  // works nice and returns false

    boolean b5 = str1.contentEquals(str2);  // works nice and returns true

    System.out.println(b5);
  }
}

The parameters of contentEquals () are StringBuffer and String (charSequence). equals() is used to compare two strings and contentEquals() is used to compare the contents of String and StringBuffer. Observe the above code and is self-explanatory.

For programming traps in using equals() and == with strings: String – Immutable Nature – Comparison

================Extra Reading on Inner classes=================

Because inner classes usage is growing slowly, they are discussed more elaborately here. Android (Java based Mobile OS developed by Google) uses inner classes very extensively, infact anonymous inner classes are passed as parameters to methods.

In this series following combinations are discussed.

1. Java Nested Classes
2. Static Nested Classes
3. Nested Interfaces
4. Interface inside Interface
5. Nested Interface within Class
6. Class inside Interface