Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Whats the difference between these statements  RSS feed

 
Elim Banda
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Jeanne Boyarsky
author & internet detective
Sheriff
Posts: 37222
519
Eclipse IDE Java VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
== compares by reference. You aren't guaranteed that the Strings will be the same reference. equals() is the better way to compare String values.
 
Stevens Miller
Bartender
Posts: 1444
30
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Elim. As you may know, we usually don't just answer questions like yours directly. Instead, we try to guide you in the process of finding the answer yourself.

In this case, let me start by asking you to tell us what you think each of those lines of code does. For example, Line 1 (which has a typo in your version) is this:

It has several distinct parts. Try to answer these questions:

  • What is the effect of the word boolean in that line?
  • What is isActive after this line is executed? That is, what kind of a thing is it?
  • What does the equals sign ("=") do in that line?
  • What is the value true being used for?
  • Why is there a semi-colon (";") at the end of the line?

  • Don't worry if you aren't sure of the answers. We're not here to test you. But give us your best answers, and your best attempts at explaining what you think the other lines in your code are doing. From there, we can give you help in finding your way to the answers you want.
     
    Elim Banda
    Ranch Hand
    Posts: 46
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Jeanne Boyarsky wrote:== compares by reference. You aren't guaranteed that the Strings will be the same reference. equals() is the better way to compare String values.


    Thanks, but what do you mean "by reference" it makes a little more sense.
     
    Jeanne Boyarsky
    author & internet detective
    Sheriff
    Posts: 37222
    519
    Eclipse IDE Java VI Editor
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Consider:
    Object obj = "a";

    obj is a variable that refers to the String object "a". This is a reference.
     
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!