Check out example codes for "string compare java". It will help you in understanding the concepts better.

Code Example 1

// These two have the same value
new String("test").equals("test") // --> true 

// ... but they are not the same object
new String("test") == "test" // --> false 

// ... neither are these
new String("test") == new String("test") // --> false

Code Example 2

if (aName.equals(anotherName))
        { 
            System.out.println(aName + " equals " + anotherName);
        }
        else 
            { 
                System.out.println(aName + " does not equal " +anotherName );
                           
            }

Code Example 3

// Java program to show how to compare Strings 
// using library function 
public class Test 
{ 
    public static void main(String[] args) 
    { 
        String s1 = "Ram"; 
        String s2 = "Ram"; 
        String s3 = "Shyam"; 
        String s4 = "ABC"; 
  
        System.out.println(" Comparing strings with compareTo:"); 
        System.out.println(s1.compareTo(s2)); 
        System.out.println(s1.compareTo(s3)); 
        System.out.println(s1.compareTo(s4)); 
    } 
}

Code Example 4

int str1.compareTo(String str2)

Code Example 5

// Java program to Compare two strings 
// lexicographically 
public class GFG { 
    public static void main(String args[]) 
    { 
        String string1 = new String("Geeksforgeeks"); 
        String string2 = new String("Practice"); 
        String string3 = new String("Geeks"); 
        String string4 = new String("Geeks"); 
        String string5 = new String("geeks"); 
  
        // Comparing for String 1 != String 2 
        System.out.println("Comparing " + string1 + " and " + string2 
                           + " : " + string1.equals(string2)); 
  
        // Comparing for String 3 = String 4 
        System.out.println("Comparing " + string3 + " and " + string4 
                           + " : " + string3.equals(string4)); 
  
        // Comparing for String 4 != String 5 
        System.out.println("Comparing " + string4 + " and " + string5 
                           + " : " + string4.equals(string5)); 
  
        // Comparing for String 1 != String 4 
        System.out.println("Comparing " + string1 + " and " + string4 
                           + " : " + string1.equals(string4)); 
    } 
}

Learn ReactJs, React Native from akashmittal.com