1 / 10

The Boolean (logical) data type boolean

The Boolean (logical) data type boolean. 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. Logical data type: boolean (cont.).

haglund
Télécharger la présentation

The Boolean (logical) data type boolean

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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) • 0representstrue • 1representsfalse

  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 keywordboolean 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 System.out.println(a); // prints true a = (3 <= 1); // 3 <= 1 evals to false, so: a = false System.out.println(a); // prints false } }

  7. Defining boolean typed variables (cont.) • Example Program: (Demo above code) • Prog file: http://mathcs.emory.edu/~cheung/Courses/170/Syllabus/06/Progs/Boolean01.java • 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 operatorsnext and discuss the logical operators after a few webpages.

More Related