Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – Black Friday 2026 – NPI EA – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

1. Overview

In this tutorial, we’ll discuss how to create a char stack in Java. We’ll first see how we can do this by using the Java API, and then we’ll look at some custom implementations.

Stack is a data structure that follows the LIFO (Last In First Out) principle. Some of its common methods are:

  • push(E item) – pushes an item to the top of the stack
  • pop() – removes and returns the object at the top of the stack
  • peek() – returns the object at the top of the stack without removing it

2. Char Stack Using Java API

Java has a built-in API named java.util.Stack. Since char is a primitive datatype, which cannot be used in generics, we have to use the wrapper class of java.lang.Character to create a Stack:

Stack<Character> charStack = new Stack<>();

Now, we can use the push, pop, and peek methods with our Stack.

On the other hand, we may be asked to build a custom implementation of a stack. Therefore, we’ll be looking into a couple of different approaches.

3. Custom Implementation Using LinkedList

Let’s implement a char stack using a LinkedList as our back-end data structure:

public class CharStack {

    private LinkedList<Character> items;

    public CharStack() {
        this.items = new LinkedList<Character>();
    }
}

We created an items variable that gets initialized in the constructor.

Now, we have to provide an implementation of the push, peek, and pop methods:

public void push(Character item) {
    items.push(item);
}

public Character peek() {
    return items.getFirst();
}

public Character pop() {
    Iterator<Character> iter = items.iterator();
    Character item = iter.next();
    if (item != null) {
        iter.remove();
        return item;
    }
    return null;
}

The push and peek methods are using the built-in methods of a LinkedList. For pop, we first used an Iterator to check if there’s an item on the top or not. If it’s there, we remove the item from the list by calling the remove method.

4. Custom Implementation Using an Array

We can also use an array for our data structure:

public class CharStackWithArray {

    private char[] elements;
    private int size;

    public CharStackWithArray() {
        size = 0;
        elements = new char[4];
    }

}

Above, we create a char array, which we initialize in the constructor with an initial capacity of 4. Additionally, we have a size variable to keep track of how many records are present in our stack.

Now, let’s implement the push method:

public void push(char item) {
    ensureCapacity(size + 1);
    elements[size] = item;
    size++;
}

private void ensureCapacity(int newSize) {
    char newBiggerArray[];
    if (elements.length < newSize) {
        newBiggerArray = new char[elements.length * 2];
        System.arraycopy(elements, 0, newBiggerArray, 0, size);
        elements = newBiggerArray;
    }
}

While pushing an item to the stack, we first need to check if our array has the capacity to store it. If not, we create a new array and double its size. We then copy the old elements to the newly created array and assign it to our elements variable.

Note: for an explanation of why we want to double the size of the array, rather than simply increase the size by one, please refer to this StackOverflow post.

Finally, let’s implement the peek and pop methods:

public char peek() {
    if (size == 0) {
        throw new EmptyStackException();
    }
    return elements[size - 1];
}

public char pop() {
    if (size == 0) {
        throw new EmptyStackException();
    }
    return elements[--size];
}

For both methods, after validating that the stack is not empty, we return the element at position size – 1. For pop, in addition to returning the element, we decrement the size by 1.

5. Conclusion

In this article, we learned how to make a char stack using the Java API, and we saw a couple of custom implementation.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

Course – Black Friday 2026 – NPI EA – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW