- Java Tutorial
- Java Introduction
- Java Features
- Java Simple Program
- JVM, JDK and JRE
- Java Syntax
- Java Comments
- Java Keywords
- Java Variables
- Java Literals
- Java Separators
- Java Datatypes
- Java Operators
- Java Statements
- Java Strings
- Java Arrays
- Control Statement
- Java If
- Java If-else
- Java If-else-if
- Java Nested If
- Java Switch
- Iteration Statement
- Java For Loop
- Java For Each Loop
- Java While Loop
- Java Do While Loop
- Java Nested Loop
- Java Break/Continue
- Java Methods
- Java Methods
- Java Method Parameters
- Java Method Overloading
- Java Recursion
- Java OOPS
- Java OOPs
- Java Classes/Objects
- Java Inheritance
- Java Polymorphism
- Java Encapsulation
- Java Abstraction
- Java Modifiers
- Java Constructors
- Java Interface
- Java static keyword
- Java this keyword
- Java File Handling
- Java File
- Java Create File
- Java Read/Write File
- Java Delete File
- Java Program To
- Add Two Numbers
- Even or Odd Numbers
- Reverse a String
- Swap Two Numbers
- Prime Number
- Fibonacci Sequence
- Palindrome Strings
- Java Reference
- Java String Methods
- Java Math Methods
Java Data Types
In Java, every variable must have a declared data type. Because Java is a statically-typed language, the type of a variable is checked at compile-time, which helps catch errors early. Java data types are divided into two main categories: Primitive and Reference types. Understanding the difference between them is fundamental to managing memory and writing efficient code.
byte instead of an int for a small range of numbers can save significant memory in large arrays.
Primitive Data Types:
- These are the most basic data types available in Java. They are not objects and do not have methods.
- They are predefined by the language and represent raw values directly in memory.
- Java provides eight primitive types: byte, short, int, long, float, double, boolean, and char.
Integer Types:
Integer types store whole numbers without decimals. They differ primarily by the range of numbers they can hold and the amount of memory they consume.
- byte: The smallest integer type. It is an 8-bit signed integer. Range: -128 to 127. Useful for saving memory in large arrays or when working with raw binary data from a file or network.
- short: A 16-bit signed integer. Range: -32,768 to 32,767. Rarely used today but helpful in legacy systems or memory-constrained environments.
- int: The standard 32-bit signed integer. This is the "go-to" type for most whole numbers (e.g., loop counters, array indices).
- long: A 64-bit signed integer. Used when the value exceeds the range of
int, such as representing world populations, timestamps in milliseconds, or large financial calculations.
long, you must append an 'L' to the end of the number. Without it, Java treats the literal as an int and may throw a compilation error if the number is too large.
Floating-Point Types:
These are used when you need fractional precision (decimals).
- float: A 32-bit single-precision floating-point. It requires an 'f' suffix (e.g.,
3.14f). Use this when memory is a concern in massive arrays of decimals. - double: A 64-bit double-precision floating-point. This is the default for decimal numbers in Java and provides much higher precision than
float.
double. However, for high-precision financial data (like currency), never use float or double due to rounding errors; use java.math.BigDecimal instead.
Character Type:
- char: A 16-bit type used to store a single Unicode character. It is defined using single quotes (e.g.,
'A'). Because it uses Unicode, it can represent characters from almost every language in the world.
char and String. Use single quotes for char ('a') and double quotes for String ("a"). A char is a primitive, while a String is a reference type.
Boolean Type:
- boolean: Represents only two possible values: true or false. It is commonly used for conditional logic and flags.
Reference Data Types:
Unlike primitives, reference types do not store the value itself; instead, they store a reference (an address) to the location in memory where the object is stored.
- They include Classes (like
String), Interfaces, Arrays, and Enums. - The default value for any reference type is
null. - They are stored on the "Heap" memory, whereas primitives are usually stored on the "Stack."
Example
public class DataTypesExample {
public static void main(String[] args) {
// --- Primitive data types ---
// Use byte for small ranges (like ages or months)
byte month = 12;
// Standard integer for most use cases
int age = 25;
// Long requires 'L' suffix
long population = 8000000000L;
// Float requires 'f' suffix
float pi = 3.14159f;
// Double is the standard for decimals
double price = 19.99;
// Char stores a single character in single quotes
char grade = 'A';
// Boolean for logic
boolean isJavaFun = true;
// --- Reference data types ---
// String is a class (Reference Type)
String name = "Software Developer";
// Arrays are reference types
int[] scores = {95, 88, 72, 90};
// Object is the root of all classes in Java
Object obj = new Object();
// Outputting values
System.out.println("User Name: " + name);
System.out.println("Age: " + age);
System.out.println("Global Population: " + population);
System.out.println("Product Price: $" + price);
System.out.println("Is Java Fun? " + isJavaFun);
System.out.println("First Score: " + scores[0]);
}
}
Summary
Java's type system is designed for safety and performance. Use Primitive types when you need to store simple values efficiently and don't need the overhead of objects. Use Reference types when you need to work with complex data structures, strings, or custom objects. Mastering the choice between these types is the first step toward writing professional-grade Java applications.