Three stacks in a single array I have implemented three stacks in a single array in Java. Any advice on object oriented design, coding structure, logical part or any sort of advice would be appreciated.  

public class ThreeStack {
    private int[] stack;

    private int first, last, mid;
    private final int midPosition;

    public ThreeStack(int size) {
        stack = new int[size];
        first = -1; last = size; mid = (first + last) &gt;&gt; 1;
        midPosition = mid;
    }

    //push
    public boolean pushFirstStack(final int val) {
        if (first + 1 &lt; midPosition) {
            stack[++first] = val;
            return true;
        } else {
            return false;
        }
    }

    public boolean pushSecondStack(final int val) {
        if (mid + 1 &lt; last) {
            stack[++mid] = val;
            return true;
        } else {
            return false;
        }
    }

    public boolean pushThirdStack(final int val) {
        if (last - 1 &gt; mid) {
            stack[--last] = val;
            return true;
        } else {
            return false;
        }
    }

    //pop
    public int popFirstStack() {
        if (first &lt; 0) {
            return Integer.MIN_VALUE;
        } else {
            int val = stack[first];
            stack[first--] = 0;
            return val;
        }
    }

    public int popSecondStack() {
        if (mid &lt; midPosition) {
            return Integer.MIN_VALUE;
        } else {
            int val = stack[mid];
            stack[mid--] = 0;
            return val;
        }
    }

    public int popThirdStack() {
        if (last &gt;= stack.length) {
            return Integer.MIN_VALUE;
        } else {
            int val = stack[last];
            stack[last++] = 0;
            return val;
        }
    }

    //size
    public int firstStackSize() {
        return mid - midPosition;
    }

    public int secondStackSize() {
        return last - mid;
    }

    public int lastStackSize() {
        return secondStackSize();
    }

    public void printStack() {
        for (int i = 0; i &lt; stack.length; i++) {
            System.out.print(stack[i]);
            System.out.print((i == first || i == mid || i == last) ? "*": "");
            System.out.print(" ");
        }
        System.out.println();
    }
}


Main Method

public class MainClass {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        ThreeStack stack = new ThreeStack(12);


        while (true) {
            stack.printStack();
            menu();
            int c = in.nextInt();
            switch (c) {
                case 1:
                    stack.pushFirstStack(in.nextInt());
                    break;
                case 2:
                    stack.pushSecondStack(in.nextInt());
                    break;
                case 3:
                    stack.pushThirdStack(in.nextInt());
                    break;
                case 4:
                    printPopVal(stack.popFirstStack());
                    break;
                case 5:
                    printPopVal(stack.popSecondStack());
                    break;
                case 6:
                    printPopVal(stack.popThirdStack());
                    break;
            }
        }

    }

    static void menu() {
        System.out.println("+------------------------------+");
        System.out.println("1 -&gt; push first stack");
        System.out.println("2 -&gt; push second stack");
        System.out.println("3 -&gt; push third stack");
        System.out.println("4 -&gt; pop first stack");
        System.out.println("5 -&gt; pop second stack");
        System.out.println("6 -&gt; pop third stack");
        System.out.println("+------------------------------+");
    }

    static void printPopVal(int val) {
        System.out.println("Pop : " + val);
    }
}
Stack sizing

The 3 stacks have different size limits:


The 1st stack can have maximum size / 2 elements
The 2nd and 3rd stacks share their storage, they can have size / 2 elements


When there is a peculiar limitation like this, it's good to document it, and hopefully with an explanation of the reasoning.

An alternative approach could have been giving each stack size / 3 space.

Another alternative would be using an array of StackNode objects, where StackNode would store the payload, and a link to the StackNode above it. This way all stacks would have dynamic size, until their total use reaches the capacity of the StackNode array. However, in addition to the extra storage needed (for the links), the logic would be also more complicated to account for the freed spots in the array.

Make final what you can

This field can be final:


private int[] stack;



One statement per line

It's best when you can read code from top to bottom.
When there are multiple statements per line,
it's easy to overlook the right end.
So instead of putting multiple assignments on the same line like this:


first = -1; last = size; mid = (first + last) &gt;&gt; 1;



It would be better to break it up, to have one statement per line.

Clever code

This code is a bit clever:


mid = (first + last) &gt;&gt; 1;



It's best to keep things simple and natural:

mid = (first + last) / 2;


Sure, bit shifting is faster than division.
But it's reasonable to expect the compiler to automatically convert / 2 to use bit shifting instead of division, so that you can write simple, natural, unsurprising code. (I don't know if the compiler actually does that, but in any case, such micro-optimization is likely to be pointless pedantry anyway.)