0% found this document useful (0 votes)
3 views13 pages

Operators in Java

Uploaded by

dikshathakur
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views13 pages

Operators in Java

Uploaded by

dikshathakur
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Operators

We use Operators to perform operations on variables and values.


Operators are represented by symbols. The variables and values
on which operators perform operation are called operands. Let’s
look at this code snippet:

Example: int x = a + 10;

Here, plus sign is the operator, the variable a and the literal value
10 are operands.
Addition using scanner

import java.util.Scanner; // Import the Scanner class

public class MyClass {


public static void main(String[] args) {
int x, y, sum;
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Type a number:");
x = myObj.nextInt(); // Read user input

System.out.println("Type another number:");


y = myObj.nextInt(); // Read user input

sum = x + y;
System.out.println("Sum is: " + sum); // Output user input
}
}
public class CompoundOperatorExample {
// Using *= (multiplication assignment)
public static void main(String[] args) { num1 *= num2; // Equivalent to num1 = num1 *
num2;
int num1 = 10; System.out.println("num1 after *=: " + num1);
int num2 = 5;
// Using /= (division assignment)
// Using += (addition assignment) num1 /= num2; // Equivalent to num1 = num1 /
num2;
num1 += num2; // Equivalent to num1 = num1 +
num2; System.out.println("num1 after /=: " + num1);
System.out.println("num1 after +=: " + num1);
// Using %= (modulus assignment)
// Using -= (subtraction assignment) num1 %= num2; // Equivalent to num1 = num1
% num2;
num1 -= num2; // Equivalent to num1 = num1 -
num2; System.out.println("num1 after %=: " + num1);
System.out.println("num1 after -=: " + num1); }
}
Forms of
Operators

• Depending on the number of operands


an operator operates upon, we classify
them into 3 forms:

• Unary
• Binary
• Ternary
• Unary Operators work on a single
operand, binary on 2 operands and
ternary on 3 operands. Most of the
operators in Java are binary, a few are
unary. There is only one ternary
operator in Java.
Types of Operators

• Based on the type of operation performed by the operator,


we can group them into the following types:

• Arithmetic
• Relational
• Logical
• Assignment
• Bitwise
• We will look at Arithmetic, Relational, Logical and
Assignment type of operators
Basic Arithmetic Operators

• Arithmetic operators to perform common mathematical


operations. Operands of Arithmetic operators must
be of numeric type. This implies that Arithmetic
operators can operate on byte, short, int, long, float,
double and char. But they cannot operate on boolean.
Table lists all the different Arithmetic operators in
Java:
Operator Name Description Example

Unary + Unary Plus


Returns value of the
operand +x
Unary - Unary minus
Returns negated
value of the operand -x
+ Addition
Adds together two
values x+y
- Subtraction
Subtract one value
from another x-y
* Multiplication Multiplies two values x*y
/ Division
Divides one value
from another x/y
% Modulus
Returns the division
remainder x%y
Increases the value ++x or
++ Increment of a variable by 1
x++
Decreases the value
public class BasicArithmeticOperators
{
public void demoBasicArithmeticOps()
{
Int a = 10 + 10;
int b = a * 3;
Int c = b / 4;
int d = c - a;
int e = -d;

System.out.println("a = " + a);


System.out.println("b = " + b);
System.out.println("c = " + c);
System.out.println("d = " + d);
System.out.println("e = " + e);

}
}
Difference Between Integer and Floating-Point Division

public class IntegerDivision


{
public void
demoIntegerDivision()
{
int a = 18;
double b = a / 4;
System.out.println("b = " + b);
}
}
The result is 4.0 and not 4.5. When both
operands are of integer type, the division
operator does not include the fractional
To get the fractional component in the result, one operand needs
to be of floating-point type like this
public class FpDivision
{
public void demoFpDivision()
{
int a = 18;
/* * Here, the denominator
*is written as a
* floating-point literal
* 4.0 instead of integer
* literal 4 */

double b = a / 4.0;
System.out.println("b = " + b);
}
}
Now the result is 4.5 because we have written the
denominator of the division operator as a floating-
point literal 4.0 instead of an integer literal 4.
The modulus operator computes the remainder of a division
operation

public class ModulusOperator


{
public void computeModulus()
{
/* * Modulus operator computes * the remainder of a * division
operation. */
int a = 59;
double b = 59.75;
System.out.println("a % 10 = " + a % 10);
System.out.println("b % 10 = " + b % 10);
}
}
Dividing 59 by 10 gives 9 as the remainder and
dividing 59.75 by 10 gives 9.75 as the remainder.
Increment and Decrement Operators

public class PrefixIncrement


{
public void demoPrefixIncr()
{
int a = 99;
/* * With Prefix increment first a is incremented to 100. After that a is assigned to b */
int b = ++a;
System.out.println("a = " + a);
System.out.println("b = " + b);
}
}
Prefix form follows the CHANGE-THEN-USE rule.
Post fix
public class PostfixIncrement
Postfix form follows the USE-THEN-
{ public void demoPostfixIncr()
CHANGE rule. In the postfix case,
{
int a = 99; first the original value of a which is 99
/* * With Postfix increment first a is assigned to b. After that a is
gets assigned to b. After the
incremented */
int b = a++; assignment, the postfix increment
System.out.println("a = " + a); operator increments the value of a to
System.out.println("b = " + b); 100. Once the two statements finish
} executing, a will have the value of
} 100 and b will have the value of 99.

You might also like