1. What kinds of numbers are there?
In programming we constantly work with numbers — from a user’s age to the number of stars in a galaxy or cents in a bank account. But different tasks require different kinds of numbers: sometimes you only need integers, sometimes very precise fractions, and sometimes you need numbers that are non‑negative.
Integers (int and others)
An integer is a number without a fractional part.
In Java the most commonly used type is int, but there are several other options that differ in size and range.
- int — the main “workhorse” type for integers. It holds very large (and very small) values while not using much memory. For example, counting likes, age, or the number of days in a year will almost always use int.
- long — used when values can be extremely large (for example, billions or trillions). Example: storing the total number of YouTube views in history.
- short — a memory‑saving but rarely used type. Suitable where there are many repeated small values (for example, audio data, color components).
- byte — for even smaller values, especially in graphics and file I/O where saving memory matters.
Floating-point numbers (double, float)
Sometimes you need numbers with a fractional part. For example, air temperature, a student’s GPA, or a product price with cents.
- double — the default type for fractional numbers. Accurate enough for most calculations (example: 3.1415926535…).
- float — a lighter and less accurate type, more common in computer graphics or when processing large arrays of data where saving memory is important.
Special numeric types
- BigInteger — for working with huge integers that exceed standard ranges, for example in cryptography or astronomical calculations.
- BigDecimal — a class for exact decimal arithmetic (finance, interest calculations); it helps avoid the inaccuracies inherent in double and float.
2. Value ranges
Integral types can store both positive and negative numbers. This is convenient when values may be above or below zero: for example, temperature, account balance, or elevation relative to sea level.
Example:
- int : -10, 0, 50
- long : 0, 10, -1000_000_000_000_000_000L
Integer types
| Type | Size | Value range | Example values |
|---|---|---|---|
|
1 byte | -128 to 127 | -128, 0, 127 |
|
2 bytes | -32,768 to 32,767 | -1000, 0, 32000 |
|
4 bytes | -2,147,483,648 to 2,147,483,647 | -1000000, 0, 2000000 |
|
8 bytes | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
-10_000_000_000, 1 |
Floating-point types
| Type | Size | Example values | Description |
|---|---|---|---|
|
4 bytes | 3.14f, -0.001f | single precision (7 digits) |
|
8 bytes | 3.1415, -1.7E+308 | double precision (15–16 digits) |
3. Numeric suffixes
In some situations you need to explicitly specify which type you want to use for a literal. You do this with suffixes:
- L or l — for type long (for example, 10000000000L)
- F or f — for type float (for example, 3.14f)
- D or d — for type double (usually unnecessary because floating‑point literals without a suffix are double)
If you don’t specify a suffix, an integer literal is int by default, and a fractional literal is double. For example, 42 is an int, and 3.14 is a double.
Why is this useful? For example, to avoid an error when assigning a large literal to a variable of type long:
long bigNumber = 9000000000L; // if you remove L, there will be a compilation error
Underscore separator _
When numbers are long, it’s easy to get lost in the zeros. For readability, Java lets you use underscores inside numeric literals.
int population = 146_700_000;
long stars = 100_000_000_000L;
This is perfectly legal: the compiler ignores underscores, and at a glance you can see the digit groups. The main thing — don’t put an underscore at the beginning or end, after a decimal point, or before a suffix.
4. The char type: what is it and why?
The char type is used to store a single character: a letter, digit, punctuation mark, space, special symbol, or even a smiley.
Examples:
char letter = 'A';
char digit = '7';
char symbol = '?';
char cyrillic = '\u0416';
char euro = '€';
char smile = '☺'; // Yes, that’s possible too!
Important rules:
- A char literal is written in single quotes: 'A', '7', '#'.
- It’s exactly one character! If you write 'AB', the compiler will complain.
- Every character has its own numeric code (Unicode).
Unicode: characters from around the world
Java uses the Unicode standard, which allows you to store not only Latin letters but also Cyrillic, ideographs, emoji, and even ancient Egyptian hieroglyphs (if you ever need them).
Fun fact:
The char type is essentially a 16‑bit number (from 0 to 65535), where each value maps to a specific character.
5. Characters and numbers: the Unicode link
Since every character is a number under the hood, we can freely convert a char to an int and back.
Example: get a character’s Unicode code
public class CharToInt {
public static void main(String[] args) {
char ch = 'A';
int code = ch; // Implicit conversion char → int
System.out.println("Code of character '" + ch + "': " + code);
}
}
Result:
Code of character 'A': 65
And now the reverse — get a character by its code:
public class IntToChar {
public static void main(String[] args) {
int code = 1040; // Code of the letter '\u0410' in Unicode (Cyrillic)
char ch = (char) code; // Explicit conversion int → char
System.out.println("Character with code " + code + ": " + ch);
}
}
Result:
Character with code 1040: \u0410
6. Core primitive types in Java
| Type | Size (bits) | Value range | Example value | Note |
|---|---|---|---|---|
|
8 | -128 ... 127 | 42 | Used very rarely |
|
16 | -32,768 ... 32,767 | 12345 | For memory savings |
|
32 | -2,147,483,648 ... 2,147,483,647 | 1000000 | Primary for integers |
|
64 | ≈ −9.22 × 1018 ... ≈ 9.22 × 1018 | 5000000000L | For very large integers |
|
32 | ~±3.4 × 10-38 ... ±3.4 × 1038 | 3.14f | Floating-point numbers, low precision |
|
64 | ~±1.7 × 10-308 ... ±1.7 × 10308 | 2.718 | Floating-point numbers, high precision |
|
16 | 0 ... 65,535 (Unicode) | '\u0416', 'A', '?' | Single character |
|
1 | true, false | true | Boolean value |
GO TO FULL VERSION