Saturday, 12 September 2015

Java String equals

Java String equals

The java string equals() method compares the two given strings based on the content of the string. If any character is not matched, it returns false. If all characters are matched, it returns true.
The String equals() method overrides the equals() method of Object class.

Signature

  1. public boolean equals(Object anotherObject)  

Parameter

anotherObject : another object i.e. compared with this string.

Returns

true if characters of both strings are equal otherwise false.

Overrides

Java String equals() method example

equals() method of java Object class.

  1. public class EqualsExample{  
  2. public static void main(String args[]){  
  3. String s1="javatpoint";  
  4. String s2="javatpoint";  
  5. String s3="JAVATPOINT";  
  6. String s4="python";  
  7. System.out.println(s1.equals(s2));//true because content and case is same  
  8. System.out.println(s1.equals(s3));//false because case is not same  
  9. System.out.println(s1.equals(s4));//false because content is not same  
  10. }}  
Output
true
false
false

No comments:

Post a Comment