Data types in Java

Data Types in Java

Hello! This is Vitaly Lesnykh. In this lesson of the "Java Basics for Beginners" course, we will discuss what data types are. Data types are the foundation of any programming language. They allow Java to understand what information we are storing and what operations we can perform on it.

In Java, every object and variable has a specific type. The data type defines the size of memory, the range of values and the available operations.

Primitive Data Types

In Java, there are 8 primitive data types. They are divided into four groups:

  • Boolean (boolean)
  • Integer (byte, short, int, long)
  • Floating Point (float, double)
  • Character (char)

Table of Primitive Types

Type Size (bit) Value Range Example
boolean 1 true or false boolean isActive = true;
byte 8 -128 ... 127 byte age = 25;
short 16 -32,768 ... 32,767 short year = 2025;
int 32 -2,147,483,648 ... 2,147,483,647 int distance = 10000;
long 64 -9,223,372,036,854,775,808 ... 9,223,372,036,854,775,807 long population = 7900000000L;
float 32 approximately ±3.4e38 float price = 19.99f;
double 64 approximately ±1.7e308 double pi = 3.1415926535;
char 16 one Unicode character char letter = 'A';

Logical Data Type

The boolean type is used to store logical values true (true) and false (false). It is often used in conditions and loops.


boolean isJavaFun = true;
if (isJavaFun) {
    System.out.println("Да, Java — это весело!");
}
  

Integer Types

To store whole numbers without a fractional part, the types byte, short, int and long are used. They differ in memory size and value range.


byte small = 10;
int medium = 1000;
long big = 10000000000L;

System.out.println(small + ", " + medium + ", " + big);
  

Floating Point Types

To store numbers with fractional parts, the float and double types are used. In most cases, double is used as it provides higher precision.


float weight = 72.5f;
double height = 1.82;
System.out.println("Weight: " + weight + " kg, Height: " + height + " m");
  

Character type

The type char stores a single character in Unicode encoding. This can be not only a Latin letter, but also a digit, sign, or even a character from another script.


char letter = 'Ж';
System.out.println("Letter: " + letter);
  

Type Conversion

Sometimes it is necessary to convert one type to another. For example, from int to double or vice versa. Java does this either automatically (implicit conversion) or manually (explicit conversion).


// Implicit conversion (int -> double)
int a = 5;
double b = a; // b = 5.0

// Explicit conversion (double -> int)
double x = 9.99;
int y = (int) x; // y = 9
  
It is important to remember: when converting types with loss of precision (for example, from double to int) the fractional part is discarded.

Homework

To reinforce the topic of data types, complete the following exercises:

  1. Create variables of all eight primitive types and assign values to them. Output them to the console.
  2. Try converting values between the types int, double and char.
  3. Create a boolean variable and use it in an if condition.
  4. Write a program that calculates the average of three numbers of type double.
  5. Try to exceed the range of the byte type and see what happens.
These tasks will help understand how memory works and the ranges of values in Java. The deeper you delve into types, the more reliable your code will be.

This was Vitaly Lesnykh with you. Subscribe to the channel and continue learning Java step by step!

Test — How well did you understand the lesson?


🌐 На русском
Total Likes:0

Оставить комментарий

My social media channel
By sending an email, you agree to the terms of the privacy policy

Useful Articles:

Go ↔ Java: Complete Guide to Runtime, Memory, and Allocator - Part 3
This article is a comprehensive guide to the key aspects of memory and runtime work in Go and Java. We will discuss fundamental concepts: execution scheduler, memory barriers, memory alignment, stack ...
Go vs. Java - Comparing Memory Models - Part 2: Atomic Operations, Preemption, Defer/Finally, Context, Escape Analysis, GC, False Sharing
Atomic operations Atomic operations ensure correct execution of variable operations without race conditions, guaranteeing a happens-before between reads and writes. Go example: import "sync/atomic" va...
Scheduler internals in Go ↔ Java: how your code is actually executed
When you write go func() or create a Thread in Java, it seems like you are managing concurrency. But in reality, you are passing the task to the scheduler. And this is where the real show begins. Go...

New Articles:

Concurrency is not about “starting many threads”. It’s about agreements between them. Imagine a restaurant kitchen: — cooks (threads / goroutines) — orders (tasks) — and the main question: how do th...
When HashMap starts killing production: the engineering story of ConcurrentHashMap
Imagine a typical production service. 32 CPU hundreds of threads configuration / session / rate limits cache tens of thousands of operations per second And somewhere inside — a regular Map. At first...
Zero Allocation in Java: what it is and why it matters
Zero Allocation — is an approach to writing code in which no unnecessary objects are created in heap memory during runtime. The main idea: fewer objects → less GC → higher stability and performance. ...
Fullscreen image