Data Types in Java: The Complete Beginner's Guide
SkillVeris Team
Engineering Team

Java has eight primitive data types: byte, short, int, long, float, double, char, and boolean, each with a fixed size and range.
In this guide, you'll learn:
- Every variable in Java must be declared with a type before use, because Java is a statically typed language.
- Reference types like String, arrays, and custom classes store an address to an object rather than the value itself.
- Choosing the smallest type that safely fits your data saves memory in large-scale applications.
- Autoboxing lets Java automatically convert between primitives and their wrapper classes, such as int and Integer.
1What Are Data Types in Java?
A data type in Java specifies what kind of value a variable can store and how the compiler should interpret and allocate memory for it. Every variable must be given a type at declaration, which is why Java is called a statically typed language.
Data types fall into two broad categories: primitive types, which hold simple values directly, and reference types, which hold an address pointing to an object stored elsewhere in memory. Getting this distinction right is fundamental to writing correct, efficient Java code.
2The Eight Primitive Types
Java defines exactly eight primitive types, each with a fixed size that does not change across operating systems or hardware, which is part of what makes Java portable.
- byte: 8-bit signed integer, range -128 to 127, useful for saving memory in large arrays.
- short: 16-bit signed integer, range -32,768 to 32,767.
- int: 32-bit signed integer, the default choice for whole numbers in most programs.
- long: 64-bit signed integer, used for very large numbers; literals need an L suffix.
- float: 32-bit floating point number, used when memory matters more than precision.
- double: 64-bit floating point number, the default choice for decimal values.
- char: a single 16-bit Unicode character.
- boolean: holds only true or false, used for conditions and flags.
💡
3Reference Types Explained
Reference types do not store their value directly in the variable; instead, the variable holds a reference (essentially a memory address) to an object created elsewhere on the heap.
Strings, arrays, and any class you define, including custom objects, are reference types. This means assigning one reference variable to another copies the address, not the underlying data, which is why two variables can end up pointing at the same object.
Strings Are Special
String is technically a reference type, but Java treats string literals as immutable objects stored in a special memory area called the string pool, so identical literals are often shared rather than duplicated.
4Type Conversion and Casting
Java converts between compatible types either automatically or with an explicit cast, depending on whether information could be lost in the process.
Widening conversion happens automatically when a smaller type is assigned to a larger one, such as an int being assigned to a long, because no data can be lost. Narrowing conversion, such as assigning a double to an int, requires an explicit cast because precision or range may be lost.
- Widening (implicit): byte to short to int to long to float to double.
- Narrowing (explicit): requires a cast like (int) someDoubleValue, and truncates the decimal portion.
- Char to int conversions rely on the underlying Unicode code point of the character.
5Wrapper Classes and Autoboxing
Every primitive type has a corresponding wrapper class, such as Integer for int and Boolean for boolean, that lets the primitive behave like an object when one is required, for example inside collections like ArrayList.
Autoboxing is the automatic conversion Java performs between a primitive and its wrapper class, and unboxing is the reverse. This convenience comes with a small performance cost, so it is worth being deliberate about it in performance-critical loops.
6Choosing the Right Type for the Job
Pick the smallest and simplest type that safely covers the range of values your variable will ever hold, and default to int and double unless you have a specific reason to use something else.
- Use int for counters, indexes, and general whole numbers.
- Use long for identifiers, timestamps, or anything that could exceed about two billion.
- Use double for prices, measurements, and scientific calculations.
- Use boolean for flags and conditions, never a number as a substitute.
- Use String (not char) for any text longer than a single character.
7Common Mistakes with Java Data Types
A handful of recurring mistakes trip up newcomers learning Java's type system.
- Comparing objects like String or Integer with == instead of .equals(), which compares references rather than values.
- Forgetting the L suffix on long literals that exceed the int range, causing a compile error.
- Losing precision silently by narrowing a double to an int without realizing the decimal part is discarded.
- Overusing wrapper classes in tight loops, which adds unnecessary object overhead compared to primitives.
💡
8Wrap-Up and Next Steps
Java's type system is strict by design: every variable has a fixed type, and the compiler enforces it before your program ever runs, which catches whole categories of bugs early.
Once the eight primitives and the basics of reference types feel natural, the next step is practicing with arrays, collections, and custom classes to see how these types compose into larger programs. SkillVeris study notes on Java cover each type with worked examples if you want structured practice.
Get The Print Version
Download a PDF of this article for offline reading.
About the Publisher
SkillVeris Team
Engineering Team
Our engineering writers turn abstract code concepts into hands-on, project-driven learning experiences.
View all postsRelated Posts
Never miss an update
Get the latest tutorials and guides delivered to your inbox.
No spam. Unsubscribe anytime.