More Arithmetic Operators
Introduction
We have studied arithmetic operators, relational operators, logical operators, etc. in previous units.
We are going to study some additional operators used more frequently like compound assignment operators, and increment and decrement operators in this unit.
1. Compound Assignment Operators
Compound assignment operators offer a more concise method for assigning an expression to a variable. Compound assignment operators are also referred to as Shorthand Assignment Operators.
There are various compound assignment operators used in Java: +=, -=, *=, /=, %=, etc.
Example 1:
class Main {
public static void main(String[] args) {
int a = 5;
a += 1; // same as a = a + 1;
System.out.println(a);
}
}
Output:
6
In the above code, a += 1 is the same as a = a + 1
Example 2:
Code
class Main {
public static void main(String[] args) {
int a = 10;
int b = 11;
a -= 2; // same as a = a - 2;
b %= 3; // same as b = b % 3;
System.out.println(a);
System.out.println(b);
}
}
Output:
8
2
2. Unary Operators
The unary operators are the operators which work on one operand and yield a new value. We have already studied some of the unary operators such as logical NOT (!) operator.
Let's learn a few more unary operators such as,
Increment Operator
Decrement Operator
Increment Operator
The Increment Operator (++), also referred to as Pre-increment or Pre-decrement operator, is an operator used to increment the value of a variable by 1, upon which it acts.
Increment operator can be implemented in two manners:
Prefix (++x)
Postfix (x++)
Prefix (++x)
If there is an Increment operator placed ahead of an operand, then that is referred to as a Prefix.
In the prefix increment, the value of the variable is increased first, and then the new value is utilized.
Syntax
++variable;
code:
class Main {
public static void main(String[] args) {
int a = 10;
++a; // a = 11
int number = ++a; // a = 12, number = 12
System.out.println("a = " + a);
System.out.println("number = " + number);
}
}
Output:
a = 12
number = 12
In the above code, the statement ++a increases the a value from 10 to 11.
In the declaration int number = ++a, first a will be incremented from 11 to 12, and then the value of a will be assigned to the number.
Postfix (x++)
If Increment operator is applied after an operand, then it is referred to as a Postfix.
In postfix, the value of the variable is initially used for calculation of the result and then incremented.
Syntax
variable++;
code:
class Main
public static void main(String[] args) {
int a = 10;
a++; // a = 11
int number = a++; // number = 11, a = 12
System.out.println("a = " + a);
System.out.println("number = " + number);
}
}
Output:
a = 12
number = 11
In the statement, a++ the a value will be incremented from 10 to 11.
In the expression int number = a++, the variable number will be set to the present value of a (11), and then a will be incremented from 11 to 12.
Decrement Operator
Decrement operator is an operator used to reduce the value of the variable by 1, on which it operates.
Like increment operator, the decrement operator is also used in two ways:
Prefix (--x)
Postfix (x--)
Prefix (--x)
Prefix decrement is the same as prefix increment, but the variable value is reduced by one rather than increased.
Syntax
--
variable;
Code
class Main {
public static void main(String[] args) {
int a = 10;
--a; // a = 9
int number = --a; // number = 8, a = 8
System.out.println("a = " + a);
System.out.println("number = " + number);
}
}
Output:
a = 8
number = 8
In the statement --a, the value of a will be reduced from 10 to 9.
In the expression int number = --a, first a will be decreased from 9 to 8, and then the new value of a will be copied to the number.
Postfix (x--)
Postfix decrement is the same as postfix increment, but here the variable value is decreased by one rather than being increased.
In the postfix, the value of the variable in the expression at the moment is employed, and then the variable value will be modified.
Syntax
variable--;
code:
class Main {
public static void main(String[] args) {
int a = 10;
a--; // a = 9
int number = a--; // number = 9, a = 8
System.out.println("a = " + a);
System.out.println("number = " + number);
}
}
Output:
a = 8
number = 9
In the line a--, the value of a will be reduced from 10 to 9.
In the expression int number = a--, the variable number will receive the value of a at that moment, and a will be decreased from 9 to 8.
Important Points:
The increment operator and decrement operator must be applied to variables and not to constants.
These operators will operate on all primitive types other than boolean
These operators will not operate on the variables declared as final
What happens if increment or decrement operators are applied to constants?
Code
class Main {
public static void main(String[] args) {
int num = 5;
num++;
7++;
System.out.println(num);
}
}
Output:
file.java:6: error: unexpected type
7++;
^
required: variable
found: value
1 error
An error will be thrown, as the increment and decrement operators should only be used on variables and not on constants.
Correct Code
class Main {
public static void main(String[] args) {
int num = 5;
num++;
System.out.println(num);
}
}
Output:
6
3. Escape Sequences
A character with a backslash \ just before a character is called an escape character or escape sequence. As in Python, the escape characters have a special meaning to the compiler.
The escape sequence begins with a backslash.
Escape sequences mostly used are:
-> New Line
\t -> Tab Space
\\ -> Backslash
\' -> Single Quote
\"" -> Double Quote
Let us see the purpose of escape characters with the following example. We have to display the following line: Hello user, "Welcome"
Code
class Main {
public static void main(String[] args) {
System.out.println("Hello user, \"Welcome\"");
}
}
Output:
Hello user, "Welcome
Let's see some additional uses of the escape sequences using the following example:
Code
class Main {
public static void main(String[] args) {
System.out.println("Hello user, \"Welcome\"."); // Use of \"
System.out.println("Hello, I am backslash \\."); // Use of \\
System.out.println("Hurray\tI am separated by a tab."); // Use of \t
System.out.println("Where are you? \nI am in a new line."); // Use of \n
}
}
Output:
Hello user, "Welcome".
Hello, I am backslash \.
Hurray I am separated by a tab.
Where are you?
I am in a new line.
In the above code,
\":" places a double quote symbol in the string where it will appear.
\\\\: places a backslash symbol in the string where it will be visible.
\\t: places a tab in the string where it will appear.
\\
: places a line return in the string where it will be present.
Potential Error
Output of double quotes (\") without applying the escape character (\).
Invalid Code
class Main {
public static void main(String[] args) {
System.out.println(""Hello World!"");
}
}
Output:
file.java:3: error: ')' expected
System.out.println(""Hello World!"");
^
file.java:3: error: ';' expected
System.out.println(""Hello World!"");
^
2 errors
In the above program, the compiler must be instructed that double quotes (") are not defining the beginning or the end of a string, but rather are to be outputted.
The example below outputs the statements enclosed with double quotes (\").
Correct Code
class Main {
public static void main(String[] args) {
System.out.println("\\\"Hello World!\"");
}
}
Output:
"Hello World!"
Summary
Compound assignment operators give a concise means of assigning an expression to a variable.
Increment Operator:
Prefix (++x): the value of the variable is incremented first, and the new value is used.
Postfix (x++): the value of the variable in the expression is used at present, and subsequently the variable value will be incremented.
Decrement Operator:
Prefix (--x): the value of the variable is decremented first, and the new value is used.
Postfix (x--): The value of the variable in the expression is taken currently, and afterwards, the variable value would be decreased.
Increment and Decrement operators cannot be applied to constants, final variables declared as final and boolean variables.
An escape character or escape sequence is a character that has a backslash \ immediately preceding a character.
Related Posts :
Java Introduction & Basics
Sequence of Instructions in Java
Java Data Types
Java Input and Output Basics
Java Type Conversions
Java String Methods
Java Logical Operators
Conditional Statements in Java
Java Ternary Operator
Java Switch Statement
Loops in Java
Java String Method