Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: why is this statement evaluating false??

  1. #1
    Member
    Join Date
    Nov 2009
    Posts
    30
    Thanks
    27
    Thanked 0 Times in 0 Posts

    Default why is this statement evaluating false??

    Hi all I am very new to Java and have come across a problem. im sure this is extremely simple to most of you but i just cant figure it out.

    I have 2 variables, one called material and one called jewel

    the material variable contains the string "diomand" and the jewel variable contains the string "diomand ring"

    now the problem im having is when i execute the following into a tester: jewel == material + "ring";

    it evaluates to false. I cant work out why this is because im thinking it should be true.

    I understand this is a very noob question but as i said, i am very new to Java and indeed all programming.

    any help is very appreciated.

    many thanks

    john


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: why is this statement evaluating false??

    When comparing strings (and other objects as well) you should use the equals() function, for example

    if ( jewel.equals(material+" ring") ){//don't forget the space in " ring"
        //do something
    }

    Using the == checks to see whether these two objects refer to the same instance, which may or may not be true. Using the equals() function you evaluate the contents. Note that for primitives (int, char, byte, etc...) == will work
    Last edited by copeg; November 3rd, 2009 at 04:11 PM.

  3. The Following User Says Thank You to copeg For This Useful Post:

    humdinger (November 4th, 2009)

  4. #3
    Member
    Join Date
    Jul 2009
    Posts
    31
    Thanks
    3
    Thanked 6 Times in 5 Posts

    Default Re: why is this statement evaluating false??

    you should always use EQUALS when comparing strings.

    A String is an OBJECT, when doing String == String, you are asking computer to check if they are the same 'object'.

    for example:

    String cat = "meow";
    String tiger = "meow";
     
    System.out.print(cat == tiger);
    RETURNS: True.


    String turtle = "";
    //turtles are quiet.
    String cat = turtle + "meow";
    String tiger = "meow";
     
    System.out.print(cat == tiger);
    RETURNS: False.


    The first example uses the same reference to "meow" both times while in the 2nd example the computer doesnt do this, and the objects dont match.

    Just use cat.equals(tiger)

    good luck

  5. The Following User Says Thank You to rsala004 For This Useful Post:

    humdinger (November 4th, 2009)

Similar Threads

  1. If Statement Java Need help
    By Keno888 in forum Loops & Control Statements
    Replies: 5
    Last Post: October 25th, 2009, 01:53 AM
  2. another unreachable statement..
    By chronoz13 in forum Loops & Control Statements
    Replies: 3
    Last Post: October 2nd, 2009, 12:15 PM
  3. unreachable statement Again?
    By chronoz13 in forum Loops & Control Statements
    Replies: 3
    Last Post: October 1st, 2009, 10:23 AM
  4. If statement
    By Dave in forum Loops & Control Statements
    Replies: 2
    Last Post: August 27th, 2009, 10:11 AM
  5. Evaluating to negative zero
    By helloworld922 in forum Java Theory & Questions
    Replies: 6
    Last Post: June 25th, 2009, 02:34 PM