Can you use string as a condition in a if statement? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Can you use string as a condition in a if statement?

12th Aug 2019, 1:23 PM
Jason Cuerbo
Jason Cuerbo - avatar
4 Answers
+ 4
You can do somthing like. //create a variable to hold a string value String str = "jason"; //as strings are objects we can use its methods //one of its methods is .equals() this returns a //boolean if true the statement will execute if(str.equals("jason"){ System.out.print("hi my name is "+str);}
12th Aug 2019, 1:26 PM
D_Stark
D_Stark - avatar
+ 1
I see, thanks. :)
12th Aug 2019, 1:29 PM
Jason Cuerbo
Jason Cuerbo - avatar
+ 1
Jason Cuerbo your welcome, I have commented the code so you can see what's happening.
12th Aug 2019, 1:30 PM
D_Stark
D_Stark - avatar
+ 1
Thanks again heheh, I understand now xD
12th Aug 2019, 1:37 PM
Jason Cuerbo
Jason Cuerbo - avatar