+ 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"); } } }
18 ответов
+ 10
In java you need to use .equals() to compare strings
Example: https://code.sololearn.com/cF21FYhHUZsV/?ref=app
+ 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");
}
+ 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.
+ 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 ==
+ 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.
+ 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 ;)
+ 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
+ 1
N4b3ts3 Java
+ 1
XXX I check hashCode of s1 and s2 also same
+ 1
Fernando Pozzetti Right
But See in this question it returns false
+ 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.
+ 1
it is not possible to compare objects
Methods
————
compareTo();
compareToIgnoreCase();
equals();
equalsIgnoreCase();
contains();
matches();
are available to compare strings
+ 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.
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.
0
Thanks Guy's
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
0
Use equals() method. == will often return false even though they have the same content.
- 2
What lang is that?