Converting Infix to Postfix in Java

When you enter a simple arithmetic operation, such as, “2+4”, you can see that there is “+” operator in between two integers. This operator is called binary operator, because it is between TWO data types.

Programming languages have a precedence rules for computing the operators. For instance, “*” operator is generally computed first to “+” operator, i.e., “2+4*5” will yield “22” as a result rather than “30”.

Note, however, there is an ambiguity when you try to understand the statement, as to whether the compiler will compute “*” operator earlier than “+” operator. To make it unambiguous, we often use brackets to emphasize which operator we want to be computed first.

In our example, “2+4*5”, you can see that we are using operators between digits. This is an infix expression, where you must use brackets to decrease ambiguity in your expressions. This is how we are accustomed to writing expressions in standard mathematical notation.

In a Postfix expression, a binary operator is applied to the two immediately preceding operands. A unary operator is applied to the one immediately preceding operand. “45*2-“.

Notice that the operands in a postfix expression occur in the same order as in the corresponding infix expression. Only the position of the operators is changed

Why the need to convert Infix to Postfix? 
Infix and Postfix (the third notation is Prefix which we won’t discuss in this guide) notations are two different but equivalent ways of writing expressions. The best thing about postfix is that the order in which
operators are applied is always unambiguous.

Implementation in Java

1) Implementing Stack Class (Optional)

Stack (LIFO) is a suitable holder for storing characters of given string of an expression, because it simply provides “push” and “pop” for adding the element to the top and retrieving the last element respectively. Stack is a sub-class of Vector (ArrayList in Java). It guarantees the rule of LIFO (Last In First Out), unlike Vector, which is quite flexible and where the elements can be added / retrieved between the list.

Even though, Java already provides Stack implementation in its standard library (java.util.Stack), it is useful to understand the implementation of Stack. This is optional, and you can use the standard class instead (more on that later).

2) Creating getOperator and getParenthesis Function
Since, we need to determine which character in a given string is an operator or a parenthesis, it would be better to create a separate function for them.

4) Creating IntToPost class
Now we will create a separate class that will take the input expression, and will have the method to convert it to Postfix expression.

3) Finishing
Now, let’s test our program to see if we implemented our program correctly.

Conclusion

In this tutorial, I have showed you how you can convert Infix to Postfix in Java programming language. This tutorial was supposed to be a beginner friendly guide with an emphasis on users trying to learn the core programming. If you have any question, then feel free to ask in the comments section below!