Current post type: post
To illustrate the differences between Data Type, Abstract Data Type (ADT), and Data Structure in Java, let’s use the example of a Stack. Below, I’ll provide code snippets for each concept:
A data type defines the type of data a variable can hold and the operations that can be performed on it. In Java, data types can be primitive (e.g., int
, float
) or reference types (e.g., String
, Object
).
int
)int number = 10; // 'int' is a primitive data type
number = number + 5; // Operation: addition
System.out.println(number); // Output: 15
String
)String name = "Alice"; // 'String' is a reference data type
name = name.toUpperCase(); // Operation: convert to uppercase
System.out.println(name); // Output: ALICE
An ADT is a high-level description of data and operations. In Java, an ADT is typically defined as an interface or an abstract class. It specifies what operations can be performed, but not how they are implemented.
public interface StackADT<T> {
void push(T element); // Add an element to the stack
T pop(); // Remove and return the top element
T peek(); // Return the top element without removing it
boolean isEmpty(); // Check if the stack is empty
int size(); // Return the number of elements in the stack
}
This interface defines the behavior of a stack (what it can do), but it does not specify how the stack is implemented.
A data structure is the concrete implementation of an ADT. In Java, this is typically a class that implements the ADT interface or provides the logic for storing and manipulating data.
public class ArrayStack<T> implements StackADT<T> {
private T[] stackArray; // Array to store stack elements
private int top; // Index of the top element
private int capacity; // Maximum capacity of the stack
// Constructor
public ArrayStack(int capacity) {
this.capacity = capacity;
this.stackArray = (T[]) new Object[capacity]; // Create array of generic type
this.top = -1; // Stack is initially empty
}
// Push operation
@Override
public void push(T element) {
if (top == capacity - 1) {
throw new IllegalStateException("Stack is full");
}
stackArray[++top] = element; // Add element to the top
}
// Pop operation
@Override
public T pop() {
if (isEmpty()) {
throw new IllegalStateException("Stack is empty");
}
return stackArray[top--]; // Remove and return the top element
}
// Peek operation
@Override
public T peek() {
if (isEmpty()) {
throw new IllegalStateException("Stack is empty");
}
return stackArray[top]; // Return the top element
}
// Check if stack is empty
@Override
public boolean isEmpty() {
return top == -1;
}
// Return the size of the stack
@Override
public int size() {
return top + 1;
}
}
This class provides the implementation of the StackADT
interface using an array as the underlying data structure.
Here’s how the three concepts work together in Java:
T
to store elements. For example, T
could be Integer
, String
, or any other data type.StackADT
interface defines the operations (push
, pop
, peek
, etc.).ArrayStack
class implements the StackADT
interface using an array to store and manipulate data.public class Main {
public static void main(String[] args) {
StackADT<Integer> stack = new ArrayStack<>(5); // Create a stack of integers
stack.push(10); // Push 10
stack.push(20); // Push 20
stack.push(30); // Push 30
System.out.println(stack.pop()); // Output: 30 (LIFO behavior)
System.out.println(stack.peek()); // Output: 20 (Top element is 20)
System.out.println(stack.size()); // Output: 2 (Stack has 2 elements)
}
}
Concept | Code Example | Purpose |
---|---|---|
Data Type | int number = 10; or String name = "Alice"; | Defines the type of data and basic operations. |
ADT | StackADT<T> interface with push , pop , peek , etc. | Defines the behavior of a data type (what operations are possible). |
Data Structure | ArrayStack<T> class implementing StackADT<T> using an array. | Provides the concrete implementation of the ADT (how operations are done). |
int
, String
).StackADT
interface).ArrayStack
class).This separation allows for flexibility and modularity in designing and using data structures in Java.
By MOUSTAFA ALSAYEH© 2025 My Company