In Java, data types specify the type of data a variable can store. Data types are essential because they define the size and type of values that can be stored in variables. Java is a strongly-typed language, meaning every variable must be declared with a data type.
Categories of Data Types in Java
Java data types are broadly categorized into two types:
- Primitive Data Types
- Non-Primitive (Reference) Data Types
1. Primitive Data Types
Primitive data types are the most basic building blocks of data storage in Java. There are 8 primitive data types:
Data Type | Size | Default Value | Example |
---|---|---|---|
byte |
1 byte | 0 | byte b = 100; |
short |
2 bytes | 0 | short s = 30000; |
int |
4 bytes | 0 | int i = 100000; |
long |
8 bytes | 0L | long l = 100000L; |
float |
4 bytes | 0.0f | float f = 5.75f; |
double |
8 bytes | 0.0d | double d = 19.99; |
char |
2 bytes | ‘u0000’ | char c = 'A'; |
boolean |
1 bit | false | boolean b = true; |
Detailed Explanation of Primitive Types
-
Integer Types:
byte
,short
,int
, andlong
are used for whole numbers.- Choose based on the range of values you expect:
byte
: -128 to 127 (smallest).short
: -32,768 to 32,767.int
: -2,147,483,648 to 2,147,483,647 (default for integers).long
: Larger numbers, up to ±9 quintillion.
-
Floating-Point Types:
float
anddouble
are used for decimal numbers.float
is single-precision (less precise), whiledouble
is double-precision (more precise, default for decimals).
-
Character Type:
char
stores a single 16-bit Unicode character (e.g.,'A'
,'9'
, or'u0041'
).
-
Boolean Type:
boolean
can only hold two values:true
orfalse
.
Examples of Primitive Data Types:
public class PrimitiveTypesExample { public static void main(String[] args) { byte b = 10; short s = 200; int i = 100000; long l = 100000L; float f = 5.75f; double d = 19.99; char c = 'A'; boolean isJavaFun = true; System.out.println("Byte: " + b); System.out.println("Short: " + s); System.out.println("Int: " + i); System.out.println("Long: " + l); System.out.println("Float: " + f); System.out.println("Double: " + d); System.out.println("Char: " + c); System.out.println("Boolean: " + isJavaFun); } }
2. Non-Primitive (Reference) Data Types
Non-primitive data types refer to objects and are used to store more complex data structures. Examples include:
- Strings: For text.
- Arrays: To store multiple values of the same type.
- Classes and Objects: For user-defined types.
- Interfaces: For defining abstract types.
Key Differences Between Primitive and Non-Primitive Data Types
Feature | Primitive | Non-Primitive |
---|---|---|
Defined By | Java language (built-in). | User-defined or provided by Java libraries. |
Size | Fixed size. | Varies. |
Default Value | Depends on type (e.g., 0, false). | null . |
Stored In | Stack memory. | Heap memory. |
Example | int , float , boolean . |
String , Array , Object . |
Example of Non-Primitive Types:
public class ReferenceTypesExample { public static void main(String[] args) { String name = "Java"; int[] numbers = {1, 2, 3, 4, 5}; System.out.println("String: " + name); System.out.println("Array: " + java.util.Arrays.toString(numbers)); } }
Type Conversion Between Data Types
Java allows conversions between compatible data types. These conversions can be:
- Implicit (Widening): Automatically converts smaller types to larger ones (e.g.,
int
tolong
). - Explicit (Narrowing): Requires casting to convert larger types to smaller ones (e.g.,
double
toint
).
Example:
public class TypeConversionExample { public static void main(String[] args) { int num = 10; double d = num; // Implicit conversion double value = 9.8; int intValue = (int) value; // Explicit conversion System.out.println("Double to Int: " + intValue); } }
Key Points to Remember
- Choose the appropriate data type for variables to optimize memory usage.
- Default values are assigned only to instance and class variables (not local variables).
- Use non-primitive types for complex data storage and manipulation.