What Will I Learn?
- Basic arithmetic operators on Java
- Mod operator
- Making a number negative with "-" operator
- Increase "++" and Decrease "--" operators
Difficulty
- Basic
Tutorial Contents
Arithmetic Operators
Arithmetic operators are used to perform mathematical operations.
+ Additive operator
- Subtraction operator
* Multiplication operator
/ Division operation
% Remainder operator
++ Increment operator; increments a value by 1
-- Decrement operator; decrements a value by 1
Basic Arithmetic Operators ( +, -, *, /)
These operators are operators that do 4 operations that we know. Let's make an E.g.
package com.org.operators;
public class Operators2 {
public static void main (String[] args)
{
int x=8 ;
int y=5 ;
System.out.println (" addition result : " + (x+y) ) ;
System.out.println (" subtraction result : " + (x-y) ) ;
System.out.println (" multiplication result : " + (x*y) ) ;
System.out.println (" division result : " + (x/y) ) ;
}
}
At the screen we'll see;
addition result :13
subtraction result :3
multiplication :40
division result :1
The result of the divide operation is 1. The reason of this is, other number is in the int type too. If we want it to be fractional, we need to make any of our two values "double" or "float".
At this example, weenclosed the values in parentheses. If we do not do this, it gives error. Because "+" value combines both values and adds up. So it has two different mission. Parentheses are important here because of it.
Mod Operator ( % )
This operator gives us the result of the mode operation. Let's make an E.g.
package com.org.operators;
public class Operators2 {
public static void main(String[] args)
{
int number1=40 ;
int number2=6 ;
System.our.println (number1 % number2) ;
}
}
The screen will show;
4
As you can see, "%" operator divided two values and gave us the remaining. This operators can be use for different purposes. We usually use it when we need to find single and double numbers.
Making a number negative with "-" operator
The "-" operator that performs the subtraction operation also allows the numbers to be negative. It turns a pozitive value to negative. Let's make an example and see the result;
package com.org.operators;
public class Operators {
public static void main (String[] args)
{
int x=5 ;
x=-x ;
System.our.println (x+x) ;
}
}
What we'll see on the screen is;
-10
We put the operator "-" at the beginning of x, which is our variable, so that it is negative and we print it on the final screen.
Increase and Decrease operators( ++, -- )
These operators increment or decrement the value of a number by 1. But there is an exception in these operators. These operators can come either before or after the variable. Let's look at an example of what the differences are when they come before and after the variable.
package com.org.operators;
public class Operators4 {
public static void main (String[] args)
{
int number1=10 ;
int a,b ;
a=number1++ ;
b=number1 ;
System.out.println (" If it is written at the end :" +a) ;
System.out.println (" New value of the number :" +b) ;
}
}
We'll see on the screen;
If it is written at the end :10
New value of the number :11
As you can see, we were waiting the "a" value to become 11 because we used the increase operator. But there is an exeption as i said before, if we write the "++" at the end, it assign "number1" to the "a" first. So our "a" variable become 10. It makes the increasing operation after that. And than our number become 11. But this value (11) used in the bottom lines and it won't be effect to the line that we processed.
Let's make a more complex sample;
int number1=20 ;
int a,b,c,d,e,f ;
a=++number1 ;
b=number1-- ;
c=--number1 ;
d=++number1 ;
e=number1++ ;
f=number1 ;
System.out.println (" A value is " +a) ;
System.out.println (" B value is" +b) ;
System.out.println (" C value is" +c) ;
System.out.println (" D value is" +d) ;
System.out.println (" E value is" +e) ;
System.out.println (" F value is" +f) ;
We'll see on the screen;
A Value is 21
B value is 21
C value is 19
D value is 20
E value is 20
F value is 21
Let's take a look what have we done here,
- First, we increased "number1". After that, we assigned it to the "a". Than it become 21.
- At the bottom line, we assigned 21 to the "b" first. After that we decreased it and made it 20. So it become 20.
- At the 3rd line we decreased the 20 and assigned it to the "c". So it become 19.
- At the 4th line we increased 19 and assigned to the "d". Our number become 20 again.
- At the 5th line we assigned 20 to the "e" first. After that, we increased it by 1. So it become 21. This number can become usefull at the bottom line.
- At the end, we assigned the last value to the "f" and we printed on the screen.
Arithmetic operators have come to an end here. After this tutorial you've learn many operations can be done with those. At the next tutorial we're going to learn all other operators left.
Curriculum
- Partitioning the number using "_" - Boxing/Unboxing and Local Variables on Java
- Basic and Other Assignment Operators, Multiple Assignment Process and Variable Swap at Java
- What is Relational Operators on Java? -With examples
Posted on Utopian.io - Rewarding Open Source Contributors