Java

What Does ++ Mean in Java?

In Java, ++ means adding 1 to the number held by the variable of interest. It can be postfix or prefix, as explained below. The number can be an int or a double. Java does not have explicit pointers, so it can-not be used to increment the pointer in Java, as compared to C++. The ++ is known as the increment operator in Java.

This article explains the meaning of ++ in Java and addresses its use in iteration, while making some comparisons with C++.

Postfix ++

Postfix means that ++ is typed after the variable, writing from left to right.

Integer

The following program illustrates the postfix operation with an int:

    public class TheClass {
        public static void main(String[] args) {
            int in = 5;
            int inPP = in++;
            System.out.print(inPP); System.out.print(", "); System.out.print(in++);
            System.out.println();
        }
    }

The output is 5, 6. With postfix operation, the value of the variable is returned before the adding of 1 takes place. That is why the output is 5 and 6, and it is not 6 and 6.

Double

The following program illustrates the postfix operation with a double:

    public class TheClass {
        public static void main(String[] args) {
            double dbl = 2.4;
            double dblPP = dbl++;
            System.out.print(dblPP); System.out.print(", "); System.out.print(dbl++);
            System.out.println();
        }
    }

The output is 2.4, 3.4. With postfix operation, the value of the variable is returned before adding 1 takes place. That is why the output is 2.4 and 3.4, and it is not 3.4 and 3.4.

Prefix ++

Prefix means that ++ is typed before the variable, writing from left to right.

Integer

The following program illustrates the prefix operation with an int:

    public class TheClass {
        public static void main(String[] args) {
            int in = 5;
            int inPF = ++in;
            System.out.print(inPF); System.out.print(", "); System.out.print(in++);
            System.out.println();
        }
    }

The output is: 6, 6. With prefix operation, the value of the variable is returned after adding 1, which takes place. That is why the output is 6 and 6, not 5 and 6, like in a previous case.

Double

The following program illustrates the prefix operation with a double:

    public class TheClass {
        public static void main(String[] args) {
            double dbl = 2.4;
            double dblPF = ++dbl;
            System.out.print(dblPF); System.out.print(", "); System.out.print(dbl++);
            System.out.println();
        }
    }

The output is 3.4, 3.4. With prefix operation, the value of the variable is returned after adding 1, which takes place. That is why the output is 3.4 and 3.4, and it is not 2.4 and 3.4, as in a previous case.

Iterating an ArrayList

Each element in an ArrayList can be accessed using a for-loop and the increment operator ++, as fol-lows:

    import java.util.*;
    public class TheClass {
        public static void main(String[] args) {
            ArrayList<Character> al = new ArrayList<Character>();
            al.add('A'); al.add('B'); al.add('C'); al.add('D'); al.add('E');
            al.add('F'); al.add('G'); al.add('H'); al.add('I'); al.add('J');
       
            for (int i=0; i<al.size(); i++) {
                char ch = al.get(i);
                System.out.print(ch); System.out.print(' ');
            }
            System.out.println();
        }
    }

The output is:

    A B C D E F G H I J

The ArrayList class is in the java.util.* package. After the ArrayList object was created in the program, elements were added. A for-loop was used to display the elements. Note that the ArrayList does not have the [] operator, like the vector in C++ does. It has but the get(index) method. The increment oper-ator used here is postfix, i++ in the parentheses, of the for-loop.

Comparing Java Iteration With That of C++

C++ can use a for-loop similarly to the previous example to access each element in the list with an itera-tor. In this case, C++ will also use the increment operator in the parentheses but for the iterator. In C++, an iterator is a class object pointer. The increment operator in C++ will move the pointer from one ele-ment to the next, not by adding 1.

In Java, the use of the iterator is different. The iterator object in Java has the next() method that returns the next element in the list related to the iterator. The next() method also advances the iterator to point to the next element. To know if the end of the list is reached, the iterator object uses its has next() method, which returns false if there is no more element left to be accessed.

With the Java iterator, the previous for-loop will be re-coded in the following program:

    import java.util.*;
    public class TheClass {
        public static void main(String[] args) {
            ArrayList<Character> al = new ArrayList<Character>();
            al.add('A'); al.add('B'); al.add('C'); al.add('D'); al.add('E');
            al.add('F'); al.add('G'); al.add('H'); al.add('I'); al.add('J');

            Iterator<Character> ite = al.iterator();

            for ( ; ite.hasNext() == true; ) {
                char ch = ite.next();
                System.out.print(ch); System.out.print(' ');
            }
            System.out.println();
        }
    }

The output is:

    A B C D E F G H I J

As expected.

After creating the iterator object, ite, there is the for-loop. Note that the initializing statement and the increment statement of the for-loop are absent. The while-condition of the for-loop is “ite.hasNext() == true”, which indicates as long as at least one more element is accessed in the list, the body of the for-loop has to be executed.

The previous for-loop is conventionally better written with a while-loop, instead of the for-loop, as in the following program:

    import java.util.*;
    public class TheClass {
        public static void main(String[] args) {
            ArrayList<Character> al = new ArrayList<Character>();
            al.add('A'); al.add('B'); al.add('C'); al.add('D'); al.add('E');
            al.add('F'); al.add('G'); al.add('H'); al.add('I'); al.add('J');

            Iterator<Character> ite = al.iterator();

            while (ite.hasNext() == true) {
                char ch = ite.next();
                System.out.print(ch); System.out.print(' ');
            }
            System.out.println();
        }
    }

The output is:

    A B C D E F G H I J

As expected.

The while-loop is more convenient to code because the initialization statement and the increment state-ment were absent in the for-loop.

Conclusion

In Java, ++ means adding 1 to the number held by the variable of interest. It can be postfix or prefix. The number can be an int or a double. Java does not have explicit pointers, so it cannot be used to in-crement the pointer in Java, as compared to C++. The ++ is known as the increment operator in Java.

With the postfix operation, the value of the variable is returned before the addition of 1 takes place. With prefix operation, the value of the variable is returned after the addition of 1 takes place. We hope you found this article helpful. Check more Linux Hint articles for tips and tutorials.

About the author

Chrysanthus Forcha

Discoverer of mathematics Integration from First Principles and related series. Master’s Degree in Technical Education, specializing in Electronics and Computer Software. BSc Electronics. I also have knowledge and experience at the Master’s level in Computing and Telecommunications. Out of 20,000 writers, I was the 37th best writer at devarticles.com. I have been working in these fields for more than 10 years.