Introduction
Data types in Java define the type of data a variable can hold. Understanding data types is important because they determine how the data is stored and what operations can be performed on it. Java provides a rich set of data types, which are categorized into primitive types and reference types. In this chapter, we will explore each data type with examples and demonstrate how they work.
Categories of Data Types
1. Primitive Data Types
Primitive data types are the basic types of data built into the Java language. These types serve as the building blocks for data manipulation. Java has eight primitive data types:
Data Type | Size | Description |
---|---|---|
byte | 1 byte | Stores whole numbers from -128 to 127 |
short | 2 bytes | Stores whole numbers from -32,768 to 32,767 |
int | 4 bytes | Stores whole numbers from -2^31 to 2^31 – 1 |
long | 8 bytes | Stores whole numbers from -2^63 to 2^63 – 1 |
float | 4 bytes | Stores fractional numbers, sufficient for storing 6-7 digits |
double | 8 bytes | Stores fractional numbers, sufficient for storing 15 digits |
char | 2 bytes | Stores a single character/letter or ASCII values |
boolean | 1 bit | Stores true or false values |
2. Reference Types
Reference types in Java are objects that store references to actual data. They are more complex than primitive types and include classes, arrays, interfaces, enums, and annotations.
Data Type | Size | Description |
---|---|---|
String | Varies | Stores a sequence of characters |
Array | Varies | Stores a collection of data items of the same type |
Class | Varies | Represents user-defined types |
Interface | Varies | Specifies methods that a class must implement |
Enum | Varies | Represents a fixed set of constants |
Annotation | Varies | Provides metadata for code elements (classes, methods, etc.) |
Primitive Data Types
1. byte (1 bytes)
The byte
data type is an 8-bit signed integer.
- Size: 8 bits
- Range: -128 to 127
- Example:
public class DataTypeExample { public static void main(String[] args) { byte byteValue = 10; System.out.println("Byte Value: " + byteValue); } }
Output:
Byte Value: 10
2. short (2 bytes)
The short
data type is a 16-bit signed integer.
- Size: 16 bits
- Range: -32,768 to 32,767
- Example:
public class DataTypeExample { public static void main(String[] args) { short shortValue = 1000; System.out.println("Short Value: " + shortValue); } }
Output:
Short Value: 1000
3. int
The int
data type is a 32-bit signed integer.
- Size: 32 bits (4 bytes)
- Range: -2^31 to 2^31 – 1
- Example:
public class DataTypeExample { public static void main(String[] args) { int intValue = 100000; System.out.println("Int Value: " + intValue); } }
Output:
Int Value: 100000
4. long
The long
data type is a 64-bit signed integer.
- Size: 64 bits (8 bytes)
- Range: -2^63 to 2^63 – 1
- Example:
public class DataTypeExample { public static void main(String[] args) { long longValue = 10000000000L; System.out.println("Long Value: " + longValue); } }
Output:
Long Value: 10000000000
5. float
The float
data type is a single-precision 32-bit IEEE 754 floating point.
- Size: 32 bits (4 bytes)
- Range: Approximately ±3.40282347E+38F
- Example:
public class DataTypeExample { public static void main(String[] args) { float floatValue = 10.5f; System.out.println("Float Value: " + floatValue); } }
Output:
Float Value: 10.5
6. double
The double
data type is a double-precision 64-bit IEEE 754 floating point.
- Size: 64 bits (8 bytes)
- Range: Approximately ±1.79769313486231570E+308
- Example:
public class DataTypeExample { public static void main(String[] args) { double doubleValue = 20.99; System.out.println("Double Value: " + doubleValue); } }
Output:
Double Value: 20.99
7. char
The char
data type is a single 16-bit Unicode character.
- Size: 16 bits (2 bytes)
- Range: 0 to 65,535 (Unicode characters)
- Example:
public class DataTypeExample { public static void main(String[] args) { char charValue = 'A'; System.out.println("Char Value: " + charValue); } }
Output:
Char Value: A
8. boolean
The boolean
data type represents one bit of information, but its “size” isn’t precisely defined.
- Size: 1 bit (logical value)
- Values: true or false
- Example:
public class DataTypeExample { public static void main(String[] args) { boolean booleanValue = true; System.out.println("Boolean Value: " + booleanValue); } }
Output:
Boolean Value: true
Reference Data Types
1. String
The String
data type represents a sequence of characters.
Example:
public class DataTypeExample { public static void main(String[] args) { String stringValue = "Hello, Java!"; System.out.println("String Value: " + stringValue); } }
Output:
String Value: Hello, Java!
2. Arrays
The Array
data type stores a fixed-size sequential collection of elements of the same type.
Example:
public class DataTypeExample { public static void main(String[] args) { int[] arrayValue = {1, 2, 3, 4, 5}; System.out.print("Array Values: "); for (int i : arrayValue) { System.out.print(i + " "); } } }
Output:
Array Values: 1 2 3 4 5
3. Class
The Class
data type defines a blueprint for creating objects, providing initial values for state (member variables) and implementations of behavior (member functions or methods).
Example:
public class DataTypeExample { public static void main(String[] args) { Person person = new Person("John", 30); System.out.println("Person Name: " + person.getName()); System.out.println("Person Age: " + person.getAge()); } } class Person { private String name; private int age; Person(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public int getAge() { return age; } }
Output:
Person Name: John Person Age: 30
4. Interface
An Interface
is a reference type in Java that is similar to a class. It is a collection of abstract methods that a class can implement.
Example:
interface Animal { void sound(); } class Dog implements Animal { public void sound() { System.out.println("Barks"); } } public class DataTypeExample { public static void main(String[] args) { Dog dog = new Dog(); dog.sound(); } }
Output:
Barks
5. Enum
An Enum
is a special Java type used to define collections of constants.
Example:
enum Day { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY } public class DataTypeExample { public static void main(String[] args) { Day day = Day.MONDAY; System.out.println("Day: " + day); } }
Output:
Day: MONDAY
6. Annotation
An Annotation
provides data about a program but is not part of the program itself. Annotations have no direct effect on the operation of the code they annotate.
Example:
@interface MyAnnotation { String value(); } @MyAnnotation(value = "Example") public class DataTypeExample { public static void main(String[] args) { System.out.println("Annotation Example"); } }
Output:
Annotation Example
Conclusion
In this chapter, we explored the various data types in Java, including primitive and reference types. Each data type serves a specific purpose and is used to handle different kinds of data. Understanding these data types is fundamental to Java programming, as they help in managing data efficiently and performing various operations.