How to Compare Two Strings in Java

This is the simple java program related to string operation. The program use java builtin function to compare two strings.

Source Code 

package packaga.sixty;
import java.util.Scanner;
public class stringComparison {

public static void main(String[] args) {
String s1 = "assad";
System.out.print("String 1 is :"+s1+"\nEnter your String to compareas:");
Scanner in = new Scanner(System.in);
String s2 = in.next();
compareStr(s1,s2);
}//End main
private static void compareStr(String s1, String s2) {
// Illustration of .equals()
if(s1.equals(s2)){
System.out.print("Matched in .equals() Functiom\n");
}else{
System.out.print("Not Matched in .equals() Functiom\n");
}//End else
// Illustration of ==
if(s1==s2){
System.out.print("Matched in == \n");
}else{
System.out.print("Not Matched in == \n");
}//End else
}//End compareStr()
}

Output of the Program

 Compare Two Strings in Java


Share on Google Plus

About Asad

Asad Niazi is Software Engineer , Programmer, Web Developer and a young mentor of BloggersTown and PProgramming. Asad Love to writes about Technology, Programming, Blogging and make money online.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment