HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to JavaVariablesData TypesOperatorsStringsArraysLists (ArrayList)Conditions (if / switch)Loops (for / while)MethodsClasses (OOP)
REFERENCE
📖 Overview
🖥️ Output
📦 Variables
🔢 Numbers & Math
✍️ String
📋 Array & List
🔁 Condition & Loop
🧩 Method & Class
Java›Data Types

Data Types

int, double, boolean, char, String; type conversion (Integer.parseInt, casting).

Simple enough for a 5-year-old to understand

Basic types

Example code
1
public class Main {
2
public static void main(String[] args) {
3
int number = 42;
integer
Variableinteger
4
double decimal = 1.72;
decimal
Variabledecimal
5
boolean dogru = true;
true/false
Variabletrue/false
6
System.out.println(number + " " + decimal + " " + dogru);
42 1.72 true
Output / Input42 1.72 true
7
}
8
}
VariableOutput / Input

About this topic

Java's type system has two layers. Primitive types hold values directly and take up the least space in memory: int, long, double, float, boolean, char, byte, short. Object types, on the other hand, are references to an object and can be null.

Every primitive type has a wrapper class: Integer for int, Double for double. Since collections can only hold objects, when you put a number into a list, Java automatically wraps it. This auto-conversion provides convenience, but trying to convert a null wrapper to a primitive type produces an unexpected error.

There are practical rules for choosing types: int for integers, long for very large numbers, double for decimals, BigDecimal for money. float is almost never used because it is less precise than double and the memory savings are negligible.

What you will learn

  • Primitive types and the space they occupy
  • Wrapper classes and auto-conversion
  • Type casting operations and data loss
  • Why integer division does not return decimals
  • The necessity of using equals for Integer comparisons

When to use?

Type conversion is required when receiving data from the outside: Integer.parseInt or Double.parseDouble are used to convert text to a number. When the user enters a letter instead of a number, these methods throw an error, so they need to be called inside a try block.

Common mistakes

Wrong

int a = 7;
int b = 2;
System.out.println(a / b);

The division of two integers is also an integer; the result comes out as 3, not 3.5, and the decimal part is discarded. You do not get an error message, the calculation is silently wrong. To get a decimal result, at least one side needs to be a decimal.

Correct

int a = 7;
int b = 2;
System.out.println((double) a / b);

Wrong

Integer x = 1000;
Integer y = 1000;
System.out.println(x == y);

Double equals compares object references. Since Java caches small numbers, 100 may output True, while 1000 outputs False — the result is unpredictable. equals is used for value comparison.

Correct

Integer x = 1000;
Integer y = 1000;
System.out.println(x.equals(y));

Wrong

Integer value = null;
int total = value + 1;

Trying to convert a null Integer to a primitive int throws a "NullPointerException". Because auto-conversion hides this situation, the error appears in an unexpected place; it is frequently seen in numbers read from collections.

Correct

Integer value = null;
int total = (value == null ? 0 : value) + 1;

Frequently asked questions

When should I choose between int and long?

int counts up to about two billion; values exceeding this — timestamps, large ID numbers, total amounts — require long. When the limit is exceeded, Java does not throw an error, the number wraps around and becomes negative; this is a very hard bug to notice.

What is the difference between char and String?

char holds a single character and uses SINGLE quotes. String holds text and uses DOUBLE quotes. Mixing up quotes causes a compilation error; writing more than one character inside single quotes is also invalid.

Which type should I use for money calculations?

BigDecimal. Since double and float store decimals approximately, penny losses accumulate and lead to unacceptable discrepancies in financial calculations. BigDecimal is slower and more verbose to write, but where precision is required, there is no alternative.

Summary

In Java, primitive types hold values, object types hold references; wrapper classes come into play in collections. Three classic traps: integer division discarding the decimal, using double equals for Integer comparisons, and a null wrapper producing a NullPointerException.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code