Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Java String-examples Java Compare String (== operator)

Related Tutorials


 
 

Share on Google+Share on Google+

Java Compare String (== operator)

Advertisement
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>javac stringmethod.java

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

C:\unique

Download this example.

 

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: August 18, 2007

Related Tutorials

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

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:7
Pankaj
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."); } } }
suvatea
October 10, 2011
*

this is not correct for reading strings from keyboard
B.Rajesh
February 22, 2012
Programes

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
azhar
April 18, 2012
java strings

please give me the complete java strings with examples to prepare for an interview
DIVI HARI BABU CHOWDARY
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.in); System.out.println("Please Enter a String"); String s=sc.next(); Output Please Enter a String Hi how are you? But it takes only "Hi". I need to take whole the String. How??????????
Hemanth
June 23, 2012
Strings

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 java.io.*; public class G2 { public static void main(String args[]) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 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"); } } }
DMCA.com