Operators in Java(part1)

Damitha karunarathne
3 min readMay 4, 2020

--

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.

int x=6+3;

In above example + is an operator.6 & 3 are operands

TYPES OF OPERATOR

  1. Arithmetic Operator [ +, -, *, /, % ]
  2. Assignment Operator [ =, +=, -=, *=, /=, %= etc…]
  3. Increment / Decrement Operator [ ++, — ]
  4. Relational Operator [ >, <, >=, <=, !=, == ]
  5. Logical Operator [ &&, ||, ! ]
  6. Bit wise Operator [ &, |, ^, ~ ]
  7. Conditional Operator [ ? : ]

Also according to the way of we are using operators, they can be categorized as follows.

  1. Unary(++, — , ==, !)// use one operand
  2. Binary(+, -, /, *, %) // use two operand
  3. Ternary( ? : ) // use three operand

Arithmetic Operators

Arithmetic operators are used in mathematical expressions in the same way that they are used in algebraic equations.

Addition

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

ex1:

int x=2;
int y=7;
int z=x+y;//answer is 9
int z="x"+"y";//error
String z="x"+"y"//answer is xy as collecting two strings

ex2:

 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

Subtraction

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

Multiplication

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

Division

The / operator divides one value by another.

double x=3;
double y=2;
double z=x/y; //answer is 1.5

Modulo

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.

ex1:

int value = 23;
int res = value % 6; // res is 5

ex2:

double x=3;
double y=2000;
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.

--

--