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

1d. Construct The Expressions Using Implicit and Explicit Type Conversions To Solve The Given Problem.

The document provides study material on expressions, type casting, mathematical functions, and operator precedence and associativity in Java. It defines expressions and describes how they are evaluated based on operator precedence and associativity rules. It also explains various mathematical functions available in Java's Math class like abs(), min(), max(), pow(), sqrt(), exp(), sin(), cos(), tan(), ceil(), and floor() along with examples.

Uploaded by

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

1d. Construct The Expressions Using Implicit and Explicit Type Conversions To Solve The Given Problem.

The document provides study material on expressions, type casting, mathematical functions, and operator precedence and associativity in Java. It defines expressions and describes how they are evaluated based on operator precedence and associativity rules. It also explains various mathematical functions available in Java's Math class like abs(), min(), max(), pow(), sqrt(), exp(), sin(), cos(), tan(), ceil(), and floor() along with examples.

Uploaded by

Hemant Thawani
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Template: Study Material

Insert the details within the < quotes >


<CO1>: <22412>: <Java Programming>: <Basic Syntactical Constructs in Java>: <LO4>: <Study Material>

<Yogita Jore> <01/02/2021> <Vijay Patil>

Key words Learning Objective: Diagram/ Picture


Expressions, Students should understand the use of expressions, type casting and math
Associativity function.
,
Type casting,
math
function
Key Concept Map
Questions

Expression:
When An expression is a
associativity combination of operators,
is used? constants and variables.
Ans:
An expression may consist
When an
of one or more operands,
expression
and zero or more
contains
operators to produce a
operators
value.
from the
same group,
associativity Expression evaluation in
rules are Java is based upon the
applied to following concepts:
determine  Operator
which precedence
operation  Associativity
should be rules
performed
first.

What is
Explanation of Concept:
Precedence
Operator Precedence in Java
order
 When several operations occur in an expression, each part is evaluated
Ans:
and resolved in a predetermined order called operator precedence.
When two
Parenthesis can be used to override the order of precedence and force
operators
some parts of an expression to be evaluated before other parts.
share an
Operations within parenthesis are always performed before those
operand, the
outside. Within parenthesis, however, normal operator precedence is
operator
maintained.
with the
 When expressions contain operators from more than one category,
higher
arithmetic operators are evaluated first, comparison operators are
precedence
evaluated next and logical operators are evaluated last. Comparison
goes first.
operators all have equal precedence; that is they are evaluated in the
left-to- right order in which they appear. Arithmetic and logical
operators are evaluated in the following order of precedence.
 Table shows all java operators from highest to lowest precedence, along
with their associativity.
Operator Description Level Associativity

[] Access array element 1 Left to Right


. Access object member
() Invoke a method
++ Post-increment
-- Post-decrement
++ Pre-increment 2 Right to Left
-- Pre-decrement
+ Unary plus
- Unary minus
! Logical NOT
~ Bitwise NOT
() Cast 3 Right to Left
New Object creation
* Multiplication 4 Left to Right
/ Division
% Modulus
+- Additive 5 Left to Right
+ String concatenation
<< Left Shift 6 Left to Right
>> Right shift
>>> Right shift with zero fill
< Relational, less than 7 Left to Right
<= Less than or equal to
> Greater than
>= Greater than or equal to
instanceof Type comparision
== Equality 8 Left to Right
!= Inequality
& Bitwise AND 9 Left to Right
^ Bitwise XOR 10 Left to Right
| Bitwise OR 11 Left to Right
&& Conditional AND 12 Left to Right
|| Conditional OR 13 Left to Right
?: Conditional 14 Right to Left
= += -= *= /= assignment 15 Right to Left
%= &= ^= |= .
<<== >>== >>>=

Precedence order: When two operators share an operand, the operator with the
higher precedence goes first.
For example 1+2*3 is treated as 1+(2*3), whereas 1*2+3 is treated as (1*2)+3
since multiplication has a higher precedence than addition.
Associativity: When two operators with the same precedence, the expression is
evaluated according to its associativity.
For example, x=y=z=17 is treated as x=(y=(z=17))), leaving all three variables with
the value 17, since the = operator has right-to-left associativity. On the other
hand, 70/2/3 is treated as (70/2)/3 since the / operator has left-to-right
associativity.

Expression of Evaluation:
An expression is a construct which is made up of literals, variables, method calls
and operators following the syntax of Java. Every expressions consists of at least
one operator and an operand. Operand can be either a literal, variable or a
method invocation.
It is common for an expression to have more than one operator.
For example, consider the below example:
(20 * 5) + (10 / 2) – (3 * 10)

So, how is the above expression evaluated?


Expression evaluation in Java is based upon the following concepts:
 Operator precedence
 Associativity rules

Example:

class Precedence
{
public static void main(String[] args)
{
int a = 10, b = 5, c = 1, result;
result = a-++c-++b;
System.out.println(result);
}
}

Output:
2

Mathematical Function:
Mathematical function such as sin, cos, sqrt used to analyze of real life problems.
Java supports these basic math functions through Math class defined in the
java.lang package. The mathematical functions are:

Abs():abs method is used to find absolute value of variable.


Syntax: Math.abs(variable);
Here variable can be double, float, int and long datatype.
Example: Math.abs(-2.3);
Output: 2.3

Min():min method is used to find minimum value between variable1 and


variable2.
syntax: Math.min(variable1,variable2)
Here variable can be double,float,int and long datatype
Example: Math.min(2,8);
output = 2
Max():max method is used to find maximum value between variable1 and
variable2.
syntax: Math.max(variable1,variable2)
Here variable can be double,float,int and long datatype
Example: Math.max(2,8);
Output: 8

Pow():Power of the number. Here variable1 is base value and variable2 is power
value
Syntax: Math.pow(variable1,variable2)
Here variable is double datatype
Example: Math.pow(2,3;
Output: 8

Sqrt():sqrt method is used for finding square root of a number.


Syntax: Math.sqrt(variable)
Here variable is double datatype
Example: Math.sqrt(9);
Output: 3

Exp():Euler's number e raised to the power of a variable.


Syntax: Math.exp(variable)
Here variable is double datatype
Example: Math.exp(2);
Output: 7.38905609893065

Sin():To calculate the sine of a value.


Syntax: Math.sin(double variable)
Example: Math.sin(82.55);
Output: 0.763419622322519

Cos():To calculate the cosine of an angle


Syntax: Math.cos(variable)
Example: Math.cos(82);
Output: 0.949677697882543

Tan():To calculate the tangent of a number.


Syntax: Math.tan(variable)
Example: Math.tan(80);
Output: 9.00365494560708

Ceil():In arithmetic, the ceiling of a number is the closest integer that is greater or
higher than the number considered. ceil method is used round up the variable.
Syntax: Math.ceil(variable)
Example: Math.ceil(12.15)=13
Math.ceil(-24.06)=-24
Consider a floating-point number such as 12.155. This number is between integer
12 and integer 13. In the same way, consider a number such as –24.06. As this
number is negative, it is between –24 and –25, with –24 being greater.
Floor()=This is the floor() function which returns you the largest value but less
than the argument. floor method is used round down the variable.
Syntax: Math.floor(variable)
Here variable is double datatype
Example: Math.floor(8.4) = 8
Math.floor(-24.06)=-25

copySign()= copySign method is used to copy sign of variable2 and assign that sign
to variable1.
Syntax: Math.copySign(variable1, variable2)
Here variable is float and double datatype
Example: Math.copySign(2,-8.3) = -2.0
Math.copySign(-2,8.3) = 2.0

Round()=Rounds to the nearest integer.


Syntax: Math.round(variable)
Example: Math.round(1.02)=1
Math.round(-2.1)=-2

Random()=This is the random() function which returns you the random number. It
is absolutely system generated. This method can be used to produce a random
number between 0 and 100

Log()=Returns the natural logarithm of the argument.


Syntax: Math.log(variable)
Example: Math.log(2)= 0.6931471805599453

Example:
public class JavaMathExample1
{
    public static void main(String[] args)
    {
        double x = 28;
        double y = 4;

        // return the maximum of two numbers
        System.out.println("Maximum number of x and y is: " +Math.max(x, y));
10.
11.         // return the square root of y
12.         System.out.println("Square root of y is: " + Math.sqrt(y));
13.
14.         //returns 28 power of 4 i.e. 28*28*28*28
15.         System.out.println("Power of x and y is: " + Math.pow(x, y));
16.
17.         // return the logarithm of given value
18.         System.out.println("Logarithm of x is: " + Math.log(x));
19.         System.out.println("Logarithm of y is: " + Math.log(y));
20.
21.         // return the logarithm of given value when base is 10
22.         System.out.println("log10 of x is: " + Math.log10(x));
23.         System.out.println("log10 of y is: " + Math.log10(y));
24.
25.         // return the log of x + 1
26.         System.out.println("log1p of x is: " +Math.log1p(x));
27.
28.         // return a power of 2
29.         System.out.println("exp of a is: " +Math.exp(x));
30.
31.         // return (a power of 2)-1
32.         System.out.println("expm1 of a is: " +Math.expm1(x));
33.     }
34. }

Output:
Maximum number of x and y is: 28.0
Square root of y is: 2.0
Power of x and y is: 614656.0
Logarithm of x is: 3.332204510175204
Logarithm of y is: 1.3862943611198906
log10 of x is: 1.4471580313422192
log10 of y is: 0.6020599913279624
log1p of x is: 3.367295829986474
exp of a is: 1.446257064291475E12
expm1 of a is: 1.446257064290475E12

Type Casting:
Converting one primitive datatype into another is known as type casting (type
conversion) in Java. You can cast the primitive datatypes in two ways namely,
Widening and, Narrowing.
Widening − Converting a lower datatype to a higher datatype is known as
widening. In this case the casting/conversion is done automatically therefore, it is
known as implicit type casting. In this case both datatypes should be compatible
with each other.

Example:
public class Main
{
public static void main(String[] args)
{
int a = 9;
double b = a; // Automatic casting: int to double

System.out.println(a);
System.out.println(b);
}
}

Output:
9
9.0

Narrowing − Converting a higher datatype to a lower datatype is known as


narrowing. In this case the casting/conversion is not done automatically, you need
to convert explicitly using the cast operator “( )” explicitly. Therefore, it is known
as explicit type casting. In this case both datatypes need not be compatible with
each other.

Example:
public class Main
{
public static void main(String[] args)
{
double a = 9.78;
int b= (int) a; // Explicit casting: double to int

System.out.println(a);
System.out.println(b);
}
}

Output:
9.78
9

Application of Concept/ Examples in real life:


 To prevent loss of data
 To generate otp using math function

Key Take away from this LO:

Concept of java tokens, data types, constants, type casting, variables

You might also like