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.
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.
Primitive Types vs. Reference Types: Key Differences
Understanding the difference between these two data types is crucial for mastering Java's memory management and object operations.