Java Data Types
In Java, every variable has a type that determines what kind of data the variable can store and what operations can be performed on that data. Java's data types are divided into two main categories: Primitive Data Types and Reference Data Types.
1. Primitive Data Types
Primitive data types are the most basic data types built into the Java language. They are not objects and directly store data values. There are 8 primitive data types in Java.
Integer Types
Used to represent numbers without a decimal part.
byte:- Size: 1 byte (8 bits)
- Range: -128 to 127
- Usage: Used when memory needs to be saved and the value range is small, such as when handling file streams or binary data.
short:- Size: 2 bytes (16 bits)
- Range: -32,768 to 32,767
- Usage: Also used to save memory, with a larger range than
byte.
int:- Size: 4 bytes (32 bits)
- Range: -2,147,483,648 to 2,147,483,647
- Usage: The most commonly used integer type. By default, any integer you write (like
100) is treated as aninttype.
long:- Size: 8 bytes (64 bits)
- Range: Very large
- Usage: Used when
intis not enough to store a very large integer. Note:longvalues must haveLorlat the end. For example:long bigNumber = 1234567890123L;
Floating-Point Types
Used to represent numbers with a decimal part.
float:- Size: 4 bytes (32 bits)
- Precision: Single precision, approximately 6-7 decimal significant digits.
- Usage: Used for scenarios requiring decimals but not high precision. Note:
floatvalues must haveForfat the end. For example:float price = 19.99F;
double:- Size: 8 bytes (64 bits)
- Precision: Double precision, approximately 15 decimal significant digits.
- Usage: The most commonly used floating-point type. Provides higher precision. By default, any decimal value you write (like
3.14) is treated as adoubletype.
Character Type
char:- Size: 2 bytes (16 bits)
- Encoding: Unicode character.
- Usage: Stores a single character. The value must be enclosed in single quotes (
'). For example:char grade = 'A';,char chineseChar = '中';
Boolean Type
boolean:- Size: The JVM does not explicitly specify its size, usually considered to be 1 bit.
- Values: Can only be
trueorfalse. - Usage: Used for logical judgments and conditional control, is the result of all relational operations.
public class DataTypesExample {
public static void main(String[] args) {
// Integer types
int myAge = 30;
long population = 8000000000L;
// Floating-point types
double pi = 3.1415926535;
float temperature = 36.6F;
// Character type
char initial = 'J';
// Boolean type
boolean isLoggedIn = true;
System.out.println("Age: " + myAge);
System.out.println("World population: " + population);
System.out.println("Pi: " + pi);
System.out.println("Temperature: " + temperature);
System.out.println("Initial: " + initial);
System.out.println("Login status: " + isLoggedIn);
}
}2. Reference Data Types
Unlike primitive data types that directly store values, reference data types store a reference (which can be understood as a memory address) to an object somewhere in memory.
- The variable itself does not store the object, but stores a "pointer" that points to that object.
- All variables of types created with the
newkeyword are reference types. - If a reference variable does not point to any object, its value is
null.
Common reference data types include:
- Classes: All types defined by
class, such as theDogclass we created earlier, and Java's built-inStringclass. - Interfaces: Types defined by
interface. - Arrays: Collections used to store elements of the same type.
// String is a very commonly used reference type
String greeting = "Hello, Java!";
// Dog is a custom class we defined, myDog is a reference variable
Dog myDog = new Dog(); // myDog stores the address of the newly created Dog object
// Arrays are also reference types
int[] numbers = new int[5]; // numbers stores the address of the newly created array objectPrimitive Types vs. Reference Types: Key Differences
| Feature | Primitive Data Types | Reference Data Types |
|---|---|---|
| Storage Content | Variable directly stores the data value | Variable stores the memory address (reference) of the object |
| Storage Location | Stack memory | The object itself is in heap memory, the reference is in stack memory |
| Default Value | Numeric types are 0 or 0.0, boolean is false, char is \u0000 | null |
| Size | Fixed size (e.g., int is 4 bytes) | Variable size |
Understanding the difference between these two data types is crucial for mastering Java's memory management and object operations.