Reputation: 3
i'm new in java and have a phase of code like this:
import javax.swing.JOptionPane;
public class test
{
public static void main(String[] args) {
String value=JOptionPane.showInputDialog("please input your value");
if (value== "1"){
System.out.println("1");
}else{
System.out.println("not 1");
}
}
}
Question : why every time i put 1,system print "not 1"?
thanks alot
Upvotes: 0
Views: 4317
Reputation: 77546
Try replacing value == "1" with value.equals("1"). Strings in Java are references and there are no operator overloads to help you with equality. Sometimes the strings are interned and == would work, but not usually. You should always use the equals method.
Upvotes: 4