Java Data Types Explained: With Syntax, Examples & Outputs
When you write code in Java, picking the right data type matters. This guide explains what they are, why they’re important, and how to use them.
What Are Data Types in Java?
In Java, a data type simply tells the computer what kind of data a variable is going to store. It’s like saying, “Hey, this container is going to store numbers,” or “This container will hold text.” Java needs to know this in advance because it’s a statically typed language, meaning you have to tell it what type of data a variable will hold before you use it. Whether it’s a number, a piece of text, a single character, or just a true/false value, Java needs that information to handle the data properly.
Types of Data Types in Java
Java has two main categories:
- Primitive Data Types: These hold the actual values directly in memory. Examples include int, char, boolean, and float. For example, if you create an int x = 5;, the value 5 is stored directly in the memory allocated for x.
- Non-Primitive Data Types: These hold a reference (or pointer) to the actual data stored elsewhere in memory. Examples include arrays, String, and objects. For example, when you declare String name = "John";, the variable name holds a reference to the memory location containing the characters J, o, h, and n.
Primitive Data Types
byte
A small integer data type. Used to save memory in large arrays when the memory savings actually matters.
Syntax:
byte variableName = value;
Size & Range: 1 byte and -128 to 127
Example:
public class GoBasicsByteExample {
public static void main(String[] args) {
byte userAge = 23;
System.out.println(userAge);
}
}
Output: 23
short
A larger integer than byte, but smaller than int. Also used to save memory in large arrays.
Syntax:
short variableName = value;
Size & Range: 2 bytes and -32,768 to 32,767
Example:
public class GoBasicsShortExample {
public static void main(String[] args) {
short userSalary = 30000;
System.out.println(userSalary);
}
}
Output: 30000
int
The most commonly used integer data type. Default data type for integral values. Be mindful about integer overflow: if a number goes over about 2 billion, it can turn into a negative number. For very large numbers, use long or BigInteger.
Syntax:
int variableName = value;
Size & Range: 4 bytes and -2,147,483,648 to 2,147,483,647
Example:
public class GoBasicsIntExample {
public static void main(String[] args) {
int countryPopulation = 1480000000;
System.out.println(countryPopulation);
}
}
Output: 1480000000
long
A larger integer data type used when a wider range than int is needed. Must append L or l to the value.
Syntax:
long variableName = valueL;
Size & Range: 8 bytes and -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
Example:
public class GoBasicsLongExample {
public static void main(String[] args) {
long maxLimit = 9876543210L;
System.out.println(maxLimit);
}
}
Output: 9876543210
float
A single-precision 32-bit IEEE 754 floating point. Used to save memory in large floating point arrays. Must append f or F to the value. Also, remember that floating-point numbers (like float and double) can make rounding mistakes. Not used for precise values (e.g., currency).
Syntax:
float variableName = valuef;
Size & Range: 4 bytes and approximately ±3.4e−38 to ±3.4e+38
Example:
public class GoBasicsFloatExample {
public static void main(String[] args) {
float normalTemperature = 98.6f;
System.out.println(normalTemperature);
}
}
Output: 98.6
double
A double-precision 64-bit IEEE 754 floating point. Default data type for decimal values. More precise than float. Used when precision is important.
Syntax:
double variableName = value;
Size & Range: 8 bytes and approximately ±1.7e−308 to ±1.7e+308
Example:
public class GoBasicsDoubleExample {
public static void main(String[] args) {
double processingTime = 9.19; // Processing time in seconds
System.out.println(processingTime);
}
}
Output: 9.19
char
A single 16-bit Unicode character. Used to store characters like 'A', 'z', '1', or special symbols.
Syntax:
char variableName = 'A';
Size & Range: 2 bytes and 0 to 65,535
Example:
public class GoBasicsCharExample {
public static void main(String[] args) {
char grade = 'A';
System.out.println(grade);
}
}
Output: A
boolean
Stores only two possible values: true or false. Used for simple flags or conditions. Useful in control statements like if, while, etc.
Syntax:
boolean variableName = true;
Size & Range: ~1 bit and true or false
Example:
public class GoBasicsBooleanExample {
public static void main(String[] args) {
boolean isCarLocked = true;
System.out.println(isCarLocked);
}
}
Output: true
Official Java Documentation: Primitive Data Types Documentation
Non-Primitive Data Types
String
Definition: Stores a sequence of characters. Don’t use == to compare two objects, like strings, because it checks if they are the same object. Instead, use .equals() to check if they have the same content. I learned this the hard way when two strings looked the same, but weren’t.
Key properties: In Java, once a string is created, it cannot be modified—this is what we call immutability. Behind the scenes, these strings are kept in a special place in memory, known as the String Pool, which helps with performance. If you want to work with the individual characters, you can access them by their position, starting from index 0
Edge case: Be cautious of calling methods like .equals() or .length() on a null string—it will throw a NullPointerException. Also, comparisons are case-sensitive, so use .equalsIgnoreCase() if needed.
Syntax:
String variableName = "value";
For example, if variableName is websiteName and the value is "GoBasics", then websiteName.charAt(0) gives you 'G', websiteName.charAt(1) gives you 'o', and so on.
Size: Varies
Example:
public class GoBasicsStringExample {
public static void main(String[] args) {
String websiteName = "GoBasics";
System.out.println(websiteName);
}
}
Output: GoBasics
Array
Definition: Arrays in Java can hold many values of the same type, but they always have a fixed size—unlike lists in Python.
Key properties: Arrays in Java are created with a fixed size—you can’t add or remove elements after that. To change the size, you’d need to create a new array. Each item in the array is accessed using an index starting from 0, and all elements are stored sequentially in a continuous block of memory.
Edge case: If you try to use an index that’s outside the array’s size, you’ll get an ArrayIndexOutOfBoundsException.
Syntax:
dataType[] arrayName = {value1, value2, value3, value4, ...};
For example, numbers[0] gives you the first value, which is value1, and the sequence continues with numbers[1] giving value2, and so on.
Size: Varies
Example:
public class GoBasicsArrayExample {
public static void main(String[] args) {
int[] numbers = {110, 120, 130,140};
System.out.println(numbers[3]);
}
}
Output: 140
Class
Definition: A blueprint or template for creating objects. It defines fields (attributes) and methods (behaviors), but it does not hold actual data.
Syntax:
class ClassName {
// fields and methods
}
Example:
class BlogPage {
String pageTitle = "Java Data Types: Primitive & Non-Primitive Types";
void displayPageInfo() {
System.out.println("Page: " + pageTitle);
}
}
public class Main {
public static void main(String[] args) {
BlogPage myPage = new BlogPage();
myPage.displayPageInfo(); // Output: Page: Java Data Types: Primitive & Non-Primitive Types
}
}
Output:
Page: Java Data Types: Primitive & Non-Primitive Types
Object
Definition: An actual instance of a class. It holds data and allows interaction with fields and methods defined in the class.
Syntax:
ClassName obj = new ClassName();
Example:
class Bike {
String brandName = "Triumph";
}
public class GoBasics {
public static void main(String[] args) {
Bike myBike = new Bike(); //object creation
System.out.println(myBike.brandName);
}
}
Output: Triumph
Official Java Documentation: Non-Primitive Data Types Documentation
Give It a Try: Java Data Types in Action
Ready to put your Java skills into practice? In our Java Data Types Example, we’ll walk you through writing a simple, hands-on program that uses common data types like int, double, and boolean. It’s a great way to see Java in action and build your confidence with coding!
Conclusion
Mastering Java’s data types is key to writing efficient and bug-free code. From simple primitives to powerful objects, understanding how Java stores and manages data helps you build better applications.
Related Posts
Java Access Modifiers Explained
Learn how Java's access modifiers (public, private, protected, default) control the visibility and accessibility of classes, methods, and fields.
Read MoreJava Control Structures
Explore Java’s control structures like if-else, switch, and loops to manage program flow.
Read MoreMastering Java Operators
Learn how to use Java’s arithmetic, relational, logical, and bitwise operators to perform calculations and make decisions in your code.
Read More