Primitive Data Types
![](https://codefinity-content-media.s3.eu-west-1.amazonaws.com/java+basics+/database-storage.png)
![](https://codefinity-content-media.s3.eu-west-1.amazonaws.com/java+basics+/internet.png)
![](https://codefinity-content-media.s3.eu-west-1.amazonaws.com/java+basics+/laptop.png)
![](https://codefinity-content-media.s3.eu-west-1.amazonaws.com/java+basics+/app-development.png)
![](https://codefinity-content-media.s3.eu-west-1.amazonaws.com/java+basics+/devops.png)
![](https://codefinity-content-media.s3.eu-west-1.amazonaws.com/java+basics+/java.png)
![](https://codefinity-content-media.s3.eu-west-1.amazonaws.com/java+basics+/games.png)
![](https://codefinity-content-media.s3.eu-west-1.amazonaws.com/java+basics+/database-storage.png)
![](https://codefinity-content-media.s3.eu-west-1.amazonaws.com/java+basics+/internet.png)
![](https://codefinity-content-media.s3.eu-west-1.amazonaws.com/java+basics+/laptop.png)
![](https://codefinity-content-media.s3.eu-west-1.amazonaws.com/java+basics+/app-development.png)
![](https://codefinity-content-media.s3.eu-west-1.amazonaws.com/java+basics+/devops.png)
![](https://codefinity-content-media.s3.eu-west-1.amazonaws.com/java+basics+/java.png)
![](https://codefinity-content-media.s3.eu-west-1.amazonaws.com/java+basics+/games.png)
Variables
A variable in code is a kind of cell where we can store information. For example, if we need to save the number 20
, we will save it in a variable and use this number again simply by using the variable name in the code.
![](https://codefinity-content-media-v2.s3.eu-west-1.amazonaws.com/courses/8204075c-f832-4cb9-88b1-4e24e74ebdcb/Java_Basics_imp_7%404x.png)
Java is a strongly typed language, so we are required to define the data type of our variable:
Main.java
The code above illustrates the basic syntax for declaring a variable.
Now, let's proceed to the study of data types, beginning with the 8 primary ones.
Data Types
![](https://codefinity-content-media.s3.eu-west-1.amazonaws.com/Java_OOP/Definition_1.png)
byte
, short
, int
, long
, float
, and double
. Textual data types are char
and String
, and boolean data types include boolean
.
Note
Note that the
String
data type is not a primitive data type, so we won't cover it in this section. It has a separate section where you will learn about this data type in detail.
Let's go through primitive data types and learn more about each of them:
byte
- This integer data type can accept values ranging from -128 to 127. It occupies 1 byte of memory and is stored in the stack memory;
Main.java
short
- is also an integer data type that can hold values ranging from -32,768 to 32,767. It occupies 2 bytes of memory and is stored in the stack memory;
Main.java
int
- This is the most commonly used integer data type, capable of holding values ranging from -2,147,483,648 to 2,147,483,647. It is also stored in the stack memory and occupies 4 bytes;
Main.java
long
- This is the largest integer data type, capable of storing values from -2⁶³ to 2⁶³ - 1. It occupies 8 bytes in memory and is also stored in the stack memory;
Main.java
float
- is the first floating-point data type to store 7 decimal digits. When declaring a variable of type float, we must remember to append the letter f at the end of the number. For example,float myFloat = 4.13f;
;
Main.java
double
- This floating-point data type can store up to 15 decimal digits. Unlikefloat
, we don't need to specify letters when declaring a double variable. Double occupies 8 bytes and is stored in the stack memory;
Main.java
boolean
- This primitive data type is special as it can only holdtrue
orfalse
values. Boolean is stored in the stack memory and occupies from 1 bit to 1 byte, depending on the system and compiler;
Main.java
char
- This character data type takes data from the ASCII table. We will delve into this data type in more detail in the next chapter, but for now, please note that it occupies 2 bytes.
Main.java
There is also a table where you can view all the essential information about primitive data types:
Data Type | Range | Size (in bytes) | Memory Location |
---|---|---|---|
byte | -128 to 127 | 1 | Stack |
short | -32,768 to 32,767 | 2 | Stack |
int | -2³¹ to 2³¹ - 1 | 4 | Stack |
long | -2⁶³ to 2⁶³-1 | 8 | Stack |
float | -2⁻¹⁴⁹ to (2 - 2⁻²³) * 2¹²⁷ | 4 | Stack |
double | -2⁻¹⁰⁷⁴ to (2 - 2⁻⁵²) * 2¹⁰²³ | 8 | Stack |
boolean | true or false | 1 bit to 1 byte | Stack |
char | 0 to 2¹⁶ - 1 | 2 | Stack |
Primitive data types can also be output using the console output command. Let's examine some examples:
Main.java
We declared an int
variable and printed it on the screen.
Main.java
We declared a char
variable and printed it on the screen.
Main.java
We declared a float
variable and printed it on the screen.
Note
We don't need to wrap variables in double quotation marks as we did when outputting text using
System.out.println()
.
![](https://codefinity-content-media.s3.eu-west-1.amazonaws.com/java+basics+/database-storage.png)
![](https://codefinity-content-media.s3.eu-west-1.amazonaws.com/java+basics+/internet.png)
![](https://codefinity-content-media.s3.eu-west-1.amazonaws.com/java+basics+/laptop.png)
![](https://codefinity-content-media.s3.eu-west-1.amazonaws.com/java+basics+/app-development.png)
![](https://codefinity-content-media.s3.eu-west-1.amazonaws.com/java+basics+/devops.png)
![](https://codefinity-content-media.s3.eu-west-1.amazonaws.com/java+basics+/java.png)
![](https://codefinity-content-media.s3.eu-west-1.amazonaws.com/java+basics+/games.png)
![](https://codefinity-content-media.s3.eu-west-1.amazonaws.com/java+basics+/database-storage.png)
![](https://codefinity-content-media.s3.eu-west-1.amazonaws.com/java+basics+/internet.png)
![](https://codefinity-content-media.s3.eu-west-1.amazonaws.com/java+basics+/laptop.png)
![](https://codefinity-content-media.s3.eu-west-1.amazonaws.com/java+basics+/app-development.png)
![](https://codefinity-content-media.s3.eu-west-1.amazonaws.com/java+basics+/devops.png)
![](https://codefinity-content-media.s3.eu-west-1.amazonaws.com/java+basics+/java.png)
![](https://codefinity-content-media.s3.eu-west-1.amazonaws.com/java+basics+/games.png)
Tudo estava claro?
Conteúdo do Curso
Java Basics
Java Basics
Primitive Data Types
![](https://codefinity-content-media.s3.eu-west-1.amazonaws.com/java+basics+/database-storage.png)
![](https://codefinity-content-media.s3.eu-west-1.amazonaws.com/java+basics+/internet.png)
![](https://codefinity-content-media.s3.eu-west-1.amazonaws.com/java+basics+/laptop.png)
![](https://codefinity-content-media.s3.eu-west-1.amazonaws.com/java+basics+/app-development.png)
![](https://codefinity-content-media.s3.eu-west-1.amazonaws.com/java+basics+/devops.png)
![](https://codefinity-content-media.s3.eu-west-1.amazonaws.com/java+basics+/java.png)
![](https://codefinity-content-media.s3.eu-west-1.amazonaws.com/java+basics+/games.png)
![](https://codefinity-content-media.s3.eu-west-1.amazonaws.com/java+basics+/database-storage.png)
![](https://codefinity-content-media.s3.eu-west-1.amazonaws.com/java+basics+/internet.png)
![](https://codefinity-content-media.s3.eu-west-1.amazonaws.com/java+basics+/laptop.png)
![](https://codefinity-content-media.s3.eu-west-1.amazonaws.com/java+basics+/app-development.png)
![](https://codefinity-content-media.s3.eu-west-1.amazonaws.com/java+basics+/devops.png)
![](https://codefinity-content-media.s3.eu-west-1.amazonaws.com/java+basics+/java.png)
![](https://codefinity-content-media.s3.eu-west-1.amazonaws.com/java+basics+/games.png)
Variables
A variable in code is a kind of cell where we can store information. For example, if we need to save the number 20
, we will save it in a variable and use this number again simply by using the variable name in the code.
![](https://codefinity-content-media-v2.s3.eu-west-1.amazonaws.com/courses/8204075c-f832-4cb9-88b1-4e24e74ebdcb/Java_Basics_imp_7%404x.png)
Java is a strongly typed language, so we are required to define the data type of our variable:
Main.java
The code above illustrates the basic syntax for declaring a variable.
Now, let's proceed to the study of data types, beginning with the 8 primary ones.
Data Types
![](https://codefinity-content-media.s3.eu-west-1.amazonaws.com/Java_OOP/Definition_1.png)
byte
, short
, int
, long
, float
, and double
. Textual data types are char
and String
, and boolean data types include boolean
.
Note
Note that the
String
data type is not a primitive data type, so we won't cover it in this section. It has a separate section where you will learn about this data type in detail.
Let's go through primitive data types and learn more about each of them:
byte
- This integer data type can accept values ranging from -128 to 127. It occupies 1 byte of memory and is stored in the stack memory;
Main.java
short
- is also an integer data type that can hold values ranging from -32,768 to 32,767. It occupies 2 bytes of memory and is stored in the stack memory;
Main.java
int
- This is the most commonly used integer data type, capable of holding values ranging from -2,147,483,648 to 2,147,483,647. It is also stored in the stack memory and occupies 4 bytes;
Main.java
long
- This is the largest integer data type, capable of storing values from -2⁶³ to 2⁶³ - 1. It occupies 8 bytes in memory and is also stored in the stack memory;
Main.java
float
- is the first floating-point data type to store 7 decimal digits. When declaring a variable of type float, we must remember to append the letter f at the end of the number. For example,float myFloat = 4.13f;
;
Main.java
double
- This floating-point data type can store up to 15 decimal digits. Unlikefloat
, we don't need to specify letters when declaring a double variable. Double occupies 8 bytes and is stored in the stack memory;
Main.java
boolean
- This primitive data type is special as it can only holdtrue
orfalse
values. Boolean is stored in the stack memory and occupies from 1 bit to 1 byte, depending on the system and compiler;
Main.java
char
- This character data type takes data from the ASCII table. We will delve into this data type in more detail in the next chapter, but for now, please note that it occupies 2 bytes.
Main.java
There is also a table where you can view all the essential information about primitive data types:
Data Type | Range | Size (in bytes) | Memory Location |
---|---|---|---|
byte | -128 to 127 | 1 | Stack |
short | -32,768 to 32,767 | 2 | Stack |
int | -2³¹ to 2³¹ - 1 | 4 | Stack |
long | -2⁶³ to 2⁶³-1 | 8 | Stack |
float | -2⁻¹⁴⁹ to (2 - 2⁻²³) * 2¹²⁷ | 4 | Stack |
double | -2⁻¹⁰⁷⁴ to (2 - 2⁻⁵²) * 2¹⁰²³ | 8 | Stack |
boolean | true or false | 1 bit to 1 byte | Stack |
char | 0 to 2¹⁶ - 1 | 2 | Stack |
Primitive data types can also be output using the console output command. Let's examine some examples:
Main.java
We declared an int
variable and printed it on the screen.
Main.java
We declared a char
variable and printed it on the screen.
Main.java
We declared a float
variable and printed it on the screen.
Note
We don't need to wrap variables in double quotation marks as we did when outputting text using
System.out.println()
.
![](https://codefinity-content-media.s3.eu-west-1.amazonaws.com/java+basics+/database-storage.png)
![](https://codefinity-content-media.s3.eu-west-1.amazonaws.com/java+basics+/internet.png)
![](https://codefinity-content-media.s3.eu-west-1.amazonaws.com/java+basics+/laptop.png)
![](https://codefinity-content-media.s3.eu-west-1.amazonaws.com/java+basics+/app-development.png)
![](https://codefinity-content-media.s3.eu-west-1.amazonaws.com/java+basics+/devops.png)
![](https://codefinity-content-media.s3.eu-west-1.amazonaws.com/java+basics+/java.png)
![](https://codefinity-content-media.s3.eu-west-1.amazonaws.com/java+basics+/games.png)
![](https://codefinity-content-media.s3.eu-west-1.amazonaws.com/java+basics+/database-storage.png)
![](https://codefinity-content-media.s3.eu-west-1.amazonaws.com/java+basics+/internet.png)
![](https://codefinity-content-media.s3.eu-west-1.amazonaws.com/java+basics+/laptop.png)
![](https://codefinity-content-media.s3.eu-west-1.amazonaws.com/java+basics+/app-development.png)
![](https://codefinity-content-media.s3.eu-west-1.amazonaws.com/java+basics+/devops.png)
![](https://codefinity-content-media.s3.eu-west-1.amazonaws.com/java+basics+/java.png)
![](https://codefinity-content-media.s3.eu-west-1.amazonaws.com/java+basics+/games.png)
Tudo estava claro?