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

16th Jul 2018, 9:49 AM
Jinesh Jain
Jinesh Jain - avatar
7 odpowiedzi
+ 1
Jinesh Jain Please post a link 🔗 to the code, so I can inspect it
16th Jul 2018, 10:45 AM
Dlite
Dlite - avatar
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
16th Jul 2018, 11:51 AM
Jinesh Jain
Jinesh Jain - avatar
0
Jinesh Jain if(Str.equals("inside") && Str1.equals("alive")) { print(statement) }
16th Jul 2018, 12:12 PM
Dlite
Dlite - avatar
0
But why we can't use ==
16th Jul 2018, 12:14 PM
Jinesh Jain
Jinesh Jain - avatar
0
Jinesh Jain That's one of the things that makes Java different from other programming languages
16th Jul 2018, 12:15 PM
Dlite
Dlite - avatar
0
OK thank you
16th Jul 2018, 12:21 PM
Jinesh Jain
Jinesh Jain - avatar
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
7th Aug 2018, 10:10 AM
Jinesh Jain
Jinesh Jain - avatar