OCA Java SE 8 Mock Exam Review - Java Primitive Data Types Review








Ranges of values stored by the numeric Java primitive data types

Data typeSizeRange of values
byte8 bits-128 to 127, inclusive
short16 bits-32,768 to 32,767, inclusive
int32 bits-2,147,483,648 to 2,147,483,647, inclusive
long64 bits-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807, inclusive

Integer literal values come in four flavors: binary, decimal, octal, and hexadecimal.

Binary number uses only 2 digits, 0 and 1.

Octal number uses digits 0 through 7 (a total of 8 digits). Here the decimal number 8 is represented as octal 10, decimal 9 as 11, and so on.

Decimal number is based on 10 digits, from 0 through 9 (a total of 10 digits).

Hexadecimal number uses digits 0 through 9 and the letters A through F (a total of 16 digits and letters).

Here the number 10 is represented as A, 11 as B, 12 as C, 13 as D, 14 as E, and 15 as F.





Primitive data types

Java defines eight primitive data types: char, byte, short, int, long, float, double, and boolean.

Primitive data types are the simplest data types.

Primitive data types are predefined by the programming language. A user can't define a primitive data type in Java.

It's helpful to categorize the primitive data types as Boolean, numeric, and char- acter data types.

The Boolean data type

The boolean data type stores data with only two possible values. The actual values that a boolean can store are true and false.

true and false are literal values.

Numeric data types

Numeric values can be stored either as integers or decimal numbers.

byte, short, int, and long can be used to store integers.

The byte, short, int, and long data types use 8, 16, 32, and 64 bits, respectively, to store their values.

float and double can be used to store decimal numbers.

The float and double data types use 32 and 64 bits, respectively, to store their values.

The default type of integers-that is, nondecimal numbers-is int.

To designate an integer literal value as a long value, add the suffix L or l to the literal value.

Numeric values can be stored in binary, octal, decimal, and hexadecimal number formats.

Literal values in the decimal number system use digits from 0 to 9 (a total of 10 digits).

Literal values in the octal number system use digits from 0 to 7 (a total of 8 digits).

Literal values in the hexadecimal number system use digits from 0 to 9 and letters from A to F (a total of 16 digits and letters).

Literal values in the binary number system use digits 0 and 1 (a total of 2 digits).

The literal values in the octal number system start with the prefix 0.

The literal values in the hexadecimal number system start with the prefix 0x.

The literal values in the binary number system start with the prefix 0b or 0B.

We can use underscores within the Java literal values to make them more readable. 0B1_0000_10_11, 0_413, and 0x10_B are valid binary, octal, and hexadecimal literal values.

The default type of a decimal number is double.

To designate a decimal literal value as a float value, add the suffix F or f to the literal value.

The suffixes D and d can be used to mark a literal value as a double value.





Character primitive data types

A char data type can store a single 16-bit Unicode character.

You can use values from \u0000 (or 0) to a maximum of \uffff (or 65,535 inclusive) to store a char.

Unicode values are defined in the hexadecimal number system.

When you assign a letter to a char, Java stores its integer equivalent value.

Single quotes are used to assign a letter to a char variable.