The assignment operator (=) is the most commonly used binary operator in Java. It evaluates the operand on the tight hand side and then assigns the resulting value to a variable on the left hand side. The right operand can be a variable, constant, function call or expression. The type of right operand must be type compatible with the left operand. The general form of representing assignment operator is
Variable = expression/constant/function call
a = 3; //constant
x = y + 10; //expression
In the first case, a literal value 3 is stored in the memory location allocated to variable a. In the second case, the value of expression y+ 10is evaluated and then stored in memory allocated to
Consider a statement x = y = z = 3;
Such type of assignment statement in which a single value is given to a number of variables is called multiple assignment statement i.e. value 3 is assigned to the variables x, y and z. The assignment operator (=) has a right to left associatively, so the above expression is interpreted as
(x = (y = (z = 3)));
Here, first the value 3 is assigned to z, then value stored in z is assigned to y and then finally y is assigned to x. Now let us consider a statement
x = 3 = 5;
This type of statement is invalid assignment statement and it will generate an error.
There is another kind of assignment statement which combines a simple assignment operator with five arithmetic binary operators and six bitwise operators. This type of operator is known as compound assignment operator. The general syntax of compound assignment operator IS
Variable operator= expression/constant/function call
For example: Suppose i is a variable of type int then the statement,
i += 5;
will add the literal 5 to the variable i and store the result back into the variable i. The various compound assignment operators and their effect are as shown in table
Such operator makes the statement concise so they are also called shorthand assignment operator. The expression a = a+b is almost same as that of a += b but the mainadvantage of shorthand assignment operator is that the operand is that the operand on the left handside of the assignment is evaluated only once. The assignment operators have the lowestprecedence as compared to other operators. Only one variable is allowed on the left hand sideof the expression. Therefore a=x*y is valid and m*n=l is invalid.
It is to be kept in mind that assignment operator (=) and equality operator (= =) are different. The assignment operator is used to assign a value to a variable whereas the equality operator used to compare two operands. These operators cannot be used in place of each other. The assignment operator( =) has lower precedence than arithmetic, relational, bitwise, and logical operators.Java Example to implement assignment operations
import java.util. *;
public static void main(String args)
int X=12, Y=13, Z=16;
System.out.println("The Assignment Value is : ");
System.out.println("The Value of X is : " +X);
System.out.println("The Value of Y is : " +Y);
System.out.println("The Value of Z is : " +Z);
Java Example to perform assignment operations using Scanner Class.
import java.util. *;
public static void main(String args)
Scanner scan=new Scanner(System.in);
System.out.print("Enter the Value of X : ");
System.out.print("Enter the Value of b : ");
System.out.println("X += 6 : "+(X+=6));
System.out.println("X -= 4 : "+(X-=4));
System. out. println("Y *= 4 : "+(Y*=4));
System. out. println("Y /= 6 : " +(Y/=6));
The Simple Assignment Operator
One of the most common operators that you'll encounter is the simple assignment operator "". You saw this operator in the Bicycle class; it assigns the value on its right to the operand on its left:
This operator can also be used on objects to assign object references, as discussed in Creating Objects.
The Arithmetic Operators
The Java programming language provides operators that perform addition, subtraction, multiplication, and division. There's a good chance you'll recognize them by their counterparts in basic mathematics. The only symbol that might look new to you is "", which divides one operand by another and returns the remainder as its result.
|Additive operator (also used for String concatenation)|
The following program, , tests the arithmetic operators.
This program prints the following:
You can also combine the arithmetic operators with the simple assignment operator to create compound assignments. For example, and both increment the value of by 1.
The operator can also be used for concatenating (joining) two strings together, as shown in the following program:
By the end of this program, the variable contains "This is a concatenated string.", which gets printed to standard output.
The Unary Operators
The unary operators require only one operand; they perform various operations such as incrementing/decrementing a value by one, negating an expression, or inverting the value of a boolean.
|Unary plus operator; indicates positive value (numbers are positive without this, however)|
|Unary minus operator; negates an expression|
|Increment operator; increments a value by 1|
|Decrement operator; decrements a value by 1|
|Logical complement operator; inverts the value of a boolean|
The following program, , tests the unary operators:
The increment/decrement operators can be applied before (prefix) or after (postfix) the operand. The code and will both end in being incremented by one. The only difference is that the prefix version () evaluates to the incremented value, whereas the postfix version () evaluates to the original value. If you are just performing a simple increment/decrement, it doesn't really matter which version you choose. But if you use this operator in part of a larger expression, the one that you choose may make a significant difference.
The following program, , illustrates the prefix/postfix unary increment operator:
« Previous • Trail • Next »