#include<stdio.h>
#include<conio.h>
main()
{
int a;
clrscr();
a=5; // Assigment OPerator
a += 10; // (a= a+ 10)
printf("%d",a);
getch();
}
-----------------------------------------------------------------------
<variable> = <expression>;
For example:
int counter = 1;
String name = "Nisha";
boolean rs = true;
Shape s1 = new Shape(); // creates new object
Shape s2 = s1; //assigning the reference of s1 to s2
counter = 5; // previous value is overwritten
In all cases a value of right side is being assigned to its type of variable lying to the left side. You can also assign a value to the more than one variable simultaneously. For example, see these expressions shown as:
x = y = z = 2;
x =(y + z);
Where the assignment operator is evaluated from right to left. In the first expression, value 2 is assigned to the variables "z", then "z" to "y", then "y" to "x" together. While in second expression, the evaluated value of the addition operation is assigned to the variable "x" initially then the value of variable "x" is returned.
Apart from "=" operator, different kind of assignment operators available in Java that are know ascompound assignment operators and can be used with all arithmetic or, bitwise and bit shiftoperators. Syntax of using the compound assignment operator is:
operand operation= operand
In this type of expression, firstly an arithmetic operation is performed then the evaluated value is assigned to a left most variable. For example an expression as x += y; is equivalent to the expression as x = x + y; which adds the value of operands "x" and "y" then stores back to the variable "x".
In this case, both variables must be of the same type.
No comments:
Post a Comment