- A =
- B ==
- C :=
- D =>
The assignment operator (=) in Java is used to assign a value to a variable. It takes the value on the right-hand side and assigns it to the variable on the left-hand side.
The assignment operator in Java is =
. Here's an example:
public class AssignmentExample { public static void main(String[] args) { // Assigning value to a variable int number = 42; // Displaying the value System.out.println("The value of number is: " + number); } }
In this example, the value 42
is assigned to the variable number
using the assignment operator (=
). When you run this Java program, it will output "The value of number is: 42".
In Java, the expression y += z
is a compound assignment operator, which adds the value of z
to the current value of y
and assigns the result back to y
. It is equivalent to the expression y = y + z
.
If y
is initially 7
and z
is 3
, the result of the expression y += z
would be:
y = y + z;
After the operation, the value of y
will be 10
(7 + 3).
Here's an example in Java:
public class CompoundAssignmentExample { public static void main(String[] args) { // Declare variables int y = 7; int z = 3; // Compound assignment y += z; // Display the result System.out.println("The value of y after y += z is: " + y); } }
When you run this Java program, it will output "The value of y after y += z is: 10".
In Java, the expression p /= 2
is a compound assignment operator, which divides the value of p
by 2
and assigns the result back to p
. It is equivalent to the expression p = p / 2
.
If p
is initially some numeric value, the result of the expression p /= 2
would be:
p = p / 2;
Here's an example in Java:
public class CompoundAssignmentExample { public static void main(String[] args) { // Declare variable double p = 10.0; // Compound assignment p /= 2; // Display the result System.out.println("The value of p after p /= 2 is: " + p); } }
In this example, if p
is initially 10.0
, after the operation, the value of p
will be 5.0
(10.0 / 2).
In Java, the expression x %= y
is a compound assignment operator, which calculates the remainder of the division of x
by y
and assigns the result back to x
. It is equivalent to the expression x = x % y
.
If x
is initially some numeric value and y
is another numeric value, the result of the expression x %= y
would be:
x = x % y;
Here's an example in Java:
public class CompoundAssignmentExample { public static void main(String[] args) { // Declare variables int x = 6; int y = 2; // Compound assignment x %= y; // Display the result System.out.println("The value of x after x %= y is: " + x); } }
In this example, if x
is initially 6
and y
is 2
, after the operation, the value of x
will be 0
(the remainder of 6 divided by 2).
In Java, the expression a <<= 2
is a compound assignment operator that performs a left shift on the variable a
by the number of bits specified on the right side of the <<=
operator. It is equivalent to the expression a = a << 2
.
Here's an example in Java:
public class CompoundAssignmentExample { public static void main(String[] args) { // Declare variables int a = 5; // Compound assignment (left shift by 2 bits) a <<= 2; // Display the result System.out.println("The value of a after a <<= 2 is: " + a); } }
In this example, if a
is initially 5
, after the operation, the value of a
will be 20
. This is because the binary representation of 5
is 0000 0101
, and left-shifting it by 2 bits results in 0001 0100
, which is 20
in decimal.
In Java, the expression b >>= c
is a compound assignment operator that performs a right shift on the variable b
by the number of bits specified on the right side of the >>=
operator. It is equivalent to the expression b = b >> c
.
Here's an example in Java:
public class CompoundAssignmentExample { public static void main(String[] args) { // Declare variables int b = 12; int c = 2; // Compound assignment (right shift by 2 bits) b >>= c; // Display the result System.out.println("The value of b after b >>= c is: " + b); } }
In this example, if b
is initially 12
, after the operation, the value of b
will be 3
. This is because the binary representation of 12
is 1100
, and right-shifting it by 2 bits results in 0011
, which is 3
in decimal.
In Java, the expression m &= 5
is a compound assignment operator that performs a bitwise AND between the variable m
and the value 5
. It is equivalent to the expression m = m & 5
.
Here's an example in Java:
public class CompoundAssignmentExample { public static void main(String[] args) { // Declare variables int m = 10; // Compound assignment (bitwise AND with 5) m &= 5; // Display the result System.out.println("The value of m after m &= 5 is: " + m); } }
In this example, if m
is initially 10
, after the operation, the value of m
will be 0
. This is because the binary representation of 10
is 1010
, and performing a bitwise AND with 5
(binary 0101
) results in 0000
, which is 0
in decimal.
class PostAndPre { public static void main( String args[]) { int a = 2; int b = a++ - a++; System.out.println(b); System.out.println(a); } }
class PostAndPre { public static void main( String args[]) { int a = 2; int b = a++ - a++; System.out.println(b); System.out.println(a); } }
class PostAndPre { public static void main( String args[]) { int a = 2; System.out.println(a++ - a++ +" Now a = "+a); } }
-1 Now a = 4