Pages

Wednesday, March 6, 2019

String contentEquals​() method in java with example - Internal Implementation

String contentEquals​() method in java with example:

contentEquals​ method is overloaded method in String class and these two are non static methods.

Read about Static in Java.

Syntax:

public boolean contentEquals​(StringBuffer sb)
public boolean contentEquals​(CharSequence cs)

Return type:

boolean in both cases. Returns true if content is matches, otherwise false.


String contentEquals​() method




public boolean contentEquals​(StringBuffer sb):

Compares this string to the specified StringBuffer. The result is true if and only if this String represents the same sequence of characters as the specified StringBuffer. This method synchronizes on the StringBuffer. 

This StringBuffer method version is introduced in java 1.4.

Java String contentEquals​(StringBuffer sb) example:

In this example program, we will compare String contents with StringBuffer contents by calling contentEquals​ method. 

Taking three strings and three StringBuffer's. We compare each string value againest StringBuffer value calling contentEquals​ method. For everytime, we call contentEquals​ method, it returns boolean value. We store the result in boolean variable. Printing the result using statement System.out.println() method.

package examples.java.w3schools.string;

public class StringcontentEqualsExample {
 public static void main(String[] args) {

  String string1 = "String One"; // This is String 1
  String string2 = "String Two"; // This is String 2
  String string3 = "String Three"; // This is String 3

  StringBuffer buffer1 = new StringBuffer("String One"); // This is StringBuffer 1
  StringBuffer buffer2 = new StringBuffer("String Two"); // This is StringBuffer 1
  StringBuffer buffer3 = new StringBuffer("String Three"); // This is StringBuffer 1
  
  // invoking contentEquals(StringBuffer sb) method
  
  boolean result1 = string1.contentEquals(buffer1);
  System.out.println("Result of comparing conents of string1 with buffer1 : "+result1);
  
  boolean result2 = string2.contentEquals(buffer2);
  System.out.println("Result of comparing conents of string2 with buffer2 : "+result2);
  
  boolean result3 = string3.contentEquals(buffer3);
  System.out.println("Result of comparing conents of string3 with buffer3 : "+result3);
  
  boolean result4 = string1.contentEquals(buffer3);
  System.out.println("Result of comparing conents of string1 with buffer3 : "+result4);
 }
}

Output:

The above program will produce the following output.


String contentEquals​() method-2

Result of comparing conents of string1 with buffer1 : true
Result of comparing conents of string2 with buffer2 : true
Result of comparing conents of string3 with buffer3 : true
Result of comparing conents of string1 with buffer3 : false


result 1,2,3 are comparing same conents of StringBuffer. So, it returned true. But when compared string1 with buffer3, the result4 was false because conents are different.

Java String contentEquals​(CharSequence sb) example:

Compares this string to the specified CharSequence. The result is true if and only if this String represents the same sequence of characters as the specified CharSequence. This method synchronizes on the StringBuffer. 

This CharSequence method version is introduced in java 1.5.

CharSequence is an interface in java. It's direct implementation classes are CharBuffer, Segment, String, StringBuffer, StringBuilder.

CharBuffer, Segment, String, StringBuffer, StringBuilder


We can pass any of these to contentEquals method.

Java String contentEquals​(CharSequence sb) example:

Writing a program to compare the conents of string and StringBuilder.

Taking three strings and three StringBuilder's. We compare each string value againest StringBuilder value calling contentEquals​ method. For everytime, we call contentEquals​ method, it returns boolean value. We store the result in boolean variable. Printing the result using statement System.out.println() method.

public class StringcontentEqualsExample2 {
 public static void main(String[] args) {
  
  String string1 = "One"; // This is String 1
  String string2 = "Two"; // This is String 2
  String string3 = "Three"; // This is String 3

  StringBuilder builder1 = new StringBuilder("One"); // This is StringBuilder 1
  StringBuilder builder2 = new StringBuilder("Two"); // This is StringBuilder 1
  StringBuilder builder3 = new StringBuilder("Three"); // This is StringBuilder 1

  // invoking contentEquals(StringBuilder sb) method

  boolean result1 = string1.contentEquals(builder1);
  System.out.println("Result of comparing conents of string1 with builder1 : " + result1);

  boolean result2 = string2.contentEquals(builder2);
  System.out.println("Result of comparing conents of string2 with builder2 : " + result2);

  boolean result3 = string3.contentEquals(builder3);
  System.out.println("Result of comparing conents of string3 with builder3 : " + result3);

  boolean result4 = string1.contentEquals(builder3);
  System.out.println("Result of comparing conents of string1 with buffer3 : " + result4);
 }
}

Output:

The above program will produce the following output

Result of comparing conents of string1 with builder1 : true
Result of comparing conents of string2 with builder2 : true
Result of comparing conents of string3 with builder3 : true
Result of comparing conents of string1 with buffer3 : false


Read about: What is the difference between String.equals() and String.contentEquals() method?


String contentEquals() method Internal Implementation:

We will see now how contentEquals method is implemented internally or how contentEquals method works internally. Below codes from java 11 or jdk 11 version.

public boolean contentEquals(CharSequence cs) internal code:

public boolean contentEquals(CharSequence cs) {
        // Argument is a StringBuffer, StringBuilder
        if (cs instanceof AbstractStringBuilder) {
            if (cs instanceof StringBuffer) {
                synchronized(cs) {
                   return nonSyncContentEquals((AbstractStringBuilder)cs);
                }
            } else {
                return nonSyncContentEquals((AbstractStringBuilder)cs);
            }
        }
        // Argument is a String
        if (cs instanceof String) {
            return equals(cs);
        }
        // Argument is a generic CharSequence
        int n = cs.length();
        if (n != length()) {
            return false;
        }
        byte[] val = this.value;
        if (isLatin1()) {
            for (int i = 0; i < n; i++) {
                if ((val[i] & 0xff) != cs.charAt(i)) {
                    return false;
                }
            }
        } else {
            if (!StringUTF16.contentEquals(val, cs, n)) {
                return false;
            }
        }
        return true;
    }

AbstractStringBuilder:

A mutable sequence of characters. Implements a modifiable string. At any point in time it contains some particular sequence of characters, but the length and content of the sequence can be changed through certain method calls. StringBuffer and StringBuilder are sub classes for AbstractStringBuilder.

Explanation:

1) First checks cs is instance of AbstractStringBuilder. Next checks for cs is instance of StringBuffer then call nonSyncContentEquals from synchronized block. If cs is instance of StringBuilder then call directly nonSyncContentEquals method.

2) If cs is instance of String then calls String class equal mehtod for comparing contents of both strings are equal.

3) If lengths of both cs and String are not equal then returns false.

4) IF string is latin then compare char by char in both cs and string.
5) If string is UTF then calls StringUTF16.contentEquals(val, cs, n).

public boolean contentEquals(StringBuffer sb) internal code:

   public boolean contentEquals(StringBuffer sb) {
        return contentEquals((CharSequence)sb);
    }


StringBuffer version method will call CharSequence conentEquals method internally.

Please post your questions and understanding in comment section for summary.

No comments:

Post a Comment

Please do not add any spam links in the comments section.