Sunteți pe pagina 1din 3

Chapter-2 Part-II

Introduction to a Class
Relational Operators: These Operators are used to find the relation between two operands of numeric or character
data type. The result of these operators is a boolean value.
Sl.no
1
2

Symbol
<
>

<=

>=

5
6

==
!=

Description
Less than
Greater than
Less than or equal
to
Greater than or
equal to
Equal to
Not equal to

Example:
int x=10,y=5;
System.out.println (x<y);
System.out.println (x>y);
System.out.println (x<=y);
System.out.println (x>=y);
System.out.println (x= =y);
System.out.println (x!=y);
Logical Operators: These operators are used to check two or more relational expressions and gives the result in
boolean data type.
Sl.no
1
2
3

Example:
int x=10,y=5, z=3;
System.out.println (x<y & z>x);
System.out.println (x>y | z<x);
System.out.println (!(x<y));

Symbol
&
|
!

Description
AND
OR
NOT

System.out.println (x<y ^
opposite result, then the result is true.

y>z);

//

In

this

case

both

the

operands

should

have

Short-Circuit Operators: These operators work the same how logical operators work. But short-circuit operators
conditionally evaluates the second operand.
Sl.no
1
2

Symbol
&&
||

Description
AND
OR

Example:
int x=10,y=5, z=3;
System.out.println (x<y && z>x);
System.out.println (x>y | |z<x);
Unary Operators: These operators are used to perform operation on single operand only.
Sl.no

Symbol

3
4
5
6

opr++
opr-++opr
--opr

Description
Unary + for indicating +ve value
and also promotes char or byte
or short value to int data type
Unary indicates the number is
negative
Postfix or post increment
Postfix or post decrement
Prefix or pre increment
Prefix or pre decrement

Example:
int x=10,y=5, z=3; System.out.println (x++); System.out.println (++x); System.out.println (y--);
System.out.println (--y); char ch=a;

System.out.println (+ch); // Unary +

int x=-12; // number is negative.

System.out.println(-x); output is 12 because -(-12)=12


Assignment Operators: These operators are used to assign values to variables.
Sl.no

Symbol

Description

Simple Assignment

op1=op2=op3

3
op1+=op2

Example:
int x=10,y=5, z=3;

Compound assignment
Short hand or Complex assignment
op1=op1+op2

op1-=op2

op1=op1-op2

op1*=op2

op1=op1*op2

op1/=op2

op1=op1/op2

op1%=op2

op1=op1%op2

int a, b, c, d;
a=b=c=d=100;
a+=10; b-=3; c*=a; d/=b; x%=y;
NOTE: Infix notation is operator between two operands.
Type Conversion: Conversion of one data type to another data type is called type conversion.
Two types of conversion: Implicit and Explicit.
Implicit conversion: this type of conversion will be done by the compiler itself.
Example: char ch=65; // stores A
int a=A; //stores 65
Explicit conversion will be done by the user using ( ) operator to convert from higher data type to lower data type.
int x=10;
double y=10.55;
int z=(int)(x+y);
Type Conversion Table
byte+byte=int
short+short=int
int+int=int
long+long=long
byte+short=int
short+int=int
int+float=float
long+char=long
byte+int=int
short+long=long
int+double=double
long+float=float
byte+long=long
short+float=float
int+long=long
long+double=double
byte+char=int
short+double=double
int+char=int
long+string=string
byte+string=string
short+char=int
int+string=string
byte+float=float
short+string=string
byte+double=double
float+float=float
double+double=double
char+char=int
boolean+string=string
float+double=double
double+char=double
char+string=string
float+char=float
double+string=string
float+string=string
Expression: It is the combination of operands and operators. Expression of two types (1) Pure expression or
Complex expression (2) Impure expression or mixed mode expression.
(1) Pure expression has operands of same data type.
(2) Impure expression has operands of different data type.
Operator Precedence: It determines the order in which expressions are evaluated. Eg: y=6+4/2; In this division
has the higher priority than +.
Operator Associativity: Associativity rule determine the grouping of operands and operators in an expression with
more than one operator of the same precedence. When the operations in an expression all have the same precedence
rating, the Associativity rule determines the order of the operators. The evaluation is done left to right. Eg: x=a+b-c;
Refer Text Book and C.W also all the above topics.

S-ar putea să vă placă și