Viewers: in countries Watching now:
Join author David Gassner as he explores Java SE (Standard Edition), the language used to build mobile apps for Android devices, enterprise server applications, and more. This course demonstrates how to install both Java and the Eclipse IDE and dives into the particulars of programming. The course also explains the fundamentals of Java, from creating simple variables, assigning values, and declaring methods to working with strings, arrays, and subclasses; reading and writing to text files; and implementing object oriented programming concepts.
True and false values are stored in Boolean variables in Java. You can either use primitive data types or the matching wrapper class also spelled Boolean with an uppercase B. I am working in a project named Booleans which you can find in Chapter 04 of the Exercise Files. I'll declare two variables. For primitive data types, use the keyword boolean, all lowercase. My first variable will be names b1 and will have a value of true. The second variable will be b2 and it will have a value of false.
Then I'll make a copy of that and paste it and I'll run the application and I'll see that the value of b1 is true and the value of b2 is false. You can reverse the value of a Boolean variable by prepending the exclamation mark or bang symbol. So for example I'll create a new variable. I'll call it b3 and I'll use the reverse value of b1 using !b1. Then I'll make a copy of my println command and I'll paste it in.
I'll change to the b3 variable and I'll run the application and I see that the result of reversing b1 which started as true is now false. If you want to evaluate numbers as Booleans, you have to write the code yourself, but it's actually a pretty small amount of code. Let's create an integer value named i and set it to a value of 1. Now again in many languages the #1 would have an equivalent value of true, but that equivalents doesn't exist already in Java, so we have to code it. So I'll create a variable named boolean b4 and I'll use this logic.
I'll create a pair of parenthesis and in there I'll use the expression, i != 0. So the expression comparing the value of the integer to the value 0 is wrapped inside parenthesis that forces it to be evaluated first and then the Boolean result which results from asking the question do these values match is returned to my boolean variable. Then I'll once again use a println command, I'll put the value of b4 and because i does not equal 0, I should get a value of true.
If I change the value of int i to 0 then the value of b4 is false. So this simple expression is a very easy way to take care of the situation where you want to translate a numeric value to a Boolean value. Finally, just as with the other primitive data types there is a wrapper class. It's named Boolean with an uppercase B and as with all the data type wrapper classes, it's a member of the package java.lang which means it always available to your code. The Boolean class has a fairly small set of supporting methods.
I'll show you how to use this one, parseBoolean. The parseBoolean method accepts a String value and translates it into a real Boolean. So let's go back to Eclipse and at the bottom of the code, I am going to declare a String, I'll just name is s and I'll give a value of true. Then I'll create a boolean variable, I'll call it b5 and I'll call that parsing method from the Boolean class, Boolean. parseBoolean and I'll pass in the value of s. And finally, I'll once again copy and paste System.out.println and change to output the value of b5 and show you that the result is a value of true.
Now the logic that's going on here is pretty interesting. The parseBoolean method is non-case sensitive. So I can actually spell the word true with upper or lower case characters, even though the true keyword in Java is all lowercase. I'll make that change and show you that evaluation still returns true. And if I change the value of s2, false, either upper or lowercase, I'll get the value false, so far as expected. But what happens if you pass in a value that's neither true nor false? I'll pass in xyz.
When I run the application now, I don't get an error. Instead, I get back a value of false. The logic that this method is using is if it's not true then it must be false. So any version of the word true will return true and any other string will return false. So that's a look at some of the rules around Booleans in Java. Once again, you start off with the keywords true and false which are the only available literals for a Boolean variable. You can reverse a Boolean variable, using the != operator and you can use the Boolean wrapper class to parse strings as Booleans and accomplish other tasks that are common to true and false values.
Find answers to the most frequently asked questions about Java Essential Training (2011) .
Here are the FAQs that matched your search "" :
Sorry, there are no matches for your search "" —to search again, type in another word or phrase and click search.
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.