1.2 Getting Started with Java Primitive Types
22/05/25
10 mins
Getting Started with Java Primitive Types: The Basics You Need to Know
When you begin learning Java, one of the first and most important concepts you'll encounter is primitive types. These are the basic building blocks of data in every Java program. Let's walk through what primitive types are, how to use them, and some handy tips for writing cleaner code—all explained simply and based on the official Java documentation.
What Are Primitive Types in Java?
In Java, a primitive type is a basic data type that's built into the language itself. You don't need to create an object to use them, and they hold simple values directly (like numbers or characters), not references to objects or other data.
There are eight primitive types in Java:
Type | Size | Example Values | Typical Use |
---|---|---|---|
byte | 8-bit integer | -128 to 127 | Small integers/arrays |
short | 16-bit integer | -32,768 to 32,767 | Medium-sized numbers |
int | 32-bit integer | -2,147,483,648 to 2,147,483,647 | Standard integers |
long | 64-bit integer | Large range of integers | Huge numbers |
float | 32-bit | Decimals (6-7 digits) | Less-precision decimals |
double | 64-bit | Decimals (15-16 digits) | Default for decimals |
boolean | 1 bit | true/false | Logic, flags |
char | 16-bit | Single Unicode character | Letters, symbols |
How to Declare Primitive Types
Java is a statically-typed language. This means you must declare the type of every variable before you use it. Here's the basic format:
int gear = 1;
int
is the typegear
is the variable name1
is the value
This tells Java to create a place in memory for a number and label it gear
.
More About Each Primitive Type
byte: 8 bits, from -128 to 127. Saves memory when used in large arrays.
short: 16 bits, from -32,768 to 32,767. Like byte
, used for saving memory in arrays.
int: 32 bits, from −2,147,483,648 to 2,147,483,647. Most common integer type.
long: 64 bits, for much larger values. Add L
at the end for long literals, e.g. long big = 123L;
.
float: 32 bits, decimal numbers. Use for less precise values. Add f
at the end for float literals: float pi = 3.14f;
.
double: 64 bits, more precise decimal numbers. This is Java's default decimal type.
boolean: Holds true
or false
, perfect for yes/no and flag situations. Size is not precisely defined, but typically one bit.
char: 16 bits, single Unicode character (like 'A'
, '9'
, or '\n'
for newline).
Literals and How to Assign Values
A literal is a fixed value directly in your code:
int num = 100; // integer literal char c = 'C'; // character literal boolean flag = true; // boolean literal
Numeric Literals
You can use different number systems:
- Decimal (base 10):
int dec = 26;
- Hexadecimal (base 16):
int hex = 0x1A;
- Binary (base 2):
int bin = 0b11010;
(Java 7 and later)
Floating Points
By default, decimals are double
. Use f
to mark them as float
:
double d1 = 123.4; float f1 = 123.4f; double d2 = 1.234e2; // Scientific notation
Default Values
If you don't assign a value to a field (a class-level variable), Java gives it a default:
Data Type | Default Value |
---|---|
byte | 0 |
short | 0 |
int | 0 |
long | 0L |
float | 0.0f |
double | 0.0d |
char | '\u0000' |
boolean | false |
Local variables (those inside methods) do not get a default value! You must assign them before use, or Java will give you a compile-time error.
Underscores in Numeric Literals
For readability (Java 7+), you can put underscores in numbers:
int million = 1_000_000; long creditCard = 1234_5678_9012_3456L;
But you can't start or end a literal with an underscore, put it next to a decimal point, or before a suffix (L
, f
).
Special Notes
- There is also a special
null
literal for reference types (not for primitives). This is often used to show a variable doesn't point to any object. - The
String
type, while commonly used like a primitive, is actually a class in Java, but has special language support.
Wrapping Up
Primitive types are at the heart of Java programming. They store the simplest, most efficient forms of data—numbers, characters, and truth values—directly in memory. Knowing how and when to use each one will make your code faster, safer, and easier to understand.
Happy coding!
PREVIOUS: 1.1 A Beginner’s Guide to Java Variables