Advertisement

Responsive Advertisement

Operators In Java

 

Operators In Java






Java provides a rich set of useful operators that can be used in manipulating variables and for performing some simple arithmetic functions.
The values at both sides of the operator is called operands.
Examples of operators in Java.

+ addition

- Subtraction

/ Divisiom

* Multiplication

% Modulo (Remainder)

 

To perform an addition operation in Java, here is sample ways to do so.

 

int sum = 3 + 5;

 

 

In the previous lesson, we talked about datatypes and variables. “int” is the data type responsible for storing the value of sum.

+ is the operator and 3 , 5 are the operands.

In other ways?

 
int num1 = 3;
int
num2 = 5;

int
sum = num1 + num2;
 

 

The code above is the same as the first one we just deed. In this one we first initialized the values in a variable called num1 and num2 then later sum the two.

The same arithmetic operations can be done for other operators too.

 

int num1 = 5;
int
num2 = 3;

int
sum = num1 + num2;
int
difference = num1 - num2;
int
product = num1 * num2;
double
quotient = (double)num1 / (double)num2;  // This is a special case
int remainder = num1 % num2;

System.out.println(sum);
System.out.println(difference);
System.out.println(product);
System.out.println(quotient);
System.out.println(remainder);

 

OUTPUT.

 
8
2
15
1.6666666666666667
2
 

 

There is a special case in the division aspect.

We first stored num1 and num2 in an int which is for taking whole numbers.

Dividing 5 and 3 gives a decimal and cannot be stored into an int hence we have to convert int into a double.

This is done naming the data type double and declaring the double variables in front of each operand. It then converts it into a double variable and gives a decimal as an output.

Decrement and Increment Operators.

In some cases of writing your project, you might want to increase a value by a constant of which there is a simple conversion to do that.

For example: creating a simple snake game, you might want the size of the snake to increase by one anytime the snake takes an apple. This is how increment is used in that case.

 


int snake = 2;
System.out.println(++snake);
OUTPUT
3

The size of the snake is increased by one and then the new size is printed out.

Decrement:

On the other hands decrement reduces the size of the variable

Example


int snake = 2;
System.out.println(--snake);
OUTPUT
1

The output is now one since it is being decreased.

There are two types of decrement as well as two types of increment.

They are pre-increment and post-increment

Pre-decrement and post-decrement

Pre-increment and Post-increment

 
int snake = 2;
System.out.println(++snake); // Pre-increment adds one to the value before printing out the result.

System.out.println(snake++); // Post-increment prints out the result first
before adding one to it
 

 

Pre-decrement and post decrement

 
int snake = 2;
System.out.println(--snake); // Pre-decrement subtracts one from the value before printing out the result.

System.out.println(snake--); // Post-decrement prints out the result first
before subtracting one from it
 

 

For a clear understanding. Watch the video for more clarification.

Teaser!!!!

Write a program that converts 1km to 1000m.

Post a Comment

0 Comments

About

Welcome to Tech With Nate. Developing is the key and find comfort while you Tech With Nate