DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4
  1. #1
    Join Date
    Mar 2004

    Using strings inside of "if" statements

    I am trying to use an if statement to compare strings and it doesn't appear to be working. The format I am using is:

    if (stringname = "value")
    do this;

    However this doesn't seem to work, unless I'm missing something it it looks like it pretty much ignores the if statement altogether despite ways I change it around. Any help would be appreciated.

  2. #2
    Join Date
    Dec 2002
    Two things. First, what you should use for checking String equality is:
    if (stringname.equals("value")) {
     // stuff!
    If you use the == operator, it actually compares the address of the String objects which isn't what you want.

    Second, using a single = means assignment. So in your original if statement, it sets the variable stringname's value to "value", and the if statement evaluates to true because it was successful. A little wierd I know.

    When doing a compare, use two ==. Here is how an if comparing two ints would look:
    int a = 0;
    int b = 1;
    if (a == b) {
     // more stuffs!
    -- Steven

  3. #3
    Join Date
    Mar 2004
    Thanks, it works now. I meant to put the "==" in the example as it was what I was using I just keep forgetting little stuff like that without the compiler to scream at me .

  4. #4
    Join Date
    Feb 2004
    just remember:

    primitives start with a lowercase letter:

    int boolean byte short float double long char

    and you compare those with ==

    if( myInt == 1)


    Objects start with an Uppercase Letter:

    String s = "hello";
    File f;
    System.out.println //System is an object
    Character c;
    Integer i; //the primitives all have Object equivalents, for special uses.

    you compare the CONTENT of 2 objects with .equals():

    you compare MEMORY ADDRESSES of 2 objects, with ==
    this is used occasionally, say for if a button handler is handling 10 buttons, and it is told which button generated the event :

    if buttonThatGeneratedTheEvent == saveButton{
    else if buttonThatGeneratedTheEvent == quitButton{


    simple rule is: use == for primitives and .equals for objects
    The 6th edict:
    "A thing of reference thing can hold either a null thing or a thing to any thing whose thing is assignment compatible with the thing of the thing" - ArchAngel, www.dictionary.com et al.
    JAR tutorial GridBag tutorial Inherited Shapes Inheritance? String.split(); FTP?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.