+ 5

Why it return false?

public class Main { public static void main(String[] args) { String s1="hello"; String s2="HELLO".toLowerCase(); if(s1==s2){    System.out.println("true"); } else{     System.out.println("false"); } } }

23rd Jan 2020, 5:41 PM
Akshay Raut
Akshay Raut - avatar
18 Antworten
+ 10
In java you need to use .equals() to compare strings Example: https://code.sololearn.com/cF21FYhHUZsV/?ref=app
25th Jan 2020, 6:57 AM
Easham Halder
Easham Halder - avatar
+ 8
In Java, you can't compare strings using the == operator as each string is a different object. == is used to compare objects. Use .equals() instead. if (s1.equals(s2)){ System.out.println("true"); } else{ System.out.println("false"); }
23rd Jan 2020, 5:51 PM
XXX
XXX - avatar
+ 6
#Read post first. https://www.sololearn.com/post/224631/?ref=app Now, In your questions. String s2 = "Hello".toLowerCase(); in this statement "Hello".toLowerCase(); Two things will be performed here 1. Hello object will be created in string constant pool. Because it will be not present in constant pool. 2. after that you are performing changes in existing object and you assign hello in s2. AT THIS TIME S1 REFERENCE WILL NOT PASS TO S2 REFERENCE THAT'S WHY YOU WILL GET FALSE. IF THE REFERENCE WILL BE SAME THEN YOU WILL GET TRUE.
23rd Jan 2020, 6:44 PM
P∆WAN M∆URY∆
P∆WAN M∆URY∆ - avatar
+ 3
Hello All Pawan Maurya¶ ɠı۷ɛ ɱɛ ʝą۷ą ÆˆÉ§Ä…Æ–Æ–É›Ć‹É É› Ê‚ÉŹÆĄàœžÉ± XXX Fernando Pozzetti Nabin Karki Easham Halder Sepehr Farid N4b3ts3 Got correct answer.Check this I find this is correct answer Because when you call toUpperCase or toLowerCase in Java it create new instant in heap. We can't compare heap object with string pool object using ==
25th Jan 2020, 2:03 PM
Akshay Raut
Akshay Raut - avatar
+ 2
Also, to add to my answer, if you said s2 = s1; Then s1==s2 would be true as they refer to the same object.
23rd Jan 2020, 5:56 PM
XXX
XXX - avatar
+ 2
sadly in java you cannot do this like python, but since it is an object oriented language , it does not see these two "objects" equal , even tho you modified s2, but for example if you make and s3 string same as "hello" without doing anything to it, it will see s3 object same as s2 object if u compare it to each other with == for checking the inside of each string , the String library uses the equal function to compare inside of strings ;)
23rd Jan 2020, 6:05 PM
Sepehr Farid
Sepehr Farid - avatar
+ 2
Fernando Pozzetti == will not always false if the content will be same. you can check String a = "Hello"; String b = "Hello"; if(a==b){ System.out.println("Hey"); } It will be printed // Hey
24th Jan 2020, 8:18 AM
P∆WAN M∆URY∆
P∆WAN M∆URY∆ - avatar
+ 1
N4b3ts3 Java
24th Jan 2020, 7:31 AM
Akshay Raut
Akshay Raut - avatar
+ 1
XXX I check hashCode of s1 and s2 also same
24th Jan 2020, 7:39 AM
Akshay Raut
Akshay Raut - avatar
+ 1
Fernando Pozzetti Right But See in this question it returns false
24th Jan 2020, 8:32 AM
Akshay Raut
Akshay Raut - avatar
+ 1
because == is an operator and compares the memory location wheras equals() is a method which comapres the values of the object. == is gemerally used for primitive values where reference type uses equals() method comapre the content of it.
25th Jan 2020, 11:08 AM
Nabin Karki
Nabin Karki - avatar
+ 1
it is not possible to compare objects Methods ———— compareTo(); compareToIgnoreCase(); equals(); equalsIgnoreCase(); contains(); matches(); are available to compare strings
25th Jan 2020, 2:08 PM
sree harsha
sree harsha - avatar
+ 1
Akshay Raut that's okay. But as far as I know (I don't know much Java), even if you didn't use .toLowerCase it would've returned false as long as you declare 2 different strings.
25th Jan 2020, 3:46 PM
XXX
XXX - avatar
0
https://code.sololearn.com/ciypzm8n8bI2/?ref=app I had a similar question 2 months ago and this is the code so you can check it. Check line 9.
23rd Jan 2020, 6:20 PM
Fernando Pozzetti
Fernando Pozzetti - avatar
0
Thanks Guy's
24th Jan 2020, 7:26 AM
Akshay Raut
Akshay Raut - avatar
0
Hey XXX Bro we can compare String literal using == because there are not store duplicate in string constant pool so s2 point on s1 If we try String s1="java"; String s="java"; System.out.println(s1==s2); It returns true
24th Jan 2020, 7:30 AM
Akshay Raut
Akshay Raut - avatar
0
Use equals() method. == will often return false even though they have the same content.
24th Jan 2020, 8:12 AM
Fernando Pozzetti
Fernando Pozzetti - avatar
- 2
What lang is that?
23rd Jan 2020, 5:53 PM
Esteban Chacon MartĂ­n
Esteban Chacon MartĂ­n - avatar