I know this is a stupid question, but why can I not compare a string literal with a string variable? For instance, compile and run this program. If you enter "blah" as prompted, it will return a false value.:
-Greg :-Q
Code:
import javax.swing.*;
import java.io.*;
class test
{
public static void main(String args[])
throws java.io.IOException
{
String testIt;
testIt = JOptionPane.showInputDialog("Enter 'blah':");
if (testIt == "blah")
{
System.out.println("The values are equal.");
}
else
{
System.out.println("testIt = ***" + testIt + "***");
System.out.println("WTF???");
}
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
System.out.print("\n\n\nType 'blah' then hit enter: ");
testIt = br.readLine();
System.out.print("\n\n");
if (testIt == "blah")
{
System.out.print("The values are equal.");
}
else
{
System.out.println("testIt = ***" + testIt + "***");
System.out.println("WTF???");
}
System.out.print("\n\n");
System.exit(0);
}
}