Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

# Valentins Mock Exam Question 6

sun par
Ranch Hand
Posts: 257
public class Question07 {
public static void main(String[] args) throws Exception {
int i=2;
boolean b = true;
throw new Exception("Values are:"+(b!=b)+","+(i=args.length)+","+(b=i==2));
}
}
-----------------------------------------------
When I ran this code it gave me

ava.lang.Exception: Values are:false,0,false
at Question07.main(Question07.java:340)
Process completed with exit code 1
------------------------------------------------
Why is it so?

Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
(b!=b)
this asks if b is not equal to b. Obviously b is equal to b so this is false.
(i=args.length)
Assuming you entered no arguments when you ran this, args.length = 0 and that is what is assigned to i
(b=i==2));
This asks if i is equal to 2. i is equal to 0 (see above) so that is false and that value (false) is assigned to b.
[ October 07, 2002: Message edited by: Thomas Paul ]

sun par
Ranch Hand
Posts: 257
Thanks...

Gautam Jha
Greenhorn
Posts: 10
throw new Exception("Values are:"+(b!=b)+","+(i=args.length)+","+(b=i==2));
b != b is always false
i=args.length: i is assigned 0
b=i==2 translates to (b =(i==2)). i value is 0, so i==2 is false, which is assigned to b.