0% found this document useful (0 votes)
8 views

Java Learn

Operators are symbols that perform specific operations on operands. There are unary, binary, and ternary operators classified based on the number of operands accepted. Common operators include arithmetic, assignment, relational, logical, increment/decrement, and conditional operators. Some examples are addition (+), assignment (=), less than (<), AND (&&), pre-increment (++var), and ternary (?:) operators. The document also provides examples and explanations of how different operators work in Java programs.

Uploaded by

Srinidhi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Java Learn

Operators are symbols that perform specific operations on operands. There are unary, binary, and ternary operators classified based on the number of operands accepted. Common operators include arithmetic, assignment, relational, logical, increment/decrement, and conditional operators. Some examples are addition (+), assignment (=), less than (<), AND (&&), pre-increment (++var), and ternary (?:) operators. The document also provides examples and explanations of how different operators work in Java programs.

Uploaded by

Srinidhi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

OPERATORS

What is operators?

● Operators are predefined symbol which is used to perform some


specific task on the given data.the data given as a input to the
operator is known as operand.
TYPES OF OPERATORS BASED ON OPERAND:
● UNARY OPERATOR
● BINARY OPERATOR
● TERNARY OPERATOR
UNARY OPERATOR :
The operator which can accept only one operand
is known as unary operator.
Example : Increment/Decrement operator.
BINARY OPERATOR:
The operator which can accept two operand is known
as binary operator.
Example : Arithmetic operator,relational operators.
TERNARY OPERATOR:

The operator which can accept three operand is


known as ternary operator.
Example : Conditional Operator
CLASSIFICATION OF OPERATOR BASED ON TASK:

● ARITHMETIC OPERATOR
● ASSIGNMENT OPERATOR
● RELATIONAL OPERATOR
● LOGICAL OPERATOR
● INCREMENT/DECREMENT OPERATOR
● CONDITIONAL OPERATOR
MISCELLANEOUS
ARITHMETIC OPERATOR
‘+’ Operator:
ADDITION

+
CONCAT
ACTIVITY:
1.Write a java program to perform addition of two numbers.
2.Write a java program to concat a number and String.
3.Write a java program to concat a floating number and String.
4.Write a java program to concat a integer and character.
5.Write a java program to concat a character and String.
6.Write a java program to concat a integer,character,boolean and
String data.
ASSIGNMENT OPERATOR:

Assignment operators are used in Java to assign values to variables.

1. +=
2. -=
3. *=
4. /=
Operator Example Equivalent to

= a=b; a=b;

+= a+=b; a=a+b;

- -= a-=b; a=a-b;
Operator Example Equivalent to

a*=b; a=a*b;
*=
/= a/=b; a=a/b;

%= a%=b; a=a%b;
RELATIONAL OPERATORS:

● < - lesser than


● > - greater than
● <= - lesser than or equal to
● >= - greater than or equal to
● == - equal to
● != - not equal to
NOTE :
The return value of relational operator is boolean.
LOGICAL OPERATOR:

&&(Logical AND)
If all the expression returns true then this operator will
return true
||(Logical OR)
If any of one of the expression return true then this operator
will return true
! (Logical NOT)
It the result is true then it will return false and vice versa
AND TABLE :

TRUE TRUE TRUE

TRUE FALSE FALSE

FALSE TRUE FALSE

FALSE FALSE FALSE


OR TABLE :

TRUE TRUE TRUE

TRUE FALSE TRUE

FALSE TRUE TRUE

FALSE FALSE FALSE


INCREMENT/DECREMENT OPERATOR

Increment Operator:
● Pre-Increment Operator
● Post-Increment Operator
Decrement Operator:
● Pre-Decrement Operator
● Post-Decrement Operator
INCREMENT OPERATOR

PRE-INCREMENT (++VAR) POST-INCREMENT(VAR++)

● INCREMENT THE ● USE THE OLD


VALUE BY 1 DATA
● USE THE ● INCREMENT THE
UPDATED VALUE VALUE BY 1
DECREMENT OPERATOR

PRE- DECREMENT(--VAR) POST- DECREMENT(VAR- -)

● DECREMENT THE ● USE THE OLD


VALUE BY 1 DATA
● USE THE ● DECREMENT THE
UPDATED VALUE VALUE BY 1
Conditional Operator:

SYNTAX:

Operand 1 ? Operand 2 : Operand 3


(Condition) ? value1 : value2
Working of Conditional Operator:

TRUE

CONDITION ? VALUE1 : VALUE2

FALSE
● Conditional Operator will check the condition,if
condition is true operand 2 will gets executed,if
condition is false operand 3 ill gets executed.

Note :
● The return value of conditional operator is always
boolean.
● The return value of operand 1 is based on operand
2 and operand 3.
EXAMPLE:
Class Demo
{
public static void main(String[] args)
{
int a = 10;
int b =20;
int c = (a>b) ? a : b; //Conditional Operator
System.out.println(c); // 20
}
}
TRACE THE PROGRAMS: (INCREMENT OPERATOR)

1.int a=10; 3.int a=10;


a=a++;
a++;
s.o.p(a);
s.o.p(a);
4.int d =40;
2.int b =20;
int e = 50;
++b; d = d++ + ++e;
s.o.p(d);
s.o.p(b);
5.short s = 20; 6. int a1=100;
byte b = 25; short a2 = 150;
s++; int a3 = 350;
long a4 = 200;
++b;
long res = a1++ - ++a2 +a3++ - ++a4;
s.o.p(s+b);
s.o.p(res);
TRACE THE PROGRAMS: (DECREMENT OPERATOR)

1.int a=35; 3.int a=100;


a= a- -;
a--;
s.o.p(a);
s.o.p(a);
4.int d =76;
2.int b = 70;
int e = 32;
- -b; d = d- - + - -e;
s.o.p(d);
s.o.p(b);
5. int a1=100;
short a2 = 150;
int a3 = 350;
long a4 = 200;
int res = a1- - - ++a2 + a3- - - ++a4;

s.o.p(res);

You might also like