The boolean type in Java

  1. The boolean type is a primitive in Java.

  2. Boolean constants (truth values): true and false

  3. Boolean variables: can contain either true or false

    Boolean variables can be assigned the result of a boolean expression ( click here):

    Example: using boolean variables
          boolean xIsGreater;
          int x, y;
    
          ...
          if ( x > y )
    	 xIsGreater = true;
          else
    	 xIsGreater = false;    
    
    
    or:
    
    
          xIsGreater = (x > y);
    
    

  4. Using boolean variables in if statements:

    Example: using boolean variables in if statement
          boolean xIsGreater;
          ...
          if ( xIsGreater == true )
    	 ...
          else
    	 ...
    
    
    better:
    
    
          if ( xIsGreater )
    	 ...
          else
    	 ...