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
posted
0
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
posted
0
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.