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: cant understand where the problem is

  1. #1
    Junior Member
    Join Date
    Dec 2019
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default cant understand where the problem is

    hello everyone!
    when i am running the code below I am getting output "false"
    but string x = z and userName = z and it still replays that they are not equal
    the code is writed in java language
    --------------------------------
    import java.util.Scanner;

    class ThirdClass {
    public static void main(String[] args) {
    Scanner myObj = new Scanner(System.in);
    System.out.println("Enter username");
    String x = "z";

    String userName = myObj.next();
    System.out.println(x);
    System.out.println(userName);
    System.out.println( userName==x);


    }
    }

  2. #2
    Member
    Join Date
    Apr 2014
    Posts
    92
    Thanks
    3
    Thanked 7 Times in 7 Posts

    Default Re: cant understand where the problem is

    Hello,

    This is a compile-time constant:

    String x = "z";

    This is not a compile-time constant:

    String userName = myObj.next();

    And this compares object reference equality, not object value equality:

    userName==x

    Even though both variables have a value of "z", they are not the same String object instance. This is the reason why Object.equals() exists. To get the result you expect (print 'true' when userName is "z"), use equals() instead of ==, like this:

    System.out.println(userName.equals(x)); // Or x.equals(userName))

    Hope that helps!

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

    TheRedLord_1 (December 11th, 2019)

  4. #3
    Junior Member
    Join Date
    Dec 2019
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: cant understand where the problem is

    Quote Originally Posted by BinaryDigit09 View Post
    Hello,

    This is a compile-time constant:

    String x = "z";

    This is not a compile-time constant:

    String userName = myObj.next();

    And this compares object reference equality, not object value equality:

    userName==x

    Even though both variables have a value of "z", they are not the same String object instance. This is the reason why Object.equals() exists. To get the result you expect (print 'true' when userName is "z"), use equals() instead of ==, like this:

    System.out.println(userName.equals(x)); // Or x.equals(userName))

    Hope that helps!


    Thank you very much, it solved the problem, I am very happy for your help, have a good day!!

Similar Threads

  1. how to understand oop
    By tolitx in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 30th, 2013, 06:48 AM
  2. Please Help me to understand
    By Theillusion in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 17th, 2013, 03:09 AM
  3. Please help to Understand x=++x+ + +x+x++
    By rayan2004 in forum What's Wrong With My Code?
    Replies: 8
    Last Post: December 2nd, 2012, 07:47 PM
  4. Simplest Problem but i am unable to understand it, WHY???
    By Mr.777 in forum Object Oriented Programming
    Replies: 11
    Last Post: October 26th, 2011, 03:28 AM
  5. Help me to understand this
    By Madhushan in forum Java Theory & Questions
    Replies: 2
    Last Post: September 10th, 2011, 08:47 AM