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:

Asynchrony and Reactivity in Java: CompletableFuture, Flow, and Virtual Threads
In modern Java development, there are three main approaches to asynchrony and concurrency: CompletableFuture — for single asynchronous tasks. Flow / Reactive Streams — for data flows with backpressur...
Основы параллельности в Go для Java-разработчиков | Сoncurrency часть 1
Если вы Java-разработчик, привыкший к потокам и ExecutorService, Go предлагает более лёгкий и удобный подход к параллельной обработке — goroutine и каналы. В этой статье мы разберём ключевые концепции...
Synchronization and security in Go vs Java | Concurrency part 2
← Part 1 — Basics of Concurrency in Go for Java Developers In the second part, we will dive into synchronization and safety of concurrent code in Go. For a Java developer, it is useful to see t...

New Articles:

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. ...
Stream vs For in Java: how to write the fastest code possible
In Java, performance is often determined not by the "beauty of the code," but by how it interacts with memory, the JIT compiler, and CPU cache. Let s analyze why the usual for is often faster than Str...
Compiler, Build, and Tooling in Go and Java: how assembly, initialization, analysis, and diagnostics are organized in two ecosystems
This article is dedicated to a general overview of how the compiler, build, and tooling practices are arranged in Go, and how to better understand them through comparison with Java. We will not delve ...
Fullscreen image