Write a program to compare two string ?

String comparison is a common programming task and Java provides several way to compare two String in Java.
Here are four examples of comparing String in Java
1) String comparison using equals method
2) String comparison using equalsIgnoreCase method
2) String comparison using compareTo method
4) String comparison using compareToIgnoreCase method


								
/**
 *
 * @author Mindclues
 */
public class EqualTest {
	public static void main (String [] args){
		String s1="abc";
		String s2="abc";
		String s3=new String("abc");
		String s4=new String();
		String s5=new String();
		
		System.out.println("Result of equals method--" +s1.equals(s1));
		System.out.println("Results of equalsIgnoreCase mthod--" +s1.equalsIgnoreCase(s1));
		System.out.println("Results of(==) result -- " +s1==s2);
		System.out.println("s3 and s1 equals--" +s3.equals(s1));
		System.out.println("s3 and s1(==) resutlt--" +s3==s1);
		System.out.println("s4 and s5(==) method in s4 and s5---" +s4==s5);
		System.out.println("Equals method in s4 and s5---" +s4.equals(s5));
	}
}
							

								
Result of equals method--true
Results of equalsIgnoreCase mthod--true
false
s3 and s1 equals--true
false
false
Equals method in s4 and s5---true


							

Related Articles

post a comment