Big Moose Saloon
 Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies Register / Login Win a copy of Head First Android this week in the Android forum!

# Valentins Mock Exam Question 6

sun par
Ranch Hand

Joined: Oct 03, 2002
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?

Sunita<br />SCJP 1.4
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
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 ]

Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
sun par
Ranch Hand

Joined: Oct 03, 2002
Posts: 257
Thanks...
Gautam Jha
Greenhorn

Joined: Sep 23, 2002
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.