Increment and decrement operators in Java with Examples PDF

Geoffrey 36 Published: 09/26/2024

Increment and decrement operators in Java with Examples PDF

I cannot create a PDF, but I can provide you with the information on increment and decrement operators in Java.

In Java, there are three types of operator that can be used to manipulate variable values: increment (++), decrement (--), and compound assignment.

Increment Operator

The ++ operator is used to increase the value of a variable by 1. It has two forms:

Prefix form: a++ means first evaluate a, then add 1 to its value, and assign the new value back to a. Postfix form: ++a means add 1 to a, and then evaluate it.

Example:

int x = 5;

System.out.println(x++); // prints 5

System.out.println(x); // prints 6

int y = 10;

System.out.println(++y); // prints 11

System.out.println(y); // prints 11

Decrement Operator

The -- operator is used to decrease the value of a variable by 1. It also has two forms:

Prefix form: a-- means first evaluate a, then subtract 1 from its value, and assign the new value back to a. Postfix form: --a means subtract 1 from a, and then evaluate it.

Example:

int x = 5;

System.out.println(x--); // prints 5

System.out.println(x); // prints 4

int y = 10;

System.out.println(--y); // prints 9

System.out.println(y); // prints 9

Compound Assignment Operators

The compound assignment operators are used to modify a variable and assign the new value back to it in one statement. The most common ones are:

a += b is equivalent to a = a + b a -= b is equivalent to a = a - b a *= b is equivalent to a = a * b a /= b is equivalent to a = a / b

Example:

int x = 5;

x += 2; // equivalent to x = x + 2, so now x is 7

int y = 10;

y -= 3; // equivalent to y = y - 3, so now y is 7

These operators are useful in various situations where you need to modify a variable's value based on its current value.

Binary operator in Java

I'm happy to respond in English!

The binary operators in Java are the foundation of arithmetic operations. They can be classified into several categories:

Arithmetic Operators: Addition: + (Example: int a = 5; int b = 3; int result = a + b;) Subtraction: - (Example: int a = 5; int b = 3; int result = a - b;) Multiplication: * (Example: int a = 5; int b = 3; int result = a * b;) Division: / (Example: int a = 10; int b = 2; double result = a / (double) b;) Modulus: % (Example: int a = 17; int b = 5; int result = a % b;) Relational Operators: Equal to: == (Example: boolean isTrue = a == b;)

Not equal to: != or <> (Example: boolean isFalse = a != b;) Greater than: > (Example: boolean isGreater = a > b;) Less than: < (Example: boolean isLess = a < b;) Greater than or equal to: >= (Example: boolean isGreaterEqual = a >= b;) Less than or equal to: <= (Example: boolean isLessEqual = a <= b;) Logical Operators: And: && (Example: boolean result = true && false;) Or: || (Example: boolean result = true || false;) Not: ! or ~ (Example: boolean result = !true;) Assignment Operators: Addition assignment: += (Example: int a = 5; a += 3;) Subtraction assignment: -= (Example: int a = 5; a -= 3;) Multiplication assignment: *= (Example: int a = 5; a *= 3;) Division assignment: /= (Example: double a = 10.0; a /= 2.0;) Modulus assignment: %= (Example: int a = 17; a %= 5;)

These binary operators are used to perform various operations on values in Java programming language.

Additional information:

In Java, the binary operator can be combined with other operators to form more complex expressions. For example:

int a = 5;

int b = 3;

System.out.println(a + (b * 2)); // prints 11

This expression uses addition and multiplication operators in combination with parentheses to calculate the result.

Note that Java is case-sensitive, so it's essential to follow the correct capitalization when using binary operators. For example, + for addition and - for subtraction.