The types that you shouldn’t ignore are int
, double
, char
, and boolean
.
The char type
Several decades ago, people thought computers existed only for doing big number-crunching calculations. Nowadays, nobody thinks that way. So, if you haven’t been in a cryogenic freezing chamber for the past 20 years, you know that computers store letters, punctuation symbols, and other characters.The Java type that’s used to store characters is called char. The code below has a simple program that uses the char
type. This image shows the output of the program in the code below.
public class CharDemo {
public static void main(String args[]) {
char myLittleChar = 'b';
char myBigChar = Character.toUpperCase(myLittleChar);
System.out.println(myBigChar);
}
}
In this code, the first initialization stores the letter b in the variable myLittleChar
. In the initialization, notice how b is surrounded by single quote marks. In Java, every char
literally starts and ends with a single quote mark.
In a Java program, single quote marks surround the letter in a char
literal.
Character.toUpperCase
method does just what its name suggests — the method produces the uppercase equivalent of the letter b. This uppercase equivalent (the letter B) is assigned to the myBigChar
variable, and the B that’s in myBigChar
prints onscreen.If you’re tempted to write the following statement,
char myLittleChars = 'barry'; //Don't do this
please resist the temptation. You can’t store more than one letter at a time in a char
variable, and you can’t put more than one letter between a pair of single quotes. If you’re trying to store words or sentences (not just single letters), you need to use something called a String.
If you’re used to writing programs in other languages, you may be aware of something called ASCII character encoding. Most languages use ASCII; Java uses Unicode. In the old ASCII representation, each character takes up only 8 bits, but in Unicode, each character takes up 8, 16, or 32 bits. Whereas ASCII stores the letters of the Roman (English) alphabet, Unicode has room for characters from most of the world’s commonly spoken languages.
The only problem is that some of the Java API methods are geared specially toward 16-bit Unicode. Occasionally, this bites you in the back (or it bytes you in the back, as the case may be). If you’re using a method to writeHello
on the screen and H e l l o
shows up instead, check the method’s documentation for mention of Unicode characters.It’s worth noticing that the two methods, Character.toUpperCase
and System.out.println
, are used quite differently in the code above. The method Character.toUpperCase
is called as part of an initialization or an assignment statement, but the method System.out.println
is called on its own.
The boolean type
A variable of typeboolean
stores one of two values: true
or false
. The code below demonstrates the use of a boolean
variable.public class ElevatorFitter2 {
public static void main(String args[]) {
System.out.println("True or False?");
System.out.println("You can fit all ten of the");
System.out.println("Brickenchicker dectuplets");
System.out.println("on the elevator:");
System.out.println();
int weightOfAPerson = 150;
int elevatorWeightLimit = 1400;
int numberOfPeople = elevatorWeightLimit / weightOfAPerson;
<strong> boolean allTenOkay = numberOfPeople >= 10;</strong>
System.out.println(allTenOkay);
}
}
In this code, the allTenOkay
variable is of type boolean
. To find a value for the allTenOkay
variable, the program checks to see whether numberOfPeople
is greater than or equal to ten. (The symbols >= stand for greater than or equal to.)
At this point, it pays to be fussy about terminology. Any part of a Java program that has a value is an expression. If you write
weightOfAPerson = 150;
then 150 ,
is an expression (an expression whose value is the quantity 150
). If you write
numberOfEggs = 2 + 2;
then 2 + 2 is an expression (because 2 + 2
has the value 4
). If you write
int numberOfPeople = elevatorWeightLimit / weightOfAPerson;
then elevatorWeightLimit / weightOfAPerson
is an expression. (The value of the expression elevatorWeightLimit / weightOfAPerson
depends on whatever values the variables elevatorWeightLimit
and weightOfAPerson
have when the code containing the expression is executed.)
Any part of a Java program that has a value is an expression.
In the second set of code,numberOfPeople >= 10
is an expression. The expression’s value depends on the value stored in the numberOfPeople
variable. But, as you know from seeing the strawberry shortcake at the Brickenchicker family’s catered lunch, the value of numberOfPeople
isn’t greater than or equal to ten. As a result, the value of numberOfPeople >= 10
is false
. So, in the statement in the second set of code, in which allTenOkay
is assigned a value, the allTenOkay
variable is assigned a false
value.
In the second set of code, System.out.println()
is called with nothing inside the parentheses. When you do this, Java adds a line break to the program’s output. In the second set of code, System.out.println()
tells the program to display a blank line.