How to compare two strings ?

How to compare two strings ?

There are two ways to compare the two strings in java: 

 * using the == operator, * using the equals() method.

 For Example:String s1=new String(\"Hello!\");String s2=new String(\"Hello!\");if(s1==s2) System.out.println(\"Equal using == operator.\");elseif (s1.equals(s2))System.out.println(\"Equal using equals method.\");elseSystem.out.println(\"Not Equal.\");

Date:2012-01-09 00:00:00

Post Your Answers

User Email:

User Name:

Related Java Subjective Links

Java Subjective interview questions and answers for experienced and fresher