++ && != and %= Java Operators Explained

My discussion assignment at school was to explain the following operators in Java; ++ (increment) && (logical AND) != (not equal) %= (modulus assignment).

I thought the quality of my post was good enough to share here, so here it is.


The Increment Operator (++)

The ++ operator is a special short hand assignment, used to add the value of 1 to a variable. It is called the increment operator and is commonly used to increment a variable that is being used as a counter. The long hand version of which looks like this (assuming the variable has already been declared and initialized).

someVariable = someVariable + 1;

Another short hand for the same thing would be:

someVariable += 1;

The increment operator offers the shortest possible way to do this:

someVariable++;

Logical AND (&&)

The && operator is a logical operator called “logical AND”. It is used to create conditional expressions such as “if this AND that, then do something”. An example of how you might use the && operator in real life is that you only wear your rain coat when it is raining AND you are outside; not one or the other but both. A code example will follow after we look at the != operator.

Not Equal (!=)

The != operator is a comparison operator, also used in conditional expressions. It reads, “not equal”. If the compared values are not equal to each other than the expression returns true.

An example of a program that uses both the && and != operator could be a program that multiplies two numbers but only if they are both non-zero values.

int num1;
int num2;
// insert code to get input from user here.
if (num1 != 0 && num2 != 0) {
    // prints the product of num1 and num2
    System.out.println(num1 * num2); 
}

Modulus Assignment (%=)

To understand the last operator, %=, let’s look at what % means in Java. % is the modulus operator, NOT percent! What it does is divide two numbers and return the “remainder”. So 5 % 2 is 1, since 2 goes into 5 twice with a remainder of 1. The %= operator is similar to the += operator we looked at earlier. It performs modulus division on a variable with a literal or another variable and returns the result into the variable on the left of the operator. That probably sounds confusing… so let’s look at the code.

int number = 5;
// right now number == 5 (== means "is equal to")
number %= 2;
// now number == 1

So,

number %= 2;

is the same thing as:

number = number % 2;

More on != and && Operators

One last note on the != and && operators; although it’s common to use these operators inside of conditional expressions, it’s also possible to use them when assigning to booleans.

boolean notEqual = 4 != 5;
// notEqual is true becuase 4 does not equal 5. 

boolean bothTrue = true && false; 
// bothTrue is false because a false and true evaluate to false in boolean logic.

Other Operators

Lastly, Java has many, many more operators, and it’s a good idea to have a list of all the operators as a reference.

Even if Java is your first language, you are likely to learn a few more, and although many popular languages are similar, you are never going to memorize all of the minute differences between them, so a list of operators is handy to have saved as a pdf, or bookmark in your browser.

Here is one that is quick and easy to scan for what you’re looking for: http://www.tutorialspoint.com/java/java_basic_operators.htm


If you have any questions or suggestions on this article, please share your comments! I’m always willing to help when I can, and am always open to improving my posts.

Thanks for reading,

Dan

Leave a Reply