☕️ Primitive Types
5 min read > Topic Docs > Katelyn Lee > 4/2/22
To understand what a data type is in the first place, we need to
understand what a bit and variable is. A bit is the
smallest unit of data a
computer can handle (stands for binary digit). A byte contains 8 bits. A variable
is a placeholder for any type of data, or a sort of container that holds a value in
memory.
Primitive variables are one of the two types of
variables used in Java
programming which contain the value of a data type. A primitive variable’s
declared data
type is a primitive data type. A data type tells the computer how much memory is
needed
to store data.
Example: int numberOfMembers
→ int is the primitive data type, numberOfMembers is a primitive
variable that stores the “number of members”
There are four primitive data types: integer,
floating-point, boolean, and char →
these types represent raw values.
INTEGERS
Bytes allocate 8 bits and represent values of integers 128 to 127. Short allocates 16 bits and represents values of integers 32,768 to 32,767. Int allocates 32 bits (or 4 bytes) of space in memory and represents values of integers -231 to 231 -1. Long allocates 64 bits and represents integers -263 to 263 - 1
FLOATING POINT NUMBERS
Boolean values allocate 8 bits or 1 byte and represent either true or false. Due to the two possible values of a boolean, they only need one bit, but variables cannot be stored if they are smaller than 1 byte. Thus, the remaining 7 bits are stuffed.
BOOLEANS
Floating point numbers, such as double and float, allow decimal numbers. Float allocates 32 bits and can represent up to 7 decimal places, whereas double allocates 64 bits for the ability to represent up to 16 decimal points. Syntax unique to float is adding “f” to the end of the value, as otherwise it will be treated as a double.
CHARACTERS
Character types called char allocate 16 bits and hold the value of a single character or digit, such as a letter, number, or symbol. They are surrounded by single quotes. You should always choose the smallest data type needed when choosing the type for your variables to speed up your computer program and use less memory.