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:
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.
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: