0% found this document useful (0 votes)
37 views5 pages

Ch4 - Operators in Java

Uploaded by

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

Ch4 - Operators in Java

Uploaded by

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

Ch 4 – Operators in Java

 Difference b/w Operator & Operands

Operator – is a symbol or token, which performs arithmetical or logical operations and gives
meaningful result.

Operands – are values which are involved in the operation.

Operator

c= a + b

Operands

 Difference b/w Arithmetic Statement and Arithmetic Expression

Arithmetic Expression – contains variables, constants and arithmetical operators.

eg. a + b

Arithmetic Statement – is when an arithmetic expression is assigned to a variable.

eg. c = a + b

 Types of Operators

1. Arithmetical Operators – used to perform arithmetical calculations in a program.

eg. + - * / %

2. Relational Operators – used to compare the value of two operands and determine the
relationship between them. Its result will always be a boolean value (true or false).

eg. < > <= >= == !=

3. Logical Operators - used to compare two relational expressions. Result will be true or
false.

eg. && (AND), || (OR), ! (NOT)


 Java Expression –

These are arithmetical expression in which only java arithmetic operators are used.

Arithmetic expression Java expression

ab (1/3) * a * b

2( l+b) 2 * ( l+b)

a*a

 Types of Arithmetic Operators

1. Unary operator – deals with only one operand

eg. + - ++ --

+ ,-  determines whether the values are positive or negative

++  increases the value by one (increment operator)

eg. int a = 6;

a++; or ++a;

System.out.println (a);

Output  7

--  decreases the value by one (decrement operator)

eg. int a = 6;

a--; or --a;

System.out.println (a);

Output  5
 Difference between

Prefix Postfix

Change before action Change after action

++a a++

eg. P=5; eg. P=5;


p= ++p * 4 p= p++ * 4
p= 6 * 4 = 24 p= 5 * 4 = 20

Counter Accumulator

A numeric variable used to keep a A numeric value which gets updated


record of the number of times a with counter or input value
process is repeated.

s=0;
c=0;
s=s+a;
c++;
** where a is the input value

Unary Binary

Deals with one operand Deals with two operands

a++ a+b

= ==

Assignment operator Relational operator

a=5 if ( a == b )
/ %

Division operator Modulus Operator

Result will be quotient Result will be remainder

eg. 5 % 2 = 1
eg. 5 / 2 = 2
6%2=0
5.0 / 2 = 2.5
4%6=4

 Binary Operator – deals with two operands

eg. + - * / %

 Ternary Operator (Conditional Assignment)– deals with three operands

Syntax: Variable = (test condition) ? Expression1 : Expression2

eg. X = ( a < b ) ? 20 : 5

 Define :

1. Dot operator – facilitates invoking members of the class to carry out tasks.

eg. java.util.Scanner , System.out.print()

2. new operator – used for dynamic allocation of the object in memory space

eg. sum ob = new sum();

 Output Statement

System.out.print() – cursor remains in the same line.

eg. System.out.print(“Best ”);

System.out.print(“of Luck”);

Output  Best of Luck


System.out.println() – cursor is shifted to the next line

eg. System.out.println(“Best”);

System.out.print(“of Luck”);

Output  Best

of Luck

You might also like