jQuery in Action, 3rd edition
The moose likes Beginning Java and the fly likes boolean assignment Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "boolean assignment" Watch "boolean assignment" New topic

boolean assignment

Tony Virdee

Joined: Jan 21, 2003
Posts: 6
Hi, once again I am befuddled by Java. The issue is performing an assignment for a boolean variable within a if statement:

public class c15
public static void main(String args[])
boolean alpha = true;
if (alpha = false)
System.out.println("alpha is now to false");

if (alpha == true)
System.out.println("alpha is equal to true");
} // close method
} // close class
The compiles correctly, I am expecting an output of: Alpha is now to false
However, nothing is displayed. Any ideas why?
I am thinking it may have something to do with the version of JDK or OS I am using.
I am using Windows 2000
Entering the command 'java -version' gives the command:
java version "1.4.0"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-b92)
Java HotSpot(TM) Client VM (build 1.4.0-b92, mixed mode)
karl koch
Ranch Hand

Joined: May 25, 2001
Posts: 388
in your first if you change alpha to false (alpha = false).

so neither the first nor the second if will evaluate to true.

= -> assignment
== -> comparison
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
The if statement sees the same value that is assigned to alpha. In otherwords, the test evaluates to false when you assign false to a bool variable, so the body of the if statement doesn't execute.

Java API Documentation
The Java Tutorial
subject: boolean assignment
It's not a secret anymore!