How the following code results in false output? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How the following code results in false output?

public class Program { public static void main(String[] args) { boolean v = (true||!(!false ))?false:true ; if(false){ v=true; } else { v=v==true; } System.out.print(v); } } //What is v=v==true?

10th May 2017, 10:22 AM
Prathamesh Jadhav
Prathamesh Jadhav - avatar
4 Answers
+ 8
Line 1:- boolean v = (1) ? (2) : (3) means if 1 is true, then assign v =2, else v=3. For your line , 1 is true, hence 2 is assigned, 2 is false. Hence v is false if(false) is never gonna run, else will run v=v==true; this is same as v = (v == true); that is, if v==true , then assign v as true, else assign it false.(== returns either true or false) since v is false and false == true is false , v==true is false .hEnce v is assigned false.
10th May 2017, 10:27 AM
Meharban Singh
Meharban Singh - avatar
+ 7
at boolean v. v is false if and else. execute else v = (false==true) now v = false System.out.print(v) Search value v and v is false print false
10th May 2017, 10:27 AM
Yanothai Chaitawat
Yanothai Chaitawat - avatar
+ 3
Anytime @Prathamesh
10th May 2017, 10:32 AM
Meharban Singh
Meharban Singh - avatar
0
Thanks Singh... That helped
10th May 2017, 10:29 AM
Prathamesh Jadhav
Prathamesh Jadhav - avatar