+ 1
When does class matter in Java?
I'm just getting into if else statements on java. Very new at coding. I haven't had much luck creating if else statements with strings. I'm just wondering if it's a mistake in my coding or if it has to do with something I haven't covered yet. I've been trying to create them under the default class in the playground.
1 Réponse
+ 4
I guess your code looks something like this:
if (string1 == "test"){
//some code
}
In Java you have two ways to check if a string is the same as another string.
1. By checking if it is the same object in the Java runtime memory. You do that by what I just showed you. string == "test"
2. By checking if the contents of two strings are the same. I guess this is what you want. You do that like this:
if (string1.equals ("test")){
//some code
}
I hope this makes things clear for you. Let me know if you have any other questions.