+ 1

Why is the output of this code is like this

When the user input hi why is the output is the excution of else statement not if import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner in = new Scanner(System.in); String p = in.nextLine(); if(p == "hi"){ System.out.println("hi"); }else{ System.out.println("error"); } } }

13th Feb 2020, 8:59 AM
‎يوسف سعد‎
‎يوسف سعد‎ - avatar
4 Answers
+ 3
== sign compare object not value. Your answer is here https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java
13th Feb 2020, 9:23 AM
A͢J
A͢J - avatar
+ 2
You can't compare String with == To compare String use equals method like p.equals("hi")
13th Feb 2020, 9:04 AM
A͢J
A͢J - avatar
+ 1
Thank you
13th Feb 2020, 9:17 AM
‎يوسف سعد‎
‎يوسف سعد‎ - avatar
+ 1
But what is happening when I use == sign can tell me
13th Feb 2020, 9:18 AM
‎يوسف سعد‎
‎يوسف سعد‎ - avatar