How to Use Assignment Operators in Java
Java has a number of assignment operators, or statements which adjust the value of a variable. Besides the simple assignment and initialization operator, there are a number of shorthand arithmetic operators to reduce repetition.
Instructions
-
Use the Assignment and Initialization Operator
-
1
Use the assignment operator, the symbol "=", to assign the value of an expression to a variable. This is the most commonly used operator in Java.
-
2
Assign to a variable as it's being declared by using variable initialization. This a slight variation of the assignment operator. The symbol is the same and the initialization operator can also assign to a final (constant) variable.
-
-
3
Type your Java variable so that the left-hand variable is the same type as the right-hand expression. Otherwise, there must be an implicit conversion from the right-hand type to the left-hand type (such as float to int). For example, you cannot assign a string to an int variable.
""int a = 7; // Initialization
final int b = 10; // Final initializationa = b * 10; // Right-hand type is int (int multiplied by an int yields an int)
float c = 21 / 2; // Right-hand type is actually int, implicitly converted to float
float d = 21.0 / 2; // Right-hand type is float""
Use the Increment and Decrement Operators
-
4
Use increment and decrement operators, the shorthand operators for the action of adding or subtracting 1 from a variable.
-
5
Realize that each increment and decrement operator comes in two forms, the prefix and postfix forms. The postfix form evaluates to the variable, incrementing and decrementing it afterward. The prefix version increments and decrements before evaluating, so that the expression evaluates to the variable's new value.
""int a = 10;
int b = a++; // b = 10, a = 11
int c = ++b; // c = 11, b = 11
c--; // c = 10""
Use the Combination Operators
-
6
Combine an arithmetic operator and an assignment operator by using the arithmetic and assignment combination operators. The combination operators are the equivalent of applying the arithmetic operator with the right-hand side of the expression to the variable, and assigning the result to the variable. For example, "a += 10;" is shorthand for "a = a + 10;".
-
7
Use anything that can go on the right-hand side of the arithmetic expression as the type on the right-hand side of the expression. There are five of these operators: addition (+=), subtraction (-=), multiplication (*=), division (/=) and modulus (%=).
""int a = 10;
a /= 2; // a = a / 2
a *= 32; // a = a * 32
a += 2; // a = a + 2"" -
8
Employ the shift and assignment combination operators in your Java code. Similar to the arithmetic assignment combination operators, these operators perform bitwise shifts on integers instead of arithmetic.
-
9
Utilize one of the three variants: <<= (shift left assignment), >>= (shift right assignment) and >>>= (shift right without sign extension assignment). The operators are unique in that both the left -and right-hand side of the operator must be integers.
""int a = 128;
a >>= 4; // a = a >> 4"" -
10
Work with the bitwise and assignment combination operators. These are a lot like the shift operators in that they perform a bitwise operation (AND, OR or XOR) before assignment. The type of both the left- and right-hand side must be an integer. There are three variants: &= (AND assignment), |= (OR assignment) and ^= (XOR assignment).
-
1
Tips & Warnings
The use of the "//" symbol notes that text following it is a programming note, not usable code.