- 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".
The assignment operator (=) in Java is used to assign a value to a variable. In this case, x = 10 assigns the value 10 to the variable x.
public class AssignmentExample { public static void main(String[] args) { // Declare a variable int x; // Assign a value to the variable x = 10; // Display the value of x System.out.println("The value of x is: " + x); } }
When you run this Java program, it will output "The value of x is: 10". This demonstrates the assignment of the value 10
to the variable x
.
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 a *= b
is a compound assignment operator, which multiplies the value of a
by the value of b
and assigns the result back to a
. It is equivalent to the expression a = a * b
.
If a
is initially 5
and b
is 2
, the result of the expression a *= b
would be:
a = a * b;
After the operation, the value of a
will be 10
(5 * 2).
Here's an example in Java:
public class CompoundAssignmentExample { public static void main(String[] args) { // Declare variables int a = 5; int b = 2; // Compound assignment a *= b; // Display the result System.out.println("The value of a after a *= b is: " + a); } }
When you run this Java program, it will output "The value of a after a *= b is: 10".
In Java, the expression q -= r
is a compound assignment operator, which subtracts the value of r
from q
and assigns the result back to q
. It is equivalent to the expression q = q - r
.
If q
is initially some numeric value and r
is another numeric value, the result of the expression q -= r
would be:
q = q - r;
Here's an example in Java:
public class CompoundAssignmentExample { public static void main(String[] args) { // Declare variables int q = 8; int r = 3; // Compound assignment q -= r; // Display the result System.out.println("The value of q after q -= r is: " + q); } }
In this example, if q
is initially 8
and r
is 3
, after the operation, the value of q
will be 5
(8 - 3).
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.
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