Java Compare String (== operator)

This section describes how two string references are compared.

Java Compare String (== operator)


This section describes how two string references are compared. If two String variables point to the same object then these two variables are called references for the same object. So to check this comparison programmatically,  == operator is used. If two variables refers the same object then this operator returns true value and if they don't then false value is returned. The essential point to remember while comparing strings with this operator is that it compares the references the two strings are pointing to rather than comparing the content of the string.  The following program uses this operator compare the two strings. If both strings are equal, it will display a message "The given strings are equal" otherwise it will show "The given string are not equal".

Description of code:

This is the method that compares an object values and returns Boolean type value either 'true' or 'false'. If it returns 'true' for the both objects, it will be equal otherwise not. Here in this case you will see that both the strings come out to be same that is because they have been allocated to the same memory.

public class stringmethod{
  public static void main(String[] args){
  String string1 = "Hi";
  String string2 = new String("Hello");
  if (string1 == string2) {
  System.out.println("The strings are equal.");
  else {
  System.out.println("The strings are unequal.");

Output pf the program:


C:\unique>java stringmethod
The strings are unequal.


Download this example.



Share on Google+Share on Google+

Java Compare String (== operator)

Posted on: August 18, 2007 If you enjoyed this post then why not add us on Google+? Add us to your Circles



Discuss: Java Compare String (== operator)   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
September 27, 2011
String Comparison by equals method.

In this section if you set the string String string1 = "Hi"; String string2 = new String("Hi"); then again it will print : "The strings are unequal." just because of the reason that "==" operator compares two String objects, not their values, and both the objects (i.e. string1 & string2 are different by name and their address location), if we want to compare both the strings then we have to use equals() method as: public class stringmethod{ public static void main(String[] args){ String string1 = "Hi"; String string2 = new String("Hi"); if (string1.equals(string2)) { System.out.println("The strings are equal."); } else { System.out.println("The strings are unequal."); } } }
October 10, 2011

this is not correct for reading strings from keyboard
February 22, 2012

Hi Sir i want to compare to Two Strings .out put will be comes below s1=abc s2=abq ans:s3=aabbcg. this type of examples iwant Sir please
April 18, 2012
java strings

please give me the complete java strings with examples to prepare for an interview
April 30, 2012
Clarification About Strings

How to Gather Whole String given at the time of enters string as input by using Scanner object?????????? For Example Scanner sc=new Scanner(; System.out.println("Please Enter a String"); String; Output Please Enter a String Hi how are you? But it takes only "Hi". I need to take whole the String. How??????????
June 23, 2012

strings are clearly understandable when we visit RoseIndia website thanks to rose india
eppy azwan
August 13, 2012
can u help me with this~

help me with this, dont know which line being the problem.. totally the output not show up.. import*; public class G2 { public static void main(String args[]) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(; String gender,res; System.out.println("Enter lines of text."); gender = br.readLine(); String f= "Female"; String m="Male"; if(gender == m){ System.out.println("male"); } else if(gender == f){ System.out.println("female"); } } }