Java equals() Vs ==


The String equals() method is used to compare two strings for equality. It compares the characters inside a string object.

 

The == operator compares the two object references.


Example:

public class Equalsto
{   
	public static void main(String args[])
    {  
    	String s1=new String("Onlinetpoint"); 
        String s2=new String("Onlinetpoint");  
		System.out.println(s1+ " equals "+s2+"-> "+s1.equals(s2));
        System.out.println(s1+ " == "+s2+"-> " +(s1==s2));
    }
} 



OnlineTpoint is a website that is meant to offer basic knowledge, practice and learning materials. Though all the examples have been tested and verified, we cannot ensure the correctness or completeness of all the information on our website. All contents published on this website are subject to copyright and are owned by OnlineTpoint. By using this website, you agree that you have read and understood our Terms of Use, Cookie Policy and Privacy Policy.