File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Output of this code?! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Output of this code?!" Watch "Output of this code?!" New topic

Output of this code?!

Shafian Kisna
Ranch Hand

Joined: Oct 25, 2005
Posts: 70
Hi Everyone,

I am aspiring to do SCJP 1.4. I came across some of these qns which I found it basic yet, unfortunately, I am having some difficulty to handle them. Could someone pls help me in the output?

1) Output of this prog?

class boolean
static boolean b;

public static void main(String args[])
int x = 0;
else if(b=false)
else if(b)
System.out.println("Value of x="+x);

My understanding since b is declared & not instantiated, the default value is false, so x=2. I think the answer is false but when I refered to the ans sheet, it was x=4.
Pls explain.....

Best Regards
Rajasekar Elango
Ranch Hand

Joined: Sep 13, 2004
Posts: 105
Hi Sk,

The trick is here

its b=false assignment and not comparison, the return value of assignment is the value assigned which is false.


SCJP 1.4
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15038

Double post

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
It is sorta covered in the JavaRanch Style Guide.
subject: Output of this code?!
It's not a secret anymore!