Home Java How to Compare two Strings in Java

How to Compare two Strings in Java

In this example we show how to compare two different strings in Java

There are a couple of ways of doing this, in  this example we will use the equals() method which returns a boolean value.

Example

import java.util.*;

public class JavaApplication1 
{
    public static void main(String[] args) 
    {
        String myString1 = "Maxprogramming";
        String myString2 = "maxprogramming";
        boolean val = myString1.equals(myString2);
        System.out.println(val);
        String myString3 = myString2;
        val = myString2.equals(myString3);
        System.out.println(val);
    }
    
}

 

Here is this examples output

run:
false
true

You may also like