Operators and Expressions in Java
-
Upload
abhilash-nair -
Category
Education
-
view
1.727 -
download
0
Transcript of Operators and Expressions in Java
![Page 1: Operators and Expressions in Java](https://reader036.fdocuments.in/reader036/viewer/2022082808/555e3380d8b42a63048b46ad/html5/thumbnails/1.jpg)
OPERATORS &
EXPRESSIONS- Ankita Karia
12 April 2023 1Ankita R Karia
![Page 2: Operators and Expressions in Java](https://reader036.fdocuments.in/reader036/viewer/2022082808/555e3380d8b42a63048b46ad/html5/thumbnails/2.jpg)
JAVA operators can be classified into a number of related categories as below:-
Arithmetic.
Relational.
Logical.
OPERATORS
+ - * / %
< <= > >= == != &&AND
T && T = T
||OR
F || F= F
!NOT
T = FF = T
12 April 2023 2Ankita R Karia
![Page 3: Operators and Expressions in Java](https://reader036.fdocuments.in/reader036/viewer/2022082808/555e3380d8b42a63048b46ad/html5/thumbnails/3.jpg)
ASSIGNMENT=
• Is used to assign value to variable.e.g.:- a =10•SHORTHAND
a = a + 1Can also be written as
a + = 1a = a / (n+1)
CONDITIONAL? :
exp1 ? exp2 : exp3 e.g.:- x=(a>b)?a:b
DECREMENT - -
• PRE:- First subtracts 1 from operand and then the result is assigned . - - m;
•POST:- First assigns the value to the variable on left and then decrements operand m - -
INCREMENT ++
•PRE:- First subtracts 1 from operand and then the result is assigned . ++m;
•POST:- First assigns the value to the variable on left and then decrements operand m + +
OPERATORS (Contd…)
12 April 2023 3Ankita R Karia
![Page 4: Operators and Expressions in Java](https://reader036.fdocuments.in/reader036/viewer/2022082808/555e3380d8b42a63048b46ad/html5/thumbnails/4.jpg)
JAVA permits mixing of constants and variables of different
types in an expression, but during evaluation it adheres to
very strict rules of type conversion.
If operands are of different types, the “lower” type is
automatically converted to “higher” type before the operation
proceeds. The result is of higher type.
The final result of an expression is converted to the type of
the variable on the left of the assignment before assigning the
value
TYPE CONVERSIONS IN EXPRESSION
12 April 2023 4Ankita R Karia
![Page 5: Operators and Expressions in Java](https://reader036.fdocuments.in/reader036/viewer/2022082808/555e3380d8b42a63048b46ad/html5/thumbnails/5.jpg)
EXAMPLES ACTION
x= (int) 7.5 7.5 is converted to integer by truncation
a = (int)21.3/(int)4.5 Evaluated as 21/4 and the result would be 5
b = (double) sum/n Division is done in floating point mode
y = (int) (a+b) The result of a+b is converted to integer
y = (int) a+b a is converted to integer and then added to b
p = cost ((double)x) Converts x to double before using it as parameter
USE OF CASTS
12 April 2023 5Ankita R Karia
![Page 6: Operators and Expressions in Java](https://reader036.fdocuments.in/reader036/viewer/2022082808/555e3380d8b42a63048b46ad/html5/thumbnails/6.jpg)
JAVA supports basic math functions through Math class
defined in the java.lang.package.
The functions should be used as follows:-
Math.function_name();
Example: y = Math.sqrt(x);
MATHEMATICAL FUNCTIONS
Function name
java.lang.Math
12 April 2023 6Ankita R Karia
![Page 7: Operators and Expressions in Java](https://reader036.fdocuments.in/reader036/viewer/2022082808/555e3380d8b42a63048b46ad/html5/thumbnails/7.jpg)
MATHEMATICAL FUNCTIONSFUNCTIONS ACTION
sin(x) Returns the sine of the angle x in radians
cos(x) Returns the cosine of the angle x in radians
tan(x) Returns the tangent of the angle x in radians
asin(x) Returns the angle whose sine is y
atan2(x,y) Returns the angle whose tangent is x/y
pow(x,y) Returns xy
exp(x) Returns ex
log(x) Returns the natural logarithm of x
sqrt(x) Returns square root of x
abs(x) Returns absolute of x
max(a,b) Returns maximum of a and b12 April 2023 7Ankita R Karia
![Page 8: Operators and Expressions in Java](https://reader036.fdocuments.in/reader036/viewer/2022082808/555e3380d8b42a63048b46ad/html5/thumbnails/8.jpg)
Determine the value of the following
arithmetic expression.
(a=5,b=2,c=1)
1. m = ++a*5;
2. p*=x/y; x=10,y=2,p=2;
3. s/=5; s=5;
4. n=b++-c*2;
EXAMPLES
12 April 2023 8Ankita R Karia
![Page 9: Operators and Expressions in Java](https://reader036.fdocuments.in/reader036/viewer/2022082808/555e3380d8b42a63048b46ad/html5/thumbnails/9.jpg)
12 April 2023 9Ankita R Karia
![Page 10: Operators and Expressions in Java](https://reader036.fdocuments.in/reader036/viewer/2022082808/555e3380d8b42a63048b46ad/html5/thumbnails/10.jpg)
12 April 2023 10Ankita R Karia
![Page 11: Operators and Expressions in Java](https://reader036.fdocuments.in/reader036/viewer/2022082808/555e3380d8b42a63048b46ad/html5/thumbnails/11.jpg)
Determine the value of the following
logical expression. (a=5,b=10,c=-6)
1. a<b && a<c;
2. a<b && a>c;
3. a= = c || b>a;
4. b>15 && c<0 || a>0;
5. (a/2.0==0.0&&b/2.0!=0.0)||c<0.0
EXAMPLES
12 April 2023 11Ankita R Karia
![Page 12: Operators and Expressions in Java](https://reader036.fdocuments.in/reader036/viewer/2022082808/555e3380d8b42a63048b46ad/html5/thumbnails/12.jpg)
12 April 2023 12Ankita R Karia
![Page 13: Operators and Expressions in Java](https://reader036.fdocuments.in/reader036/viewer/2022082808/555e3380d8b42a63048b46ad/html5/thumbnails/13.jpg)
1. The straight-line method of computing the yearly depreciation of the value of an item is given by
depreciation = Purchase Price – Salvage Value
Years of service
Write a program to determine the salvage value of an item when the purchase price, years of service and Annual depreciation are given
12 April 2023 13Ankita R Karia