Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
10 views

Lesson 4 - Operators in Java

The document provides an overview of operators in Java, including arithmetic expressions and various types of operators such as unary, binary, and ternary. It explains concepts like increment/decrement, operator hierarchy, and the use of operators in expressions. Additionally, it covers examples and questions to illustrate the application of these operators in Java programming.

Uploaded by

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

Lesson 4 - Operators in Java

The document provides an overview of operators in Java, including arithmetic expressions and various types of operators such as unary, binary, and ternary. It explains concepts like increment/decrement, operator hierarchy, and the use of operators in expressions. Additionally, it covers examples and questions to illustrate the application of these operators in Java programming.

Uploaded by

k.anany1750
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Operators in JAVA

We now know …
• Character sets in JAVA
• UNICODE ,ASCII code
• Escape sequences
• Token and its types(keywords, literals, identifiers,
punctuators, operators)
• Variables (initialisation)[ Qs. how is static and dynamic
initialisation different?)
• Data types in JAVA(Qs. integers are primitive type or non
primitive type?)
• Type conversion(implicit, explicit)
Scope of this chapter
• Arithmetic expression , statement
• Operators(unary, binary, ternary)
• Increment, decrement, postfix, prefix
• New operator ,invoking members of class by dot operator
• Output statement
• Hierarchy of operators
Operators and Arithmetical expression
• A symbol or token performing arithmetical or logical operations ->gives meaningful
result

ARITHMETIC EXPRESSION

VARIABLES CONSTANTS
OPERATORS
x,y ,a,b +,-,*
2,15,4

• A combination of them makes an expression


• x+y ,m-20,b*b + c*a ,a*a + b*b are all expressions
• Arithmetic Statement: when expression is assigned a variable (m=x+y;p=m-
20;k=b*b+c*a)
• Operands are connected by operators to give an expression
Forms of operators
• Unary: operator applied to single operand
❑x++,x--,--p,++p

• Binary: applied to two operands


❑a+b ->addition on operand a and b
❑a*b->returns the product of a and b

• Ternary(conditional assignments):deal with three operands,


contains a logical expression
Unary operator
• (+) operator :
❑If a =8,+a results in 8,if a =-10,+a results in -10
➢(-) operator :
❑If a =4 ,then –a =-4 ,if a=-0,then –a=0,if a=-3.6 then –a=3.6
➢INCREMENT and DECREMENT
❑(++)increases value of operand by one
❑(--) decreases value of operand by one
❑Examples : →x=x+1 can be written as x++(postfix) or ++x(prefix)
❑→x=x-1 can be written as x– -(postfix) or – -x (prefix)
Prefix ,Postfix
• Before action/prefix : before operation value will change
➢p=5;
p= ++p*4;
Step 1:p=5
Step2:p=5+1=6
Step3:p=6*4=24(output)
• After action/postfix: after operation value changes
➢p=5;
➢c=p++*4;
➢Step1:p=5;
➢Step2:p++*4=20(output)
➢Step3 :p=5+1=6;
Arithmetic
short-hand
Assignment
operations
Let us solve!!
• Example1:if p=5;find q=++p+5;
▪ ++p =6
▪ So, q=6+5=11
• Example2:if a=48,find a=a++ + ++a;
▪ a++ will remain 48 ,
▪ then a =49 ,then ++a=49+1=50
▪ Finally, a=48+50=98
• Example3:if a=8;find a- = ++a + a++ + 4;
▪ a-=b means ,a =a-b ,so a=a- (++a + a++ + 4);
▪ 8-(9 + 9+4)=8-22=-14
Types of Binary Operators
Binary Arithmetic Operators
• If a=10 and B =20 ,then …
• In a program ,the expression has to be written in to JAVA expression ,such as

• Relational operators : These show relation between the operands, such as true
or false
Relational operators in JAVA
• The program gives output according to condition
Logical operators
• Operators like AND ,OR , NOT are used as logic
• True or false output is given depending upon the outcome of
expressions

• If all logic are together ,NOT will


perform first
Ternary operators
• Deals with three operands
• The variable value /output depends upon a logical expression

• Variable=(test expression)?Expression1:Expression2
Rewrite as ternary operator
• If (a>b)
{
d=(a-b);
else
d=(b-a);
}
Answer: d=(a>b)?(a-b): (b-a)

• QS 2 .If int c =(3<4)? 3*4:3+4 What is the output???


• 3<4 so it is true ,so we go to expression1 ,which is 3*4 =12 ,so output =12
RECAP !
• If a=10 and b=6,what is a/b and a% b ?

• If m=12,then find n=m++ *5+ --m ???

• How do you write p=a2+ bc as java expression?

• How unary is different from binary operator?

You might also like