Java Basics
Data Types in Java
A data type determines which values a variable can hold and which operations Java permits on those values. Java has eight primitive types and a rich system of reference types such as classes, interfaces, arrays, records, enums, and type variables.
Quick answer
What are data types in Java?
Java data types classify values. The eight primitive types are byte, short, int, long, char, float, double, and boolean. Reference types describe objects and arrays and hold reference values, including the special value null.
What you will learn
- The difference between primitive and reference types.
- The exact ranges of Java integral types.
- How floating-point, character, and boolean values behave.
- Which default values apply to fields and array components.
- How literals, suffixes, wrapper classes, and type inference work.
- How to choose a suitable type for common programming tasks.
Before you begin
Review Java Variables. Every variable has a compile-time type, and local variables must be definitely assigned before their values are read.
Java type-system overview
Java is statically typed: the compiler checks whether values and operations are compatible with their declared types. The two broad families are primitive types and reference types.
The eight primitive data types
Primitive types are predefined by the Java language. Their values are not objects, although wrapper classes can represent primitive values as objects.
| Type | Value model | Default field value | Range or values |
|---|---|---|---|
byte | 8-bit signed integer | 0 | -128 to 127 |
short | 16-bit signed integer | 0 | -32,768 to 32,767 |
int | 32-bit signed integer | 0 | -2,147,483,648 to 2,147,483,647 |
long | 64-bit signed integer | 0L | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
char | 16-bit unsigned UTF-16 code unit | '\u0000' | '\u0000' to '\uffff' (0 to 65,535) |
float | 32-bit IEEE 754 binary floating point | 0.0f | Finite values, infinities, signed zeros, and NaN |
double | 64-bit IEEE 754 binary floating point | 0.0d | Finite values, infinities, signed zeros, and NaN |
boolean | Logical value | false | true or false |
Do not assign a “1-bit size” to boolean
The Java language defines the values and operations of boolean, but it does not define a general storage size for a boolean variable. The JVM or data structure may represent boolean values differently.
Integral types: byte, short, int, long, and char
The integral types represent exact integer values. int is the normal choice for whole-number arithmetic, while long is used when the required range exceeds int.
public class IntegralTypes {
public static void main(String[] args) {
byte temperature = 28;
short year = 2026;
int population = 1_500_000;
long distance = 9_876_543_210L;
char grade = 'A';
System.out.println(population);
System.out.println(distance);
System.out.println(grade);
}
}Underscores can improve the readability of numeric literals. A long literal that exceeds the int range requires the L suffix.
Floating-point types: float and double
float and double represent approximate binary floating-point values. double is the usual choice because it provides greater precision. A float literal requires an f or F suffix.
float discount = 12.5f;
double pi = 3.141592653589793;
Money and exact decimal arithmetic
Binary floating-point types cannot exactly represent many decimal fractions. For financial values that require controlled decimal precision and rounding, use BigDecimal with an explicit rounding policy.
The boolean type
A boolean value is either true or false. Java does not treat numeric values such as 0 and 1 as boolean values.
boolean loggedIn = true;
if (loggedIn) {
System.out.println("Welcome");
}Default values of fields and array components
Instance fields, static fields, and array components receive default values. Local variables do not receive a value that can be read automatically; they must be definitely assigned first.
public class DefaultValues {
int number;
double price;
char letter;
boolean active;
String name;
public static void main(String[] args) {
DefaultValues value = new DefaultValues();
System.out.println(value.number);
System.out.println(value.price);
System.out.println((int) value.letter);
System.out.println(value.active);
System.out.println(value.name);
}
}Output
0
0.0
0
false
null
The character default is the null character '\u0000', which is usually invisible. Casting it to int displays its numeric value, 0.
Reference data types
A reference variable holds a reference value that can refer to an object or array, or it can hold null. Common reference types include classes, interfaces, arrays, enums, records, and type variables.
Stringand other class types- Array types such as
int[] - Interface types such as
List<String> - Enum and record types
- User-defined classes and interfaces
import java.util.ArrayList;
import java.util.List;
public class ReferenceTypes {
public static void main(String[] args) {
String language = "Java";
int[] scores = {82, 91, 76};
List<String> topics = new ArrayList<>();
topics.add("Data Types");
System.out.println(language.toUpperCase());
System.out.println(scores.length);
System.out.println(topics.get(0));
}
}Primitive vs reference types
| Primitive types | Reference types |
|---|---|
| Values are primitive values, not objects. | Values are references to objects or arrays, or null. |
| There are exactly eight primitive types. | Applications and libraries can define many reference types. |
| Cannot be dereferenced to call instance methods. | A non-null reference can be used to access members allowed by its type. |
Field defaults are zero-like values or false. | The default field value is null. |
Examples: int, double, boolean. | Examples: String, arrays, classes, interfaces. |
Avoid oversimplified memory claims
The language specification defines values and type behavior, not a simple rule that every primitive is always “on the stack” and every object is always “on the heap.” Runtime implementations and optimizations may organize storage differently.
Literals, suffixes, and type inference
- An integer literal such as
42is normally anint. - Use
Lfor alongliteral when required:9_000_000_000L. - A floating-point literal such as
3.14is adouble. - Use
forFfor afloatliteral:3.14f. - A character literal uses single quotes:
'A'. - A string literal uses double quotes:
"Java". varcan request local-variable type inference, but Java still assigns a static compile-time type.
Primitive wrapper classes
Each primitive type has a corresponding wrapper class. Wrappers are useful with generic collections and APIs that require objects.
| Primitive | Wrapper |
|---|---|
byte | Byte |
short | Short |
int | Integer |
long | Long |
float | Float |
double | Double |
char | Character |
boolean | Boolean |
Complete data-types example
public class DataTypeExample {
public static void main(String[] args) {
int lessons = 124;
long visitors = 2_500_000L;
double completion = 87.5;
char grade = 'A';
boolean published = true;
String course = "Java Tutorial";
System.out.println(course);
System.out.println("Lessons: " + lessons);
System.out.println("Visitors: " + visitors);
System.out.println("Completion: " + completion + "%");
System.out.println("Grade: " + grade);
System.out.println("Published: " + published);
}
}Common mistakes with Java data types
- Writing a large
longliteral without theLsuffix. - Assigning a
doubleliteral directly tofloatwithoutf. - Using
doublefor money without considering decimal rounding requirements. - Assuming a local variable receives a default value.
- Treating
0or1as boolean values. - Dereferencing a reference variable that contains
null. - Claiming that Java defines
booleanas exactly one bit.
float rate = 12.5;float rate = 12.5f;What’s next?
Continue with Java Identifiers to learn the naming rules for variables, methods, classes, and other declarations.
Lesson summary
- Java is statically typed.
- Java has eight primitive types.
- Reference values can point to objects or arrays, or be
null. - Integral types have precisely defined ranges.
floatanddoubleuse IEEE 754 binary floating-point arithmetic.- Fields and array components receive defaults; local variables require definite assignment.
- Wrapper classes represent primitive values as objects when APIs require reference types.
Frequently asked questions
What are data types in Java?
Data types define the type of data a variable can store.
How many primitive data types are there?
Java has 8 primitive data types.
Check your knowledge
Free learner features
Save your learning progress
Sign in to mark lessons as completed, save your Java learning progress across devices, and participate in lesson discussions.
Sign in to track progressDiscussion
Ask questions, share suggestions, or discuss this lesson.
No approved comments yet. Start the discussion by asking a helpful question.