How to use the Java boolean primitive – J011

by Apr 29, 2015




DeegeU Java Course

This video “How to use the Java boolean primitive” is part of a larger free online class called “Free Java Course Online”. You can find more information about this class on “Free Java Course Online” syllabus.

Try at home

  1. Type in a program with boolean types
  2. Type in a program with chars.  Try symbols other than the smiley face.
  3. Try creating errors in your program and compiling it

Transcript – How to use the Java boolean primitive

So far we’ve looked at all the ways represent numbers using Java primitives. In this lesson we’re going to look at how to represent boolean value in Java. Boolean values have two states. True/false. On/off. Yes/no. We’ll also get a look at how to represent single characters. Java characters are single letters, numbers, and symbols. Once we’re done with this, we are done with Java primitives.

So let’s get started.

This is the last lesson on Java primitives. By the end of this lesson you should understand what booleans are and how to create them. We’re then going to close up with characters. These are letters, numbers, and symbols.

This is called a boolean. Booleans are not only used to store just true/false, you can use them as expressions. So you can set a boolean primitive to the literal value true, or you can set it to something like 12 is greater than 14. This expression would evaluate to false.

java boolean primitive video - Free Java Course Online

A boolean value can be only true or false. There is no maybe.

Let’s take it look at the code to get a better feel for this. To declare a boolean, it’s the same pattern we’ve seen. Type, name, then value. The only possible literal values are the key words true and false. Here I have the name “bool” set to true. Let’s see what gets printed when we run this. And it prints “true”. You can also set the value to false.

The other thing I mentioned was you can treat expressions as a value. So if we said “bool” equal to 12 greater than 14, that means we want “bool” set to whatever that evaluates to. In this case “false”. Run it, and it prints “false”.

We’ll cover expressions and variables in the next few lessons.

The last Java primitive type is a 16-bit Unicode character. Unicode is the standard for representing characters from any language. It’s mostly letters, but there’s a lot of symbols too. There are only around 64,000 possible characters using this type. Unfortunately that’s way fewer than it needed to be. Unicode was eventually expanded to 32 bits to accommodate more cultural alphabets. When Java was created they hadn’t extended Unicode to 32-bit yet. They can’t retroactively change the type, because that would break everyone’s existing programs. So that means there are many characters you can’t use in the Java primitive. If you’re in a Western European country that’s not usually a problem. Other countries you going to need to use other solutions for characters that can’t be encoded in 16 bits. We’ll cover Unicode localization later. This is just a Java primitive type.

Still you do some fun things like set a value to a smiley face in code. Now you can find the value for particular character in Unicode lists for the simple ASCII characters like A, B, and C. You can use a chart like the one I have here.

Let’s go play with some characters and see what we can do. Finally we have characters. They’re a little different to define a character literal. It must be enclosed between two single quotes. In this case I’m setting the name “c” to the value ‘A’.

We run it. And it prints ‘A’.

We can also set a character to a number. This is a number that fits into two bytes. Positive only. Setting “c” to 66, and we run it. And it prints ‘B’, because capital ‘B’ has a character value 66.

We can also set it to a hex literal. Again two bytes. We’ll set it to 43. We run it. And it prints the letter ‘C’.

Like I said earlier we can also set it to other characters, like the smiley face. Run it. And it prints a smiley face. Characters have some uses, but we really want to string a bunch of characters together into a string of characters. We’lll talk about strings after we look at classes.

And on that note, we’re done with Java primitives!

Next we’re going to talk about variables, how they’re typed, and introduce a few key words to our toolkit.

See you in the next lesson!

Hey! Thanks for watching the video. There’s a quick quiz for this on DeegeU.com if you’d like to gauge how much you learned.

If you like the videos are seeing, please let me know by liking the video and hitting the subscribe button for the DeegeU channel on YouTube. I’d really appreciate that. If you have concerns or questions please leave them in the comments below or on DeegeU.com. There’s a poll on the front page of DeegeU.com so you can let me know what topic it’s covered next.

Thanks for watching, and see in the next video!







Tools Used

  • Java
  • NetBeans

Media Credits

All media created and owned by DJ Spiess unless listed below.

“How it Begins”
Kevin MacLeod (incompetech.com)
Licensed under Creative Commons: By Attribution 3.0
http://creativecommons.org/licenses/by/3.0/

“Audience Claps”
From “The Great Gabbo” (1929)
Public Domain

Get the code

The source code for “Are you ready to tackle the fizzbuzz test in Java?” can be found on Github. If you have Git installed on your system, you can clone the repository by issuing the following command:

 git clone https://github.com/deege/deegeu-java-intro.git

Go to the Support > Getting the Code page for more help.

If you find any errors in the code, feel free to let me know or issue a pull request in Git.

Don’t miss another video!

New videos come out every week. Make sure you subscribe!

Comments

comments

DJ Spiess

DJ Spiess

Your personal instructor

My name is DJ Spiess and I’m a developer with a Masters degree in Computer Science working in Colorado, USA. I primarily work with Java server applications. I started programming as a kid in the 1980s, and I’ve programmed professionally since 1996. My main focus are REST APIs, large-scale data, and mobile development. The last six years I’ve worked on large National Science Foundation projects. You can read more about my development experience on my LinkedIn account.

Pin It on Pinterest

Share This