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

Thread: Why am I getting false ?

  1. #1
    Member
    Join Date
    Oct 2021
    Posts
    63
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default Why am I getting false ?

    I'm following this instruction :
    Write a Java program to check if a string starts with a specified word. Go to the editor
    Sample Data: string1 = "Hello how are you?"
    Sample Output:
    true

    here is my code :

     String string1 = "Hello how are you"; // intialize string
            String[] string2 = string1.split(" "); // split string into pieces
     
            System.out.println(string2[0]); // print Hello
     
            // if (string2[0] == "Hello") {
            // System.out.println("True");
            // }
            System.out.println(string2[0] == "Hello"); // return false why? I was expecting a True

    In the comments you can see my issue, I'm asking.

  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 am I getting false ?

    The == operator tests for the equality of the two operands. If the operators are objects, the == does NOT test the contents of the objects. With objects, the == tests if the two operands both refer to the same object.

    To compare the contents of objects use the equals method.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Oct 2021
    Posts
    63
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default Re: Why am I getting false ?

    Yes, you are right. Silly mistake. I just adjusted my code here it is but I'm still not getting what I'm looking for.

      String string1 = "Hello how are you"; // intialize string
            String[] string2 = string1.split(" "); // split string into pieces
     
            // System.out.println(string2[0]); // print Hello
     
            if (string2[0] == "Hello") {
                System.out.println("True"); // does not print anything? WHY? 
            }

    I do not get any output..

  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 am I getting false ?

    The code is using the == operator for comparing references to objects. You need to use the equals method to compare the contents of two objects.
    If you don't understand my answer, don't ignore it, ask a question.

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

    siid14 (March 25th, 2022)

Similar Threads

  1. [SOLVED] Why is my value coming out to be false ???
    By smartshahezad in forum What's Wrong With My Code?
    Replies: 0
    Last Post: July 12th, 2014, 09:35 PM
  2. Why is if statement false?
    By genefalk in forum What's Wrong With My Code?
    Replies: 6
    Last Post: April 22nd, 2014, 07:24 PM
  3. [SOLVED] Answer always false.
    By tyb97 in forum Loops & Control Statements
    Replies: 5
    Last Post: October 7th, 2011, 10:56 AM

Tags for this Thread