+ 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"); } } }
4 Answers
+ 3
== sign compare object not value.
Your answer is here
https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java
+ 2
You can't compare String with ==
To compare String use equals method like p.equals("hi")
+ 1
Thank you
+ 1
But what is happening when I use == sign can tell me