Primitive Data Types
Estimated time to read: 2 minutes
Non-Object Data in Java¶
Java users several types of constructs
- Outermost Blocks
- Classes
- Interfaces
- Annotations
- Enums
- Other types
- Arrays
- Primitives
You can have no more than one public structure in a file with the file name, matching, the public structure
Primitive Data Types¶
Primitives are simply variables to hold data
Java provides several primitive data types, there are 8 in total, they are:
Data | Type Size | Description |
---|---|---|
byte | 1 byte (8 bits) | Stores whole numbers from -128 to 127 |
short | 2 bytes | Stores whole numbers from -32,768 to 32,767 |
int | 4 bytes | Stores whole numbers from -2,147,483,648 to 2,147,483,647 |
long | 8 bytes | Stores whole numbers from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
float | 4 bytes | Stores fractional numbers. Sufficient for storing 6 to 7 decimal digits |
double | 8 bytes | Stores fractional numbers. Sufficient for storing 15 decimal digits |
boolean | 1 bit | Stores true or false values |
char | 2 bytes | 160bit Unicode. Stores a single character/letter or ASCII values |
Integer Types¶
The integer types in Java are byte, short, in and long
Arithmetic expressions can throw exceptions for circumstances such as a divide by zero.
The compiler assumes literals are integer values; to declare a long literal you would append 'L'. long myLong = 23456L
Real Number Types¶
Real numbers are represented by float and double types. Real number literals default to double Real numbers also include special values for infinity and NaN
To declare a float, append 'F'. float myFloat = 234.567F
To declare a long, append 'L' `long myLong = 1231345235252524L'