Can anyone help me with this code | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Can anyone help me with this code

public class Program { public static void main(String[] args) { System.out.println("main starts"); boolean boolval; if(boolval=true){ System.out.println(boolval) ; } System.out.println(boolval); } } I did not assign boolval to true,yet output shows true.how is this possible?

7th Jul 2020, 10:16 AM
Ajoh Pv
Ajoh Pv - avatar
2 Answers
+ 2
You need to use == to compare. Otherwise, it will set boolval to true.
7th Jul 2020, 10:19 AM
Rowsej
Rowsej - avatar
+ 2
Ajoh Pv You need to know the difference between = and == = is assignment operator == equality check operator means comparison operator. So boolval = true means you are assigning which will be true. That's why it's going in if statement and returns true.
7th Jul 2020, 2:20 PM
A͢J
A͢J - avatar