Operators and Expressions
Operators and Expressions
operands.
For example, in a +b ,+ is an operator.
OPERAND: Operands are the values (constants or variables) on which operator operates.
For example, in a +b, a and b are operands.
TYPES OF OPERATORS:
1. ARITHMETIC OPERATORS: Arithmetic operators are those operators which are used to perform
arithmetic operations like addition, subtraction, multiplication, division etc.
Types of Arithmetic operators:
· Unary arithmetic operators
· Binary Arithmetic operators
· Unary Arithmetic operators: These operator works on one operand. They are:
o Unary plus (+): This operator is applied before the operand, which indicates that the
operand is positive. For example +3
o Unary minus (-):This operator is applied before the operand, which indicates that the
opera
o Increment operator(++):This operator increases the value of operand by one.
For example x++ or ++x means x=x+1
o Decrement operator(--):This operator decreases the value of operand by one.
For example x-- or --x means x=x-1
Page 1
· Postfix increment: When increment operator is applied after the operand then it is called
as Postfix increment. For example a++
This operator works on the principle “USE THEN CHANGE”. This means that the value of
the variable is used in expression and then changes .
· Prefix decrement: When decrement operator is applied before the operand then it is called
as Prefix decrement. For example --a
This operator works on the principle “CHANGE THEN USE”. This means that the value of
the variable changes before it is used in expression.
For example,
int a=3,b;
b=--a;
The values of a and b will be a=2 and b=2
· Postfix decrement: When decrement operator is applied after the operand then it is called
as Postfix increment. For example a--
This operator works on the principle “USE THEN CHANGE”. This means that the value of
the variable is used in expression and then changes .
int a=3,b;
b=a--;
The values of a and b will be a=2 and b=3
· Binary Arithmetic operators: These operator works on two operands. They are:
o Addition(+) : This operator add two operands and returns the sum.
o Subtraction(-) : This operator subtract one operand from other operand and returns
difference.
o Multiplication(*) : This operator is used to multiply two operands and returns product.
o Division(/) : This operator is used to divide two operands and returns Quotient.
o Modulus or Remainder(%) : This operator is used to divide two operands and returns
Remainder.
Page 2
Java provides special shorthand assignment operators.
+= a+=2 means a=a+2-= a-=4 means a=a-4
*- a*=5 means a=a*5
/= a/=4 means a=a/4
%= a%=5 means a=a%5
3. RELATIONAL OPERATORS: They are used to show relationship among the operands. They
compare variables and constants and returns true or false. The six relational operators are:
> greater than
< less than
>= greater than or equal to
<= less than or equal to
== equal to
!= not equal to
4. LOGICAL OPERATORS: They are used to perform logical operations. They result in true or false.
There are three logical operators in Java:
&& logical AND
|| logical OR
! logical NOT
· Logical AND: This operator works on two operands(result of relational expressions) and
returns true if both the expressions are evaluated to true.
e.g., 5>4 && 7<8 results true as both expressions results in true.
· Logical OR:This operator works on two operands (result of relational expressions) and
returns true if any of the expressions is evaluated to true.
e.g., 5>7 || 5<8 results true as result of second expression is true.
· Logical NOT: It is unary operator which reverts the outcome of expression.
e.g., !(8>3) is false as 8>3 is true.
HIGHER PRECEDENCE
· BRACKET()
· POST INCREMENT/DECREMENT
· PRE INCREMENT/DECREMENT
· UNARY PLUS/MINUS
· ARITHMETIC OPERATORS
o EXPONENT
o MULTIPLICATION, DIVISION(*,/,%)
o ADDITION,SUBTRACTION(+,-)
· RELATIONAL OPERATORS
· LOGICAL OPERATORS
o AND(&&)
o OR(| |)
· TERNARY
· ASSIGNMENT
LOWER PRECEDENCE
EXPRESSION:
An expression is a valid combination of operator and operands. The operands can be variables and
constants. For example c=a+b;
Types of expression:
· ARITHMETICAL EXPRESSION
An expression that contains variables , constants and arithmetical operators to perform any
calculation is called as arithmetical expression.
For example: a+b
x*y+z/8
b*b-4*a*c
Page 4
It is of two types:
o PURE ARITHMETIC EXPRESSION
An expression that contains all the operands of same data type is called as pure
expression. They are further of two types:
▪ Integer arithmetic expression: are those which contain only integer
operands. For example:
inta,b,c;
c=a+b;
▪ Real arithmetic expression: are those which contain only real(float-
ing) operands.For example:
double a,b,c;
c=a+b;
o IMPURE(MIXED) ARITHMETIC EXPRESSION
An expression which contains different types of operands is called as Impure
expression. For example,
int a;
float b;
double c=a+b;
· BOOLEAN EXPRESSION
Expressions that result into true or false are called a s Boolean Expressions.
TYPE CONVERSION:
Java performs any operation on data types which are compatible i.e., of same data type. When in
an expression (mixed expression) the operands are of different data types i.e., not compatible then one
data type will be converted into another before performing the operation. This is known as type conver-
sion. The type conversion can be done in two ways:
· Implicit type conversion
· Explicit type conversion
println() will print the message on the screen and then changes the line causing the next output to be
printed on different line.
For example:
System.out.println(“GOOD”);
System.out.println(“MORNING”);
OUTPUT: GOOD
MORNING
Page 12
10
11
Page 7
8
9