Join David Gassner for an in-depth discussion in this video Working with Boolean values and expressions, part of Java 8 Essential Training.
- A variable marked with the boolean data type can have a value of either true or false. I'll demonstrate this in the booleans project. Within the main method, I'll declare two variables, and I'll start with the keyword boolean, all lower case. That means this is a primitive variable, not an object. I'll name the variable b1, and I'll assign it a value of true. Then, I'll create another boolean variable, and I'll say that one is false. Now, I'm going to shrink down my project window to eliminate some flickering that's happening, then I'll add some output to the console.
I'll start with a string of "The value of b1 is" and then I'll append the value b1. I'll duplicate that line of code, and change both the label and the value, and then I'll run the class. And I get the expected output. The values of the primitive variables, b1 and b2, have been translated to equivalent strengths. So, true gets a string of true, and false gets a string of false. All primitive variables, as I've mentioned previously, can have default values.
So, if you declare them, but you don't assign their values, a boolean will always start off as false. If I try to demonstrate this within the method, it won't work; I'll get an error because local variables must be initialized. But you can also declare a variable outside the method as a member of a class. I'll place the cursor after the class declaration and start with the keyword static and then the data type. And I'll name this new variable, bDef.
I've declared it, but I haven't assigned its value. It's dimmed out right now because I don't have any references or other uses of the variable. But now, I'll come back down here and I'll duplicate my output code again, and I'll change the label and the value that I'm outputting. And I see that the value is false. Again, that's the default value. You can take any value and reverse its value by adding an exclamation mark. That's called the negation operator in java.
I'll create another variable that I'll name b3, and I'll give it a value of !b1. So I'm reversing the value of b1. It's starting as true and I'm changing it to false. I'll get some code to output the value, and I'll change the label and the value I'm outputting, and there's the result. B1 was true, and b3 is false. In addition to the keywords, you can also create boolean values from expressions by comparing values to each other.
I'll create an integer that I'll name i1, and I'll give it a value of 0. Then I'll say boolean b4, and I'll assign that an expression wrapped in a pair of parenthesis. The expression will look like this: (i1 !=0); I'll close the statement with a semicolon. The operator != means not equal to. So, once again, I'll output the result to the console, and b4 is false, i1 does have a value of zero, the expression is evaluating whether the values don't match, and so it returns false.
Finally, I'll show that you can take a string with the value of true or false, and parse it as a real primitive boolean. I'll create a string called sBoolean, and I'll give it a value of "true." Then, I'll create a boolean variable that I'll name parsed, and I'll give it a value by using the helper class for the boolean data type: the name of the class is Boolean, with an upper case "B." I'll call a method from that class named parsBoolean, and I'll pass in sBoolean, then I'll output the result.
I'll start with the label of parsed, and then I'll output the parsed variable, and there's the result. Note that when you call parsed boolean, you must pass in values that work for java. The strings true and false. So, that's a look at boolean values. When you want to represent boolean values, it's recommended that you use primitives whenever possible. You can also use the boolean helper class in some circumstances, but it takes a bit more memory. But, as you can see, it also has incredibly helpful methods you can use to manage your boolean values.
- Understanding the history and principles of Java
- Installing Java, IntelliJ IDEA, and BlueJ
- Creating a Java project
- Working with variables, values, and expressions
- Working with object data types
- Building, comparing, and parsing strings
- Debugging and exception handling
- Creating loops and reusable code
- Passing arguments by reference or value
- Using simple and complex arrays
- Creating custom classes
- Understanding inheritance and polymorphism
- Managing files with Java libraries
- Documenting code with Javadoc
- Packaging classes in JAR files