0
In string comparison
When I Was using == in string comparison, statement is not printing on the console. But I use equals or compareTo then it works fine. Can u give me reason why this is happen in java I am accepting string user from word Str=sc.next(); And comparing Like if(str=="inside" && str1=="alive" ) { print statement} Problem is print statement is not display on the console
7 ответов
+ 1
Jinesh Jain
Please post a link 🔗 to the code, so I can inspect it
0
I am accepting string user from word
Str=sc.next();
And comparing
Like if(str=="inside" && str1=="alive" )
{ print statement}
Problem is print statement is not display on the console
0
Jinesh Jain
if(Str.equals("inside") && Str1.equals("alive")) {
print(statement)
}
0
But why we can't use ==
0
Jinesh Jain
That's one of the things that makes Java different from other programming languages
0
OK thank you
0
See which I learnt from Web and practise is that if we want to compare two strings
There is compareTo(): it basically return integer of what is difference between two letters
equals()
These type of comparison is used for specific types of function