For basic mathematical operation the Java programming language provides arithmetic operators like addition (+), subtraction (-), division (/), multiplication(*) and modulus (%, which divides one operand by another and returns the remainder as its result).
In this post we'll see what all arithmetic operators are available in Java. Apart from that we'll also learn about Compound assignment operators, Unary operators and Increment and decrement operators in Java.
Arithmetic operators in Java
Operator | Description |
---|---|
+ | Additive operator (also used for String concatenation) |
- | Subtraction operator |
* | Multiplication operator |
/ | Division operator |
% | Remainder operator |
Java arithmetic operators example
public class ArithmeticDemo { public static void main(String[] args) { int num1 = 3; int num2 = 4; int result = num1 + num2; System.out.println("Addition result - " + result); // Subtraction result = result - 3; System.out.println("Subtraction result - " + result); // Division result = result/2; System.out.println("Division result - " + result); // Multiplication result = result * 6; System.out.println("Multiplication result - " + result); // Modulo division result = result % 8; System.out.println("Modulo division result - " + result); // overloaded + operator for string concatenation String str1 = "This is "; String str2 = "a string"; String concatString = str1 + str2; System.out.println("Concatenated String " + concatString); } }
Output
Addition result - 7 Subtraction result - 4 Division result - 2 Multiplication result - 12 Modulo division result - 4 Concatenated String This is a string
Compound assignment operators in Java
You can also combine the arithmetic operator with the assignment operator to create compound assignments. For example x = x + 7; can also be written as x += 7;
Operator | Description |
---|---|
+= | Addition assignment |
–= | Subtraction assignment |
*= | Multiplication assignment |
/= | Division assignment |
%= | Modulus assignment |
Java Compound assignment operators example
public class OperatorDemo { public static void main(String[] args) { int x = 5; int y = 6; int z = 7; int p = 4; int q = 16; x += 4; System.out.println("x - " + x); y -= 2; System.out.println("y - " + y); z *= 3; System.out.println("z - " + z); p /= 2; System.out.println("p - " + p); q %= 3; System.out.println("q - " + q); } }
Output
x - 9 y - 4 z - 21 p - 2 q – 1
Unary operators in Java
Operator | Description |
---|---|
+ | Unary plus operator; indicates positive value (numbers are positive by default though) |
- | Unary minus operator; negates an expression |
++ | Increment operator; increments a value by 1 |
-- | Decrement operator; decrements a value by 1 |
! | Logical complement operator; inverts the value of a boolean |
Java Unary operators example
Let’s see an example where unary plus operator, unary minus operator and logical component operator are used.
public class OperatorDemo { public static void main(String[] args) { // unary plus operator int x = +5; System.out.println("x = " + x); // unary minus operator x = -x; System.out.println("x = " + x); boolean flag = false; System.out.println("flag = " + flag); // logical component operator System.out.println("flag = " + !flag); } }
Output
x = 5 x = -5 flag = false flag = true
Increment and decrement operators in Java
The increment operator increases its operand value by 1. For example x = x + 1; can be written as x++; using increment operator.
Same way decrement operator decreases its operand value by 1. For example x = x – 1; can be written as x--; using decrement operator.
The increment/decrement operators can be applied before (prefix) or after (postfix) the operand. For example prefix code ++x; or the postfix code x++; both will result in x incremented by one.
Difference between prefix and postfix is that in prefix version operand is incremented/decremented and that value is used in the expression. Whereas in postfix version original value is used in the expression and then the operand is incremented/decremented.
As example-
x = 7; y = ++x;
Here y has the value 8 because the operand is incremented before using it in expression.
X = 7; y = x++;
Here y has the value 7 because the original value is used in the expression and then the operand is incremented. So x is 8 but y has the original value of x which was 7.
Java Increment and decrement operators example
public class OperatorDemo { public static void main(String[] args) { // prefix int x = 5; int y = ++x; System.out.println("x - " + x); System.out.println("y - " + y); // postfix int a = 8; int b = a++; System.out.println("a - " + a); System.out.println("b - " + b); y = --x; System.out.println("x - " + x); System.out.println("y - " + y); b = a--; System.out.println("a - " + a); System.out.println("b - " + b); } }
Output
x - 6 y - 6 a - 9 b - 8 x - 5 y - 5 a - 8 b - 9
That's all for this topic Arithmetic And Unary Operators in Java. If you have any doubt or any suggestions to make please drop a comment. Thanks!
>>>Return to Java Basics Tutorial Page
Related Topics
You may also like-
Great! You are doing nice work. I read this blog post with my interest. Really, this is wonderful post. Please publish new blog posts related college assignments such as PHP assignment, computer science, IT assignment help and android assignment help etc.
ReplyDelete