This is a Java program that demonstrates the use of the equals() method, the == operator, and the compareTo() method to compare strings.
public class Equals_CompareTo_Operator { public static void main(String[] args) { String str1 = new String("Apple"); String str2 = new String("Cherry"); System.out.println("Comparing Strings using equals() Method.."); System.out.println("String 1 and 2 : "+str1.equals(str2)); System.out.println("String 1 and 1 : "+str1.equals(str1)); System.out.println("Comparing Strings using == Operator.."); System.out.println("String 1 and 2 : "+(str1 == str2)); System.out.println("String 1 and 1 : "+(str1 == str1)); System.out.println("Comparing Strings using compareTo() Method.."); System.out.println("String 1 and 2 : "+str1.compareTo(str2)); System.out.println("String 2 and 2 : "+str2.compareTo(str2)); } }
Comparing Strings using equals() Method.. String 1 and 2 : false String 1 and 1 : true Comparing Strings using == Operator.. String 1 and 2 : false String 1 and 1 : true Comparing Strings using compareTo() Method.. String 1 and 2 : -2 String 2 and 2 : 0
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions