Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Spring Framework | Web Services | BioInformatics | Java Server Faces | Jboss 3.0 tutorial | Hibernate 3.0 | XML
 
 
Hot Web Programming Job

 

Tutorial Categories: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML

[an error occurred while processing this directive]

Java Notes

Boolean

[an error occurred while processing this directive]

The primitive type boolean has only two possible values: true and false.

Boolean literals - true and false

The two values are written with the reserved words true and false.

Booleans in control statements

The if, for, while, and do statements all require boolean values. Usually these are written as boolean valued expressions, using operators which produce boolean values.

Comparison operators

Comparison operators are used to compare two primitive values (rarely objects).

OpNameMeaning
i < j less than6 < 24 is true.
i <= jless than or equal6 <= 24 is true.
i == j equal 6 == 24 is false.
i >= jgreater than or equal10 >= 10 is true.
i > j greater than10 > 10 is false.
i != j not equal 6 != 24 is true.

Logical operators

OpNameMeaning
a && bandThe result is true only if both a and b are true.
a || b or The result is true if either a or b is true.
!a nottrue if a is false and false if a is true.

Other operators and methods returning boolean values

  • The instanceof operator.
  • Many methods return boolean values, eg, equals, and methods that begin with "is". If you are writing your own boolean method, starting the name with "is" is a good practice.
  • Less common logical operators: &, |, and ^ with boolean operands. These are generally used with bits. || (or) and && (and) are preferred to | and & because they are short-circuit operators that can stop the evaluation when one of the operands determines the resulting value.

Boolean variables

You can declare boolean variables and test them. For example, this simple bubble sort keeps looping until there were no exchanges, which means that everything must be sorted. This is only an example, not a good way to sort.

void bubbleSort(int[] x, int n) {
    boolean anotherPass;  // true if something was out of order
    do {
        anotherPass = false;  // assume everything sorted
        for (int i=0; i<n-1; i++) {
            if (x[i] > x[i+1]) {
                int temp = x[i]; x[i] = x[i+1]; x[i+1] = temp; // exchange
                anotherPass = true;  // something wasn't sorted, keep going
            } 
        }
    } while (anotherPass);
}

Not like C/C++

Unlike C/C++, Java does NOT allow an integer expression where false is a zero value and true is any non-zero value. This doesn't work in Java.

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

0 comments so far (
post your own) View All Comments Latest 10 Comments:
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification

Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2007. All rights reserved.