Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Primitive Data Types | Basic Types, Operations
Java Basics
course content

Course Content

Java Basics

Java Basics

1. Getting Started
2. Basic Types, Operations
3. Loops
4. Arrays
5. String

book
Primitive Data Types

Variables

A variable in code is like a container where we can store information. For instance, if we need to store the number 20, we'll save it in a variable and refer to this number later by simply using the variable name in the code.

Java is a strongly typed language, meaning we must define the data type of our variable:

java

Main

copy
1
variable_type name = value;

The code above shows the basic syntax for declaring a variable.

Next, let's move on to studying data types, starting with the 8 main ones.

Data types

Let's go through primitive data types and learn more about each of them:

Byte

This integer data type can hold values ranging from -128 to 127. It takes up 1 byte of memory and is stored in the stack memory.

java

Main

copy
1
byte var = 10;

Short

The short data type is also an integer 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.

java

Main

copy
1
short var = 10000;

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.

java

Main

copy
1
int var = 123123123;

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.

java

Main

copy
1
long var = -10290394;

Float

The float data type is the first floating-point type that stores 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;.

java

Main

copy
1
float var = 13.41f;

Double

This floating-point data type can store up to 15 decimal digits. Unlike float, we don't need to specify letters when declaring a double variable. Double occupies 8 bytes and is stored in the stack memory.

java

Main

copy
1
double var = 12.299384;

Boolean

This primitive data type is special as it can only hold true or false values. In Java, the boolean type occupies 1 bit of memory, but in practice, it may take up more space because it can be packed into other data structures, such as arrays or objects (which we will explore later). Boolean values are stored in stack memory.

java

Main

copy
1
boolean var = true;

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.

java

Main

copy
1
char var = 'c';

There is also a table where you can view all the essential information about primitive data types:

Primitive data types can also be output using the console output command.

java

Main

copy
12345678
package com.example; public class Main { public static void main(String[] args) { int number = 41; System.out.println(number); } }

We declared an int variable and printed it on the screen.

java

Main

copy
12345678
package com.example; public class Main { public static void main(String[] args) { char character = 'c'; System.out.println(character); } }

We declared a char variable and printed it on the screen.

java

Main

copy
12345678
package com.example; public class Main { public static void main(String[] args) { float number = 1.12f; System.out.println(number); } }

We declared a float variable and printed it on the screen.

1. Which integer data type occupies the least amount of memory?

2. What data type can have only two values: true or false?

Which integer data type occupies the least amount of memory?

Which integer data type occupies the least amount of memory?

Select the correct answer

What data type can have only two values: ``true`` or ``false``?

What data type can have only two values: true or false?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 2. Chapter 1
We're sorry to hear that something went wrong. What happened?
some-alt