Free Ebook cover Learn to program in complete Java, from programming logic to advanced

Learn to program in complete Java, from programming logic to advanced

3.5

(4)

238 pages

Operators in Java (arithmetic, logical, etc.): Assignment Operators

Capítulo 32

Estimated reading time: 7 minutes

Audio Icon

Listen in audio

0:00 / 0:00

6.2. Operators in Java: Assignment Operators

Java, like many other programming languages, offers a variety of operators that are essential for manipulating data and variables. Understanding assignment operators is essential for anyone who wants to learn to program in Java, as they are constantly used to store values ​​in variables.

Basic Assignment Operators

The most basic assignment operator in Java is the equals sign (=). This operator is used to assign the value on the right side of the expression to the variable on the left side. For example:

int number = 10;

Here, the value 10 is assigned to the variable number. This means that the variable number now contains the value 10.

Composite Assignment Operators

In addition to the basic assignment operator, Java offers compound assignment operators. These combine an arithmetic or bitwise operation with assignment, simplifying certain operations. Compound assignment operators include:

  • += (addition assignment)
  • -= (subtraction assignment)
  • *= (multiplication assignment)
  • /= (division assignment)
  • %= (module assignment)
  • &= (bitwise AND assignment)
  • |= (bitwise OR assignment)
  • ^= (bitwise XOR assignment)
  • <<= (left shift assignment)
  • >>= (right shift assignment)
  • >>>= (unsigned right shift assignment)

These operators update the value of the variable with the result of the operation. For example:

Continue in our app.

You can listen to the audiobook with the screen off, receive a free certificate for this course, and also have access to 5,000 other free online courses.

Or continue reading below...
Download App

Download the app

int number = 5;
number += 3; // number is now 8, as 5 + 3 = 8

Here, number += 3 is equivalent to number = number + 3.

Arithmetic Operators

Arithmetic operators are used to perform common mathematical operations such as addition, subtraction, multiplication, division, and modulo. The arithmetic operators in Java are:

  • + (addition)
  • - (subtraction)
  • * (multiplication)
  • / (division)
  • % (module)

These operators can be used in conjunction with assignment operators to update the value of a variable. For example:

int sum = 10 + 5; // sum is 15
int difference = sum - 3; // difference is 12
int product = difference * 2; // product is 24
int quotient = product / 4; // quotient is 6
int remainder = quotient % 2; // remainder is 0

Logical Operators

Logical operators are used to perform Boolean operations, such as conjunction (AND), disjunction (OR) and negation (NOT). In Java, the logical operators are:

  • && (logical AND)
  • || (logical OR)
  • ! (NOT logical)

These operators are often used in control structures to test conditions. For example:

boolean estaRaining = true;
boolean temGuardaChuva = false;
boolean vaiSeMolhar = estaChovendo && !temGuardaChuva; // vaiSeMolhar is true

Comparison Operators

Comparison operators are used to compare two values. In Java, these operators are:

  • == (equal to)
  • != (not equal to)
  • > (greater than)
  • < (less than)
  • >= (greater than or equal to)
  • <= (less than or equal to)

These operators are crucial for testing conditions in decision structures and loops. For example:

int a = 10;
int b = 20;
boolean result = a < B; // result is true, as 10 is less than 20

Bitwise Operators

Bitwise operators operate at the bit level and are used to manipulate integer values ​​in their binary representation. The bitwise operators in Java are:

  • & (AND bitwise)
  • | (bitwise OR)
  • ^ (bitwise XOR)
  • ~ (bitwise complement)
  • << (left shift)
  • >> (right shift)
  • >>> (unsigned right shift)

These operators can be used to perform operations such as masking, setting or flipping specific bits. For example:

int bitmask = 0x000F;
int val = 0x2222;
int result = val & bitmask; // result is now 0x0002

In rIn summary, assignment operators and other operators in Java are powerful tools that allow programmers to write concise and efficient code. Understanding how and when to use each type of operator is essential for any Java developer, from entry-level to advanced.

Now answer the exercise about the content:

_Which of the following is a correct example of a compound assignment operator in Java?

You are right! Congratulations, now go to the next page

You missed! Try again.

Option 2 is correct. The += operator is an example of a compound assignment operator in Java. It adds the value on the right to the variable on the left and assigns the result back to the variable. In the example, number += 3; is equivalent to number = number + 3;.

Next chapter

Operators in Java (arithmetic, logical, etc.): Comparison Operators

Arrow Right Icon
Download the app to earn free Certification and listen to the courses in the background, even with the screen off.