Java provides eight primitive data types: byte
, short
, int
, long
, float
, double
, char
, and boolean
. The data types byte
, short
, int
, and long
are for integers, and the data types float
and double
are for real numbers (numbers with decimal places).
The College Board Advanced Placement (AP) Examinations only require you to know about the int
, double
, and boolean
data types. This curriculum will, from time to time, also use the char
type when appropriate.
An integer is any positive or negative number without a decimal point.
7 -2 0 2025
A double is any signed or unsigned number with a decimal point. Doubles cannot contain a comma or any symbols. A double value can be written using scientific notation.
7.5 -66.72 0.125 5
$37,582.00 #5.0 10.72%
1625. = 1.625e3 .00125 = 1.25e-4
Note: When applying 5 to a double variable, Java will automatically add the decimal point for you.
The following table summarizes the bytes allocated and the resulting size.
Name | Size | Minimum Value | Maximum Value |
---|---|---|---|
byte |
1 byte | -128 | 127 |
short |
2 bytes | -32768 | 32767 |
int |
4 bytes | -2147483648 | 2147483647 |
long |
8 bytes | -9223372036854775808 | 9223372036854775807 |
float |
4 bytes | -3.40282347E+38 | 3.40282347E+38 |
double |
8 bytes | -1.79769313486231570E+308 | 1.79769313486231570E+308 |
Character type consists of letters, digits 0 through 9, and punctuation symbols. A character must be enclosed within single quotes.
'A', 'a', '8', '*'
Java characters are stored using 2 bytes according to the ASCII code. ASCII stands for American Standard Code for Information Interchange. See Handout A3.2, ASCII Characters - A Partial List
Using ASCII, the character value 'A' is actually stored as the integer value 65. Because a capital 'A' and the integer 65 are physically stored in the same fashion, this will allow us to easily convert from character to integer types, and vice versa.
In Java, you can make a direct assignment of a character value to an integer variable, and vice versa. This is possible because both an integer and a character variable are ultimately stored in binary. However, it is better to be more explicit about such conversions by using type conversions. For example, the two lines of code below assign to position the ASCII value of letter.
Programmers use the single quote to represent char data and double quotes to denote String types. To use either of those characters in a String literal, such as in a System.out.println statement, you must use an escape sequence. Java provides escape sequences for unusual keystrokes on the keyboard. Here is a partial list:
Character | Java Escape Sequence |
---|---|
Newline | '\n' |
Horizontal tab | '\t' |
Backslash | '\\' |
Single quote | '\'' |
Double quote | '\"' |
Null character | '\0' |
Data types are provided by high-level languages to minimize memory usage and processing time. Integers and characters require less memory and are easier to process. Floating-point values require more memory and time to process.
The last primitive data type is the type boolean
. It is used to represent a single *true
/ false
value,* and it can only hold one of these two values.
In a Java program, the reserved words
true
andfalse
always refer to theseboolean
values.
Last modified: December 12, 2022
Back to Identifiers In Java