Programming, website development forum Get latest updates by RSS Follow TechnicalTalk on Twitter Follow TechnicalTalk on Facebook 
HomeSearchRecent PostsLoginRegisterContact Us

Username  
Password    
  Forgot your password?  

Pages: [1]   Go Down
 
  Email this topic  |  Print
0 Members and 1 Guest are viewing this topic.

compare two string variables

 
webmaster forum
vijay12  Offline
Activity
0%
 
Regular Coder
Posts: 67
Topics: 33
January 08, 2009, 08:11:12 AM

hello dear friends . can anyone  suggest , what should i  use to compare two string variables - the operator == or the method equals()?  thank you !!
 
webmaster forum
polas  Offline
Activity
33.33%
 
Code Guru
Gender: Male
Posts: 1399
Topics: 85
WWW
January 08, 2009, 09:15:17 AM

Do not use ==, that will not work as it will check the references rather than the objects themselves. Using equals will achieve what you want i.e. stringone.equals(stringtwo)

This is all in the API reference http://java.sun.com/javase/6/docs/api/

Mesham Type Oriented Parallel Programming Language, Free online technical support
 
webmaster forum
kevisa23  Offline
Activity
0%
 
New Poster
Posts: 7
Topics: 1
September 09, 2010, 05:07:10 AM

use a String methods compareTo () or compareToIgnoreCase() these methods return an interger
 
webmaster forum
Activity
0%
 
Skilled Coder
Posts: 125
Topics: 0
January 28, 2011, 10:37:45 PM

The equals method is used when we need to compare the content of the text present in the String objects. The == operator is used when we have to compare the String object references.

Optometry software

Network Management Service
 
webmaster forum
Life Is Good!
Activity
0%
 
Professional Coder
Gender: Female
Posts: 242
Topics: 3
WWW
April 09, 2011, 07:17:59 AM

use the equals() method.. its more accurate compared to the == operator..

Affordable Custom Web Design Services
 
webmaster forum
Andrew09  Offline
Activity
0%
 
New Coder
Posts: 36
Topics: 4
September 27, 2011, 11:42:47 PM

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. ");
  }
  }
}

web site development services | Web development company | reputation management 
 
webmaster forum
polas  Offline
Activity
33.33%
 
Code Guru
Gender: Male
Posts: 1399
Topics: 85
WWW
October 03, 2011, 03:13:47 AM

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. ");
  }
  }
}

Wrong Wrong Wrong!

Using == compares the objects (i.e. are string1 and string2 the same object) rather than the values. Instead use equals so it will be:

Code:

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


Mesham Type Oriented Parallel Programming Language, Free online technical support
 
webmaster forum
Activity
0%
 
New Coder
Real name: Opal Infotech
Posts: 16
Topics: 1
WWW
November 10, 2011, 06:31:52 AM

Hi, According to when you compare Two String it is equal().  and == operator is not use for compare two Strings.  

web design india
 
webmaster forum
rahulbiz  Offline
Biz
Activity
0%
 
Regular Coder
Gender: Male
Age: 24
Posts: 87
Topics: 31
November 14, 2011, 04:44:07 AM

i think instead of == use equal is better.

b2b portal
agriculture product
 
  Email this topic  |  Print
Pages: [1]   Go Up
 
Jump to:  



Powered by SMF 1.1.15 | SMF © 2011, Simple Machines


Google visited last this page January 27, 2012, 11:01:40 AM