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 7 of 7

Thread: Why is if statement false?

  1. #1
    Junior Member
    Join Date
    Apr 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Why is if statement false?

    This application:

    class Method4
    {
    public static void main( String[] args )
    {
    System.out.println( "args[0] = " + args[0] );
    System.out.println( "args[1] = " + args[1] );

    if (args[1] == "test1")
    {
    System.out.println( "args[1] using if statement= " + args[1] );
    }
    }
    }
    Executing with this line:
    >java Method4 test0 test1

    yields this output:
    args[0] = test0
    args[1] = test1

    Question: Why is the if statement false?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Why is if statement false?

    You should use the equals() method to compare String objects, not the == operator.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Apr 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Why is if statement false?

    I will do that but why does my code not work?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Why is if statement false?

    why does my code not work?
    Because == doesn't compare the contents of an object.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Apr 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Why is if statement false?

    Is it comparing the references?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Why is if statement false?

    Yes.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Feb 2014
    Posts
    180
    Thanks
    0
    Thanked 48 Times in 45 Posts

    Default Re: Why is if statement false?

    Java: Whats the difference between equals() and ==? might help you understand the difference between == and equals().
    Last edited by jashburn; April 22nd, 2014 at 07:25 PM. Reason: Fix link label

Similar Threads

  1. True and False that
    By kindon in forum Totally Off Topic
    Replies: 0
    Last Post: December 6th, 2013, 06:33 PM
  2. [SOLVED] Answer always false.
    By tyb97 in forum Loops & Control Statements
    Replies: 5
    Last Post: October 7th, 2011, 10:56 AM
  3. would this expression be true or false??
    By robertsbd in forum Java Theory & Questions
    Replies: 1
    Last Post: October 24th, 2010, 10:00 PM
  4. why is this statement evaluating false??
    By humdinger in forum Object Oriented Programming
    Replies: 2
    Last Post: November 3rd, 2009, 04:28 PM