Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

boolean assignment

 
Tony Virdee
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 388
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic