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

Thread: Need help understanding how Java uses expressions and how they evaluate

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

    Default Need help understanding how Java uses expressions and how they evaluate

    Hi,

    Hopefully this is a simple one. I have a section of code:

    int x = 17;
    int y = 9;
    String name = "Robinson";
    String bird = "Robin";

    My task is to explain what each expression evaluates to, but more importantly why. The 2 expressions I am having trouble with are:

    name == bird + "son";

    and

    name.equals(bird + "son");

    I have checked both statements in the application I am using and the first statement evaluates to False and the second evalutes to True. I'm not really undestanding how these expressions are working as I would expect both statements to evaluate as true?

    Any help is greatly appreciated!

    thanks


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,662
    Thanks
    45
    Thanked 1,960 Times in 1,934 Posts

    Default Re: Need help understanding how Java uses expressions and how they evaluate

    The == operator compares the values of the reference pointers to see if they both refer to the same object.
    The equals methods compares the contents of the two objects pointed to by the reference pointers.

    name and bird are what I am calling reference pointers. They have the address of String objects that contain Strings. You'd test if they both have the same address using the == operator.
    For example:
    name = "X";
    bird = name;

    Now bird == name is true. They both point to the same object.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Need help understanding how Java uses expressions and how they evaluate

    Hi,

    thanks very much for the reply. I think I understand what you are saying there. I'm still having trouble understanding why the statement:

    name == bird + "son";

    evaluates to False, I have got to that answer by running the code through the application I am using.

    The way I am reading it would suggst an answer of True, in that name = Robinson, bird + son would be Robin and son, so Robinson, but I guess I'm missing something with how the "+" operator works in this example?

    Am I missing something here?

    Cheers

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,662
    Thanks
    45
    Thanked 1,960 Times in 1,934 Posts

    Default Re: Need help understanding how Java uses expressions and how they evaluate

    If there are two String objects with the same contents, equals() will return true, == will return false.
    equals() compares the contents, == compares the locations of where the objects are located.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Need help understanding how Java uses expressions and how they evaluate

    ahh, great, got it!

    Thanks for the help

Similar Threads

  1. Java Project / Library to evaluate poker hands
    By Farmer in forum Java IDEs
    Replies: 1
    Last Post: February 14th, 2012, 09:25 AM
  2. Web Crawling & Java Regular Expressions
    By Nytol in forum Java Theory & Questions
    Replies: 1
    Last Post: November 18th, 2011, 01:34 PM
  3. Expressions in Java
    By naushadjadoon in forum Threads
    Replies: 3
    Last Post: October 14th, 2011, 05:16 AM
  4. Evaluate the environmental flexibility of programming in Java?
    By azzic7 in forum Java Theory & Questions
    Replies: 1
    Last Post: June 26th, 2010, 11:57 AM
  5. [SOLVED] Java Regular Expressions (regex) Greif
    By username9000 in forum Java SE APIs
    Replies: 4
    Last Post: June 11th, 2009, 05:53 PM