What does += do in Java with an example?

George 102 Published: 09/06/2024

What does += do in Java with an example?

I'll respond in English this time.

The += operator in Java is a shorthand for addition and assignment. It adds the value of the right-hand side operand to the left-hand side operand and assigns the result back to the left-hand side operand.

Here's an example:

int x = 5;

x += 3; // equivalent to x = x + 3;

System.out.println(x); // outputs 8

In this example, x is initially set to 5. Then, we use the += operator to add 3 to x. The result of the addition (which is 8) is assigned back to x.

This shorthand can be used with any numeric primitive type in Java, such as int, long, float, or double. For example:

long y = 10;

y += 5; // equivalent to y = y + 5;

System.out.println(y); // outputs 15

float z = 2.0f;

z += 4.5f; // equivalent to z = z + 4.5f;

System.out.println(z); // outputs 6.5

The += operator is often used in loops or when you need to update a variable incrementally.

In addition to numeric types, the += operator can also be used with Strings. In this case, it concatenates the two strings and assigns the result back to the left-hand side operand. For example:

String s = "Hello";

s += ", World!"; // equivalent to s = s + ", World!";

System.out.println(s); // outputs "Hello, World!"

Note that when using += with Strings, the resulting string is a concatenation of the two operands. This can be useful for building up a string incrementally, such as constructing a SQL query or generating an HTML page.

In summary, the += operator in Java is a convenient way to perform addition and assignment operations on numeric primitive types or Strings. It's often used in loops or when updating variables incrementally.

Assignment operators in java w3schools

I'll make sure to respond in English and provide the information you're looking for.

Assignment Operators in Java (W3Schools)

In Java, assignment operators are used to assign a value to a variable. There are several types of assignment operators, each with its own specific behavior:

= (Simple Assignment)

The simplest way to assign a value to a variable is using the equals sign (=). For example:

int x = 5;

This sets the value of x to 5.

+= (Addition Assignment)

The += operator adds the right-hand operand to the left-hand operand and assigns the result to the left-hand operand. For example:

int x = 5;

x += 3; // equivalent to x = x + 3

This sets x to 8 (5 + 3).

-= (Subtraction Assignment)

The -= operator subtracts the right-hand operand from the left-hand operand and assigns the result to the left-hand operand. For example:

int x = 5;

x -= 2; // equivalent to x = x - 2

This sets x to 3 (5 - 2).

* (Multiplication Assignment)

The *= operator multiplies the left-hand operand by the right-hand operand and assigns the result to the left-hand operand. For example:

int x = 5;

x *= 3; // equivalent to x = x * 3

This sets x to 15 (5 * 3).

/= (Division Assignment)

The /= operator divides the left-hand operand by the right-hand operand and assigns the result to the left-hand operand. For example:

int x = 10;

x /= 2; // equivalent to x = x / 2

This sets x to 5 (10 / 2).

%= (Modulus Assignment)

The %= operator calculates the remainder of the left-hand operand divided by the right-hand operand and assigns the result to the left-hand operand. For example:

int x = 10;

x %= 3; // equivalent to x = x % 3

This sets x to 1 (10 % 3).

&= (Bitwise AND Assignment)

The &= operator performs a bitwise AND operation on the left-hand operand and the right-hand operand, and assigns the result to the left-hand operand. For example:

int x = 5;

x &= 3; // equivalent to x = x & 3

This sets x to 1 (5 & 3).

|= (Bitwise OR Assignment)

The |= operator performs a bitwise OR operation on the left-hand operand and the right-hand operand, and assigns the result to the left-hand operand. For example:

int x = 2;

x |= 3; // equivalent to x = x | 3

This sets x to 3 (2 | 3).

^= (Bitwise XOR Assignment)

The ^= operator performs a bitwise XOR operation on the left-hand operand and the right-hand operand, and assigns the result to the left-hand operand. For example:

int x = 2;

x ^= 3; // equivalent to x = x ^ 3

This sets x to 1 (2 ^ 3).

<<= (Left Shift Assignment)

The <<= operator shifts the bits of the left-hand operand to the left by the number of positions specified by the right-hand operand, and assigns the result to the left-hand operand. For example:

int x = 5;

x <<= 1; // equivalent to x = x << 1

This sets x to 10 (5 << 1).

>>= (Right Shift Assignment)

The >>= operator shifts the bits of the left-hand operand to the right by the number of positions specified by the right-hand operand, and assigns the result to the left-hand operand. For example:

int x = 10;

x >>= 1; // equivalent to x = x >> 1

This sets x to 5 (10 >> 1).

Note that these operators work with both primitive types (such as int, double) and reference types (such as String).