wood burning stoves 2.0*
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


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "boolean assignment" Watch "boolean assignment" New topic
Author

boolean assignment

Tony Virdee
Greenhorn

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:
Version:
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
hi
in your first if you change alpha to false (alpha = false).

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

remember:
= -> assignment
== -> comparison
k
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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: boolean assignment