Java Fundamentals  «Prev  Next»
Lesson 10Using Java Compound Assignment Operators
ObjectiveExamine Compound Assignment operators in Java.

Java Compound Assignment Operators : Boolean operators

Java also supports a number of Boolean, string, and assignment operators. Boolean operators are used to perform logical comparisons, and always result in one of two values: true or false. Following are the most commonly used Boolean operators:

Compound Assignment Operator

1) Simple assignment: Assigns a value to a variable
1) Simple assignment: Assigns a value to a variable

2) Addition: Adds a value to a variable and assigns the result to the variable
2) Addition: Adds a value to a variable and assigns the result to the variable

3) Subtraction: Subtracts a value from a variable and assigns the result to the variable
3) Subtraction: Subtracts a value from a variable and assigns the result to the variable

4) Multiplication: Multiplies a variable by a value and assigns the result to the variable
4) Multiplication: Multiplies a variable by a value and assigns the result to the variable

5) Division: Divides a variable by a value and assigns the result to the variable
5) Division: Divides a variable by a value and assigns the result to the variable

6) Modulus: Dividies a variable by a value and assigns the remainder to the variable
6) Modulus: Dividies a variable by a value and assigns the remainder to the variable


Arithmetic Compound Assignment Operators

Java provides special operators that can be used to combine an arithmetic operation with an assignment. As you probably know, statements like the following are quite common in programming:
a = a + 4;

In Java, you can rewrite this statement as shown here:
a += 4;

This version uses the += compound assignment operator. Both statements perform the same action: they increase the value of a by 4. Here is another example,
a = a % 2;

which can be expressed as
a %= 2;

In this case, the %= obtains the remainder of a /2 and puts that result back into a. There are compound assignment operators for all of the arithmetic, binary operators. Thus, any statement of the form
var = var op expression;

can be rewritten as
var op= expression;
The compound assignment operators provide two benefits. First, they save you a bit of typing, because they are “shorthand” for their equivalent long forms. Second, in some cases they are more efficient than are their equivalent long forms. For these reasons, you will often see the compound assignment operators used in professionally written Java programs.


Java Reference

Java String Operators

There is only one string operator, the concatenation operator (+), which appends a string onto another string. Following is an example of using the concatenation operator:
String nickname = "tough guy";
String message = "Hey there, " + nickname + "!";

The nickname string is effectively placed in the middle of a sentence. This code acts somewhat like a form letter in that you provide a sentence and then plug a name into it.
  • Assignment operators: Assignment operators all perform some type of operation that results in a value being stored in a variable. The following link contains the most commonly used assignment operators:

prime Application - Exercise

In this exercise, You will create a command-line application to determine whether a number is prime.
prime Application - Exercise

SEMrush Software