Using == operatorwill the following be correct? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Using == operatorwill the following be correct?

I know when use the == operator to compare 2 reference variable is used to see if they point to the same object, address. Will this code generate true? or is even allowed to to check if the reference object points to the same address of the string literal?String myName = "Bob";if (myName == "Bob"){ //do something }

15th Sep 2016, 3:15 AM
Machete X7
Machete X7 - avatar
3 Answers
+ 3
u can use method "equals" to compare values of strings. operator == compare addresses of variables
15th Sep 2016, 4:45 AM
Alexey Rudyk
Alexey  Rudyk - avatar
+ 1
Yes that code equals true
16th Sep 2016, 12:03 PM
FyremanMusic
FyremanMusic - avatar
+ 1
the code will run however we will be looking at a logic error, I had to refresh my mind. the rational operator == should not be used when comparing strings.
16th Sep 2016, 3:39 PM
Machete X7
Machete X7 - avatar