Hi readers! This is my fourth article and it is about Java operators.I hope you all have read my other three articles and I was really happy to see many of your comments and opinions which made me forced to write my fourth article.
Operators and Operands
•An operator is a function that has a special symbolic name and is invoked by using that symbol with an expression.A value used on either side of an operator is called an operand.
In above example + is an operator.6 & 3 are operands
TYPES OF OPERATOR
- Arithmetic Operator [ +, -, *, /, % ]
- Assignment Operator [ =, +=, -=, *=, /=, %= etc…]
- Increment / Decrement Operator [ ++, — ]
- Relational Operator [ >, <, >=, <=, !=, == ]
- Logical Operator [ &&, ||, ! ]
- Bit wise Operator [ &, |, ^, ~ ]
- Conditional Operator [ ? : ]
Also according to the way of we are using operators, they can be categorized as follows.
- Unary(++, — , ==, !)// use one operand
- Binary(+, -, /, *, %) // use two operand
- Ternary( ? : ) // use three operand
Arithmetic operators are used in mathematical expressions in the same way that they are used in algebraic equations.
The + operator adds together two values such as two constants, a constant and a variable, or a variable and a variable. Here are a few examples of addition
int sum1 = 50 + 10;//two constants
int sum2 = sum1 + 66;//constant and a variable
int sum3 = sum2 + sum2;//two variables
int z=x+y;//answer is 9
String z="x"+"y"//answer is xy as collecting two strings
int num1 =50+10;//answer is 50+10=60
int num2=num1+66;//answer is 60 +66 =126
int num3 =num2+num2;//answer is 126+126 =252
The — operator subtracts one value from another.
int num1=1000 - 10; //answer is 990
int num2=num1 - 5; //answer is 985
int num3=num1 - num2;//answer is 5
The * operator multiplies two values.
int sum1 = 1000*2 //answer is 2000
int sum2 = sum1*10 //answer is 20,000
int sum3 = sum1*sum2 // answer is 40,000,000
The / operator divides one value by another.
double z=x/y; //answer is 1.5
The modulo (or remainder) math operation performs an integer division of one value by another, and returns the remainder of that division.
The operator for the modulo operation is the percentage (%) character.
int value = 23;
int res = value % 6; // res is 5
double z=x%y; // answer is 3.0
In this article I explained about what are operators,operands,types of operators and mainly about Arithmetic operators with examples.In next article I would like to write about other types of operators which that we should clearly understand.Your opinions are highly appreciated.Thank you.Keep in touch.