If I have an int variable called C I can type if ( C==3 ) and it will work. But when I tried the same with a String variable it didn't work.I wrote if ( C=="B" ). What should I write to check a String's context?
Printable View
If I have an int variable called C I can type if ( C==3 ) and it will work. But when I tried the same with a String variable it didn't work.I wrote if ( C=="B" ). What should I write to check a String's context?
I'm assuming you meant to type "content" rather than "context".
Yes, you don't check the contents of a String using the == operator (for reasons that are rather complicated to explain at this stage of your Java learning).
Instead, you check the content of a String using the .equals(String) method:
Code:String string1 = "Hello";
String string2 = "World";
if (string1.equals(string2)) {
System.out.println("Equal");
}
else {
System.out.println("Not Equal"); // This will be displayed.
}