INTRODUCTION
In today's free lesson, we will talk about the last two primitive data types we have left – boolean and char.
We will encounter the boolean very often, especially when we start talking about if-else statements in the next section.
BOOLEAN DATA TYPE
A variable of type boolean can only be equal to the keyword true
or the keyword false
. These two specific words cannot be used as variable names. They are reserved words in Java, used to define boolean values.
Let's take a look at a simple example that shows us in an easy way how to define boolean variables and how to assign values to them.
App.java
Output
In the program above, we define a boolean variable named t
and assign it the initial value true
. When we ask to see what value the variable t
holds, it makes sense that the result we get will be true
.
In many cases, the value assigned to a boolean variable is not set directly by specifying true
or false
, but rather indirectly as the result of a condition, as demonstrated with the variable y
.
In this specific line of code, the value of x
(which was previously initialized to 10) is compared with 15.
Since x
is not greater than 15, the result of the comparison is false
, and this value is then assigned to the boolean variable y
.
Before we move on to the char
data type, let's discuss a common mistake made by programmers coming from other programming languages.
In Java, the boolean
data type cannot accept an integer value.
In other words, we cannot assign 0
to represent false
or 1
to represent true
.
In Java, a boolean variable can only have the values true
or false
— nothing else.
CHAR DATA TYPE
The char
data type represents individual characters in Java.
To correctly declare a char
value, the character must be enclosed in single quotes (' '
).
Be careful: if you use double quotes (" "
), the value will no longer be treated as a char
, but instead as a String
.
Sometimes, you may encounter a character value combined with a backslash (\
). This allows us to define any Unicode character we desire.
Unicode is a universal character encoding standard that assigns a unique number (code point) to every character across all languages and scripts. For example, the Unicode value for the letter 'A' is \u0041
.
There are also additional features of the char
data type that we will address immediately after you write and execute the following program.
App.java
Output
Let's take a closer look at what’s happening here, as you might be a bit confused by the result you see in the terminal.
We start by declaring a char
variable named a
and assigning it the value 'A'
.
Next, we add the number 1
to the variable a
.
In Java, when the addition operator is used between a char
and an int
, Java automatically converts the character A
to its corresponding ASCII value (which for uppercase A
is 65) and performs the addition as if it were between two integers.
The result, which is the number 66
, is then cast back to a character (according to the ASCII table, 66 corresponds to B
) and assigned to the char
variable b
.
Now, when we try to print the result of a + b
, we get the number 131
.
This happens because Java converts both variables to their corresponding int
values (based on the ASCII table) before performing the addition.
In the very next println()
call, the result is different because the first part of the println()
method is a String
.
As a result, Java ignores any arithmetic operations that follow the String
and simply concatenates the characters one after another.
In other words, when a String
is combined with any other data type, Java will print a single String
with all the values joined together side by side.
This process is called concatenation in Java.
Continuing to the next line of code, we apply the idea that numbers — as long as they fall within the valid range of the ASCII table (typically values from 0 to 127) — can be converted into char
values.
This allows us to represent numeric values as corresponding characters based on the ASCII encoding.
Finally, by using a backslash (\
) followed by a hexadecimal number from the Unicode table, we can print special characters and letters from other languages, such as the Greek letter Δ.
0 Comments
What do you think about Ground of Code?