When does class matter in Java? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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.

10th May 2018, 3:15 PM
AM0
AM0 - avatar
1 Answer
+ 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.
10th May 2018, 4:22 PM
***
*** - avatar