- BASIC STRUCTURE OF A JAVA PROGRAM
- PRIMITIVE DATA TYPES AND EXPRESSIONS
- STATEMENTS
- SHORTCUT NOTATIONS
- ARRAYS
- STATIC METHODS
- APIs
- STRINGS
- INPUT AND OUTPUT
- BINARY SEARCH
- PERSPECTIVE
- Q&A
- EXERCISES
- CREATIVE PROBLEMS
- EXPERIMENTS
PRIMITIVE DATA TYPES AND EXPRESSIONS
A data type is a set of values and a set of operations on those values. We begin by considering the following four primitive data types that are the basis of the Java language:
- Integers, with arithmetic operations (int)
- Real numbers, again with arithmetic operations (double)
- Booleans, the set of values { true, false } with logical operations (boolean)
- Characters, the alphanumeric characters and symbols that you type (char)
Next we consider mechanisms for specifying values and operations for these types.
A Java program manipulates variables that are named with identifiers. Each variable is associated with a data type and stores one of the permissible data-type values. In Java code, we use expressions like familiar mathematical expressions to apply the operations associated with each type. For primitive types, we use identifiers to refer to variables, operator symbols such as + - * / to specify operations, literals such as 1 or 3.14 to specify values, and expressions such as (x + 2.236)/2 to specify operations on values. The purpose of an expression is to define one of the data-type values.
term |
examples |
definition |
|
primitive data type |
int double boolean char |
a set of values and a set of operations on those values (built in to the Java language) |
|
identifier |
a abc Ab$ a_b ab123 lo hi |
a sequence of letters, digits, _, and $, the first of which is not a digit |
|
variable |
[any identifier] |
names a data-type value |
|
operator |
+ - * / |
names a data-type operation |
|
literal |
int |
1 0 -42 |
source-code representation of a value |
double |
2.0 1.0e-15 3.14 |
||
boolean |
true false |
||
char |
'a' '+' '9' '\n' |
||
expression |
int |
lo + (hi - lo)/2 |
a literal, a variable, or a sequence of operations on literals and/or variables that produces a value |
double |
1.0e-15 * t |
||
boolean |
lo <= hi |
Basic building blocks for Java programs
To define a data type, we need only specify the values and the set of operations on those values. This information is summarized in the table below for Java’s int, double, boolean, and char data types. These data types are similar to the basic data types found in many programming languages. For int and double, the operations are familiar arithmetic operations; for boolean, they are familiar logical operations. It is important to note that +, -, *, and / are overloaded—the same symbol specifies operations in multiple different types, depending on context. The key property of these primitive operations is that an operation involving values of a given type has a value of that type. This rule highlights the idea that we are often working with approximate values, since it is often the case that the exact value that would seem to be defined by the expression is not a value of the type. For example, 5/3 has the value 1 and 5.0/3.0 has a value very close to 1.66666666666667 but neither of these is exactly equal to 5/3. This table is far from complete; we discuss some additional operators and various exceptional situations that we occasionally need to consider in the Q&A at the end of this section.
type |
set of values |
operators |
typical expressions |
|
expression |
value |
|||
int |
integers between |
+ (add) |
5 + 3 |
8 |
double |
double-precision real numbers |
+ (add) |
3.141 - .03 |
3.111 |
boolean |
true or false |
&& (and) |
true && false |
false |
char |
characters |
[arithmetic operations, rarely used] |
Primitive data types in Java
Expressions.
As illustrated in the table at the bottom of the previous page, typical expressions are infix: a literal (or an expression), followed by an operator, followed by another literal (or another expression). When an expression contains more than one operator, the order in which they are applied is often significant, so the following precedence conventions are part of the Java language specification: The operators * and / ( and %) have higher precedence than (are applied before) the + and - operators; among logical operators, ! is the highest precedence, followed by && and then ||. Generally, operators of the same precedence are applied left to right. As in standard arithmetic expressions, you can use parentheses to override these rules. Since precedence rules vary slightly from language to language, we use parentheses and otherwise strive to avoid dependence on precedence rules in our code.
Type conversion.
Numbers are automatically promoted to a more inclusive type if no information is lost. For example, in the expression 1 + 2.5 , the 1 is promoted to the double value 1.0 and the expression evaluates to the double value 3.5 . A cast is a type name in parentheses within an expression, a directive to convert the following value into a value of that type. For example (int) 3.7 is 3 and (double) 3 is 3.0. Note that casting to an int is truncation instead of rounding—rules for casting within complicated expressions can be intricate, and casts should be used sparingly and with care. A best practice is to use expressions that involve literals or variables of a single type.
Comparisons.
The following operators compare two values of the same type and produce a boolean value: equal (==), not equal (!=), less than (<), less than or equal (<=), greater than (>), and greater than or equal (>=). These operators are known as mixed-type operators because their value is boolean, not the type of the values being compared. An expression with a boolean value is known as a boolean expression. Such expressions are essential components in conditional and loop statements, as we will see.
Other primitive types.
Java’s int has 232 different values by design, so it can be represented in a 32-bit machine word (many machines have 64-bit words nowadays, but the 32-bit int persists). Similarly, the double standard specifies a 64-bit representation. These data-type sizes are adequate for typical applications that use integers and real numbers. To provide flexibility, Java has five additional primitive data types:
- 64-bit integers, with arithmetic operations (long)
- 16-bit integers, with arithmetic operations (short)
- 16-bit characters, with arithmetic operations (char)
- 8-bit integers, with arithmetic operations (byte)
- 32-bit single-precision real numbers, again with arithmetic operations (float)
We most often use int and double arithmetic operations in this book, so we do not consider the others (which are very similar) in further detail here.