Booleans in java
By: aathishankaran Printer Friendly Format
Booleans in java
Java has a simple type, called Boolean, for logical values. It can have only one of two possible values, true of false. This is the type returned by all relational operators, such as a<b. Boolean is also the type required by the conditional expressions that govern the control statements such as if and for.
Here is a program that demonstrates the Boolean type:
// Demonstrate Boolean values.
class BoolTest {
public static void main (String args[]) {
boolean b;
b = false;
System.out.println(“b is “ + b);
if ( b ) System.out.println(“This is not executed. “);
System.out.println(“10 > 9 is †+ (10 > 9) );
}
}
The output generated by this program is shown here:
b is false
b is true
This is executed.
10 > 9 is true
There are three interesting things to notice about this program. First, as you can see, when a boolean value is output by println(), “true†or “false†is displayed. Second, the value of a boolean variable is sufficient, by itself, to control the if statement. There is no need to write an if statement like this.
If
(b == true) …
Third, the outcome of a relational operator, such as <, is a boolean value. This is why the expression 10>9 displays the value “true.†Further, the extra set of parentheses around 10>9 is necessary because the + operator has a higher precedence than the >.
Comment on this tutorial
- Data Science
- Android
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
Subscribe to Tutorials
Related Tutorials
Program using concept of byte long short and int in java
Update contents of a file within a jar file
Tomcat and httpd configured in port 8080 and 80
Count number of vowels, consonants and digits in a String in Java
Student marks calculation program in Java
Calculate gross salary in Java
Calculate average sale of the week in Java
Vector in Java - Sample Program
MultiLevel Inheritance sample in Java