What is ++ in java?
Hello. In this tutorial, we will understand the postfix and prefix operators in the java programming language.
1. Introduction
In java, postfix and prefix operators are used to perform the mathematical operations (i.e. either addition (++) or subtraction (–) based on the operator supplied.
2. Practice
Let us dive into some practice stuff from here and I am assuming that you already have Java 1.8 or greater installed on your local machine. I am using JetBrains IntelliJ IDEA as my preferred IDE. You’re free to choose the IDE of your choice.
2.1 Understanding postfix operator
Create a java file that will show the implementation of the postfix operator. The example will present the usage of the postfix operator on integer and double data type values.
Implementation class
package com.learning; public class PostfixOperator { public static void main(String[] args) { int i = 0; System.out.println("Post increment of integer"); // i value is incremented to 1 after returning the current value i.e; 0 System.out.println(i++); double num = 5.5; System.out.println("Post increment of double"); // num value is incremented to 6.5 after returning the current value i.e; 5.5 System.out.println(num++); } }
Run the file as a java file and if everything goes well the following logs will be shown on the IDE console.
2.2 Understanding prefix operator
Create a java file that will show the implementation of the prefix operator. The example will present the usage of the prefix operator on integer and double data type values.
Implementation class
package com.learning; public class PrefixOperator { public static void main(String[] args) { int i = 0; System.out.println("Pre increment of integer"); // i value is incremented to 1 before returning the current value System.out.println(++i); double num = 5.5; System.out.println("Pre increment of double"); // num value is incremented to 6.5 before returning the current value System.out.println(++num); } }
Run the file as a java file and if everything goes well the following logs will be shown on the IDE console.
That is all for this tutorial and I hope the article served you with whatever you were looking for. Happy Learning and do not forget to share!
3. Summary
In this tutorial, we discussed the practical implementation of postfix and prefix operators in the java programming language. You can download the source code from the Downloads section.
4. Download the Project
This was a tutorial to understand the practical implementation of postfix and prefix operators in the java programming language.
You can download the full source code of this example here: What is ++ in java?