Booleans in java

By: aathishankaran Emailed: 1730 times Printed: 2344 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

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


Java Home | All Java Tutorials | Latest Java Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(0)


Be the first one to add a comment

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by aathishankaran
Web Security Issues
The Web User's Perspective
Server-side plug-Ins
The best way to avoid security vulnerabilities with new server
JavaScript Security
Window Object
Working with Status Bar Messages
Retrieving a Portion of a String
Referencing Windows
Math Object
Frame Object
Document Object
Closing Windows
Built-in Object in Javascript
Textarea Object

More Tutorials in Java
Update contents of a file within a jar file
Tomcat and httpd configured in port 8080 and 80
Java File
Java String
Count number of vowels, consonants and digits in a String in Java
Reverse a number in Java
Student marks calculation program in Java
Handling Fractions 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
Multiple Inheritance sample in Java
Java program using Method Overriding
Java program to check if user input is an even number

More Latest News
Most Viewed Articles (in Java )
How to Send SMS using Java Program (full code sample included)
Student marks calculation program in Java
Count number of vowels, consonants and digits in a String in Java
XML and Java - Parsing XML using Java Tutorial
What is Java?
FileReader and FileWriter example program in Java
Stack example in Java - push(), pop(), empty(), search()
Method Overriding in Java
arraycopy example in Java
Read from a COM port using Java program
How to use ArrayList in Java
Method Overloading (function overloading) in Java
InetAddress Example program in Java
The Basic Structure of a Simple Java program
The switch Statement example in Java
Most Emailed Articles (in Java)
The Unsigned Right Shift
Reverse a number in Java
Multiple Inheritance sample in Java
Java program using Method Overriding
Java program to display numbers from 1 to 10 on Applet
Insert, Delete, Search, Print an int Array in Java
Steps in using verisign certificate with Glassfish appserver
Step by Step guide to setup freetts for Java
Method Overloading sample in Java
Method Override sample in Java
How Java is Organized?
Setting Up SSL on Tomcat
Compare compareTo() function of String class with Collator and CollationKey
Applet Notinited : Applet xxxxxxxxxxxx notinited
The break Statement example in Java