Presentation is loading. Please wait.

Presentation is loading. Please wait.

The Boolean (logical) data type boolean

Similar presentations


Presentation on theme: "The Boolean (logical) data type boolean"— Presentation transcript:

1 The Boolean (logical) data type boolean

2 Logical data type: boolean
The boolean data type: is a built-in (primitive) data type of Java is used to represent the logical values There are 2 logical values:       true false    

3 Logical data type: boolean (cont.)
Encoding scheme used in the boolean data type: uses 1 byte of memory (to store 0 or 1) 0 represents true 1 represents false

4 Boolean literals There are 2 boolean literals (= logical constants) in Java: These 2 words are keywords (reserved words) in Java true and, false    

5 Defining boolean typed variables
Syntax to define an boolean typed variable: Notes: boolean NameOfVariable ; The keyword boolean announces the variable definition clause The NameOfVariable is an identifier which is the name of the variable. The variable definition clause is must be ended with a semi-colon ";" A boolean typed variable can store true (1) or false (0)

6 Defining boolean typed variables (cont.)
Example: public class Boolean01 { public static void main(String[] args) boolean a; // boolean typed variable a = true; System.out.println(a); // prints true a = false; System.out.println(a); // prints false a = (3 > 1); // 3 > 1 evals to true, so: a = true a = (3 <= 1); // 3 <= 1 evals to false, so: a = false }

7 Defining boolean typed variables (cont.)
Example Program: (Demo above code) Prog file: How to run the program:             Right click on link and save in a scratch directory To compile:   javac Boolean01.java To run:          java Boolean01

8 Operations that return a boolean result
There are 2 types of operators that return a boolean result (true or false): Compare operators: A compare operator compares 2 numerical expressions and return a Boolean result. Example: the compare operation 3 > 1 returns the Boolean value true

9 Operations that return a boolean result (cont.)
Logical operators: A logical operator compares 2 Boolean (logical) expressions and return a Boolean result. Example: the logical operation true AND false returns the Boolean value false

10 Operations that return a boolean result (cont.)
We will discuss the compare operators next and discuss the logical operators after a few webpages.


Download ppt "The Boolean (logical) data type boolean"

Similar presentations


Ads by Google