Page 164 - Computer science 868 Class 12
P. 164
Let us see some examples:
int a = 56;
double b = 5.6;
float f = 56.74f;
The assignment operator has right to left associativity.
Shorthand Operator
Shorthand assignment operator is a special compound assignment operator as it provides an easy way to assign an
expression to a variable. This operator uses the combination of an arithmetic operator and an assignment operator.
For example:
var = var + 10;
In Java, you can also write the above statement like this:
var += 10;
There are various compound assignment operators used in Java:
Operator Meaning
+= Adds then assigns
-= Subtracts then assigns
*= Multiplies then assigns
/= Divides then assigns
%= Modulus then assigns
For example:
class shorthand
{
public static void main()
{
//Simple assigns
int num = 50;
System.out.println("Original Number : " + num);
//Increases by 10
num += 10;
System.out.println("Number : " + num);
//decreases by 5
num -= 5;
System.out.println("Number : " + num);
//Multiplies by 2
num *= 2;
System.out.println("Number : " + num);
//Quotient
num /= 2;
System.out.println("Number : " + num);
}
}
162162 Touchpad Computer Science-XII

