Java: Boolean Data Type

An error occurred trying to load this video.

Try refreshing the page, or contact customer support.

Coming up next: Java Data Type Conversion

You're on a roll. Keep up the good work!

Replay
Your next lesson will play in 10 seconds
• 0:04 Boolean
• 1:08 Syntax
• 1:37 Some Examples
• 2:50 Lesson Summary
Save Save

Want to watch this again later?

Timeline
Autoplay
Autoplay
Speed Speed

Recommended Lessons and Courses for You

Lesson Transcript
Instructor: Martin Gibbs

Martin has 16 years experience in Human Resources Information Systems and has a PhD in Information Technology Management. He is an adjunct professor of computer science and computer programming.

True or false? This is a question that's asked frequently in programming languages. Java is no exception. In this lesson, we'll learn about the Boolean data type (true or false) and provide some examples.

Boolean

George Boole invented logic in math and defined the algebraic concept named after him. In Java, the Boolean data type is a primitive data type, which means it comes packaged with the programming language. A Boolean data type can only have a value of either true of false. Note that these are keywords that are reserved by the programming language: they cannot be used for variable, function, class, or object names!

The concept of a switch, in this case true or false, is a core component of any programming language. Therefore the result of a Boolean operation is only true or false (lowercase in Java code).

It may not seem obvious at first but other Java programming features rely inherently on a Boolean value. Think of an if statement: The code will execute if a specific condition is true. It won't if the condition is false. Therefore, the Boolean logic is embedded deep within the guts of Java!

Now that we've established the Boolean data type as primitive, or native to the language, and defined its possible values (true or false), let's look at the syntax of declaring a Boolean variable.

Syntax

The following is the basic syntax for declaring a Boolean variable. Notice that there are three lines, even though we stated that only true or false are valid:

`boolean isValid;boolean flag = true;boolean stop = false;`

Each line is valid, but be careful! The first variable, isValid, will give you headaches if you try use it later in your code. Java doesn't know what you want the value to be: It will NOT default in a value.

Therefore, it is highly recommended to initialize your variables! Whether you pick true or false depends entirely on the situation or what you are trying to accomplish with the code.

Some Examples

Let's take a look at how the Boolean data type is used in programming. Pay careful attention to how the true/false logic is used and evaluated in these examples.

Example 1: Stop Flag

A very simple example is here. We set a stop flag to false and then begin some processing. Once the counter (i) hits 5, we flip the flag to true. There could be other code in the program that depends upon that flag being true or false.

`boolean stop = false;for(int i = 0; i < 10; i++) {  if(i = 5) {   stop = true;  }}System.out.println(stop);`

To unlock this lesson you must be a Study.com Member.

Register to view this lesson

Are you a student or a teacher?

See for yourself why 30 million people use Study.com

Become a Study.com member and start learning now.
Back
What teachers are saying about Study.com

Earning College Credit

Did you know… We have over 200 college courses that prepare you to earn credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.