Java Tutorial: Associativity of Operators in Java
Summary
TLDRThis video covers essential topics related to operator precedence and associativity in Java, explaining how Java evaluates expressions. The speaker clarifies that, unlike mathematical BODMAS rules, Java relies on precedence (which operator is applied first) and associativity (the direction of execution). Through examples, they demonstrate how operators like multiplication, division, addition, and subtraction are evaluated in different contexts, helping viewers understand complex expressions. The video is highly relevant for students, interviewees, and anyone preparing for exams, offering a deep dive into an often misunderstood topic.
Takeaways
- đ» The video focuses on explaining operator precedence and associativity in Java.
- đą Java does not use the BODMAS rule, but instead follows precedence and associativity rules for operator evaluation.
- đ ïž Operators like multiplication (*) and division (/) have higher precedence than addition (+) and subtraction (-).
- âïž Associativity determines the order in which operators with the same precedence are evaluated (left to right or right to left).
- đ In Java, multiplication and division operators are left-associative, meaning they are evaluated from left to right.
- 𧟠Example: The expression '60 / 5 - 34 * 2' evaluates division first (60/5=12), then multiplication, and finally subtraction, resulting in -56.
- đ The instructor emphasizes using parentheses to avoid errors in complex expressions where multiple operators are involved.
- đ The video includes a quadratic expression example, showing how operator precedence affects evaluation and how to correct it using parentheses.
- đ Operator precedence is critical in programming, especially during interviews or exams, as it affects how expressions are executed.
- â The video encourages viewers to understand these concepts early on to avoid common mistakes in programming and interviews.
Q & A
What is operator precedence in Java?
-Operator precedence in Java determines the order in which operators are evaluated in expressions. Operators with higher precedence are evaluated before those with lower precedence.
How does associativity affect the evaluation of expressions?
-Associativity defines the direction in which operators with the same precedence are evaluated. Most operators in Java are left-to-right associative, meaning they are evaluated from left to right.
What is the difference between precedence and associativity?
-Precedence determines which operator is evaluated first based on priority, while associativity resolves the order of evaluation for operators with the same precedence, dictating whether they are evaluated from left to right or right to left.
Why is the rule of BODMAS not used in Java?
-Java uses precedence and associativity rules instead of the BODMAS rule for evaluating mathematical expressions. The precedence of operators in Java is defined by the language, not mathematical conventions like BODMAS.
How does Java evaluate the expression `6 * 5 - 34 / 2`?
-In Java, `6 * 5` is evaluated first because multiplication has a higher precedence than subtraction. Then, `34 / 2` is evaluated because division also has higher precedence than subtraction. Finally, `30 - 17` is calculated, resulting in 13.
What happens when operators have the same precedence?
-When operators have the same precedence, associativity determines the order of evaluation. For example, both division and multiplication have the same precedence, so Java evaluates them left to right.
How can parentheses change the order of evaluation?
-Parentheses override the default precedence and associativity rules, forcing specific parts of an expression to be evaluated first. For example, `(6 - 5) * 2` will evaluate the subtraction before the multiplication, even though multiplication has higher precedence.
What happens if the denominator of an expression becomes zero?
-If the denominator of an expression becomes zero, Java will throw an `ArithmeticException`, indicating a division by zero error. Division by zero is undefined in mathematics and causes an error in programming.
What is the significance of knowing precedence and associativity for interviews or exams?
-Understanding operator precedence and associativity is crucial for writing and debugging Java code. It also helps in solving coding questions in interviews and exams where correct expression evaluation is necessary.
Why is it important to use parentheses in complex expressions?
-Parentheses help clarify the intended order of evaluation in complex expressions, preventing unexpected results due to precedence and associativity rules. This makes code more readable and less prone to errors.
Outlines
đ§ Understanding Java Operator Precedence
The first paragraph introduces the topic of operator precedence in Java, which is important both in academic contexts and technical interviews. It explains that operators are applied and evaluated based on their precedence, and the video aims to cover this concept thoroughly. The speaker starts by naming the Java file and demonstrating an example of how precedence works in a Java expression. They highlight that precedence determines which operations are performed first, such as multiplication or division over addition or subtraction, and clarify that Java uses precedence and associativity rules instead of the traditional BODMAS rule.
đ Associativity in Java Operations
This paragraph expands on the concept of associativity, particularly how it resolves conflicts when operators have equal precedence, such as multiplication and division. Associativity determines the direction in which operations are evaluatedâleft-to-right or right-to-left. The speaker provides a practical example involving division and subtraction, and walks through how associativity impacts the final result. They stress that many students mistakenly apply the BODMAS rule, but in Java, associativity is key to determining the order of operations.
đ Practical Example: Operator Precedence and Associativity in Expressions
The third paragraph introduces a 'quick quiz' for the audience, focusing on how to correctly write expressions based on the discussed concepts. The speaker gives examples of algebraic expressions like quadratic equations and walks through how to code them in Java, emphasizing the importance of precedence and associativity in getting the correct result. They also explain why using parentheses is recommended for complex expressions to avoid unintended evaluation orders, providing a detailed breakdown of how expressions like 'bÂČ - 4ac / 2a' are evaluated in Java.
đ Highest Precedence: The Role of Parentheses in Complex Expressions
In this paragraph, the speaker explains that parentheses have the highest precedence, meaning that any expression inside parentheses is evaluated first. They use this concept to further clarify how complex expressions, like those involving quadratic equations, should be handled in Java. The speaker also provides a mathematical rule that division by zero leads to undefined expressions, which can cause errors in programming. They conclude by encouraging viewers to understand these concepts early to avoid struggles during exams or interviews.
đ Wrapping Up and Final Thoughts
The final paragraph provides a conclusion, summarizing the key points discussed in the videoâoperator precedence, associativity, and how to correctly evaluate expressions in Java. The speaker reflects on their personal experience of learning these concepts later in their career and emphasizes the importance of mastering them early. They also invite viewers to like the video, share it on social media, and stay tuned for future content. The speaker signs off by thanking the audience for watching.
Mindmap
Keywords
đĄOperator Precedence
đĄAssociativity
đĄBODMAS
đĄExpression
đĄMultiplication and Division
đĄAddition and Subtraction
đĄQuadratic Formula
đĄParentheses
đĄArithmetic Exception
đĄInterview and Exam Preparation
Highlights
Introduction to Java operator precedence and associativity, essential for both academic exams and job interviews.
Explanation of operator precedence: multiplication and division have higher precedence than addition and subtraction.
Example using the expression 'int a = 6 * 5 - 8 / 2', explaining how precedence leads to evaluating multiplication and division before addition and subtraction.
Clarification that Java does not follow the BODMAS rule but instead uses operator precedence and associativity.
Definition of associativity: when two operators have the same precedence, associativity (either left-to-right or right-to-left) determines the evaluation order.
Demonstration of left-to-right associativity using division and multiplication in an expression like '60 / 5 - 34 * 2'.
Difference between associativity of star (multiplication) and slash (division): both have equal precedence but are evaluated left-to-right.
Practical example: how left-to-right associativity in Java affects the result of a complex expression like '60 / 5 - 34 * 2'.
Use of comments in code to clarify which operations will be evaluated first based on precedence and associativity.
Explaining how parentheses can override default precedence and associativity in Java expressions.
Quadratic equation example: 'b^2 - 4ac / 2a', showcasing how operator precedence and parentheses affect expression evaluation.
Recommendation to use parentheses in complex expressions to ensure correct evaluation order and avoid programming errors.
Mention of potential programming errors, like arithmetic exceptions when the denominator becomes zero in division.
Personal note: the speaker reflects on their own late understanding of these concepts during their career, emphasizing the importance of learning early.
Encouragement to share and like the video, inviting viewers to tag the speaker on Instagram when sharing screenshots.
Transcripts
Guys, today's video will cover some important topics
like which thing is done first
Java compiler multiply will do first
divide will do first or then
subtract will do first or addition will do first
what is the precedence of our operator
what is the associativity of our operator
many questions are asked on these topics
whether you are from academics or
you are giving an interview for a corporate
this video is going to be very important for you
so in today's video we are going to discuss this thing
guys in this course of java we have talked a lot
in today's video we will talk about how precedence works of operator
because we have seen operators, types of operators
how expressions are there, we can write in java we have seen the types of operators what are the expressions we can write in java
we have seen all these things in detail
and you must have understood it well, we have done a lot of work on this
but I will do one thing here that I will make a file
and I am going to keep its name
I will keep its name according to the video number
so that whenever you are watching the video number, you will know which file it is
I have to give you a code, so I write here its name
09 underscore
and here I write cwh underscore
and chapter 2
operator precedence dot java
ok
and what is this problem, I think I have done something wrong, I can't write 09 in this
this is the problem, my class name can't start with 09
no problem, I will write cwh underscore 09
and see here, this is my class
I will minimize it, zoom in a little
let's put the main function here and we are good to go now
we can do our coding here
so I have told you about precedence in notes
precedence of operators
the operators are applied and evaluated based on precedence
so here according to precedence
your operators are applied like
less precedence of plus and minus than star divide
let's say i write an expression here
int a is equal to 6 multiplied by 5 minus a divided by 4
or 2 let us say
I do it like this and I shout it
I say print this
if you apply the rule of baud mass
apply the rule of baud mass, if the rule of mathematics is
6 multiplied by 5 will be 30
and 34 divided by 2 will be 17
so here 30-17 is 13
so this 13 will be the answer
you can see here I have run the wrong file
I have to right click and see here 13 is here
but what happens is that in java, baud mass is not used
in java, precedence and associativity is used
so what are these two things
today I will explain you well precedence and associativity so what are these two things today I will explain you
precedence and associativity
what is precedence?
precedence means
that I am bigger than you
so star means minus
that I am bigger than you
and minus means
that I am bigger than you
and the bigger one
is evaluated first the other will be evaluated first
the one who is bigger will be evaluated first
the one who is the most respected person will be evaluated first
and then the slash will be evaluated
now see why is this happening here
what is the precedence of associativity
the precedence is that star is bigger than minus
so it will be evaluated first
but what is associativity
and to explain associativity i will write an expression b
in which i will do a simple thing, i will put a slash and put a star
ok
ok i will tell you one thing
now tell me in which order it will be evaluated
earlier it was 6x5
then 34x2
and then this result and this result
but now we have to see its evaluation order
this is associativity
if your star is big
then it will be evaluated like this
let me put a comment here, multiline, and look at this carefully
you will not know this thing by chance
6 multiplied by 5 will be 30
minus then it will be 34 divided by 2
and after that it will be evaluated as 30 minus 17
and it will be equal to 13 this was our precedence but if I do the same thing for you
I do the same thing for B then you will see that what you think will not happen
there is a high chance that it will not happen
now there is a difference between star and slash
which one will be evaluated first
some people will say put a bodmask and do star
divide first and then multiply
no it doesn't work like that
here bodmask doesn't work
here we see associativity
so precedence is equal to slash and star
now when precedence is equal
star and slash are equal in precedence
so to break this tie we see associativity
associativity is like this, left to right, right to left
associativity of star and slash is left to right
so it will start to get evaluated like this
so 6 divided by 5 will be first
and I will make it 60
so that the expression will be correct
60 divided by 5 will be 12
and I will remove the rest of the expression
then you can see here
34 multiplied by 2 will be here
which is 68
so 12 minus 68 will be
and 12 minus 68 is
12 minus 68
is minus 56 so is equal to minus 56
so it will be minus 56
so let's print B
and let's print B here
I will run it here and see minus 56 is here
so 13 was here and minus 56 is here
so this is how our associativity works
so minus 56 minus 56 and 13
now many of you will think that how will we get associativity and how will we know
that what is the thing that we multiply first and divide
so to make this thing easy I have prepared an image for you
see here all the precedence, associativity
of all operators are written here
now many of you may not know about these operators
and you don't need to know about it, you just need to keep this list
you don't need to keep it, you just need to refer this table
whenever you will evaluate operators
then first of all you will see
who has the highest precedence here
this number is given to the precedence, highest precedence
so here if I want to see
which one will be evaluated first, let me zoom in
so in plus and star, you see here
the precedence of plus is less, the precedence of star is more
star has 12 precedence, plus minus has 11
so I will evaluate star first
but if star and slash come together
if I face both of them together
then I will use associativity
I will evaluate left to right
if there is slash in left then it will be evaluated first
if there is star then it will be evaluated first
like if I show you this expression
here 60 by 5 minus 34 into 2
so here slash is not being divided because the bodmass rule is running
that's why it is being evaluated first because it is in the left
associativity goes from left to right
this way your order of evaluation works
if you have a tie in the operators, if there is a tie in the precedence
so first of all associativity to break the precedence and tie, associativity is fine
so precedence and associativity work like this
so here I write, I make the comment very clear
that here, see, this star and slash, star was evaluated first
because its highest precedence of star and slash
and after that associativity was left to right
highest precedence goes to star and slash
they are then evaluated on the basis of left to right associativity
left to right associativity
after that they are getting evaluated
so they will get evaluated on the basis of left to right associativity
so here A's value is this B's value is this
and they are getting evaluated very easily
so I have written the same thing in notes for you
precedence of operators, operators are applied and evaluated based on precedence
ok, they are evaluated based on precedence
I have taken an example, there is less precedence of plus and minus
star and slash, so star and slash are evaluated first
and many students don't know that
that evaluation is done according to precedence and associativity
some people start applying baud mass here and say it is evaluated according to the precedence and associativity
some people start using the word board mask
and say that it is based on the board mask
some people say that it is a dangerous thing and we don't want to learn it
it is not dangerous, it is just an image
you have to look at the image carefully
and understand it
and this image will tell you
that what is the precedence of what and what is associativity
so here you have seen what is precedence and what is associativity
after that you can evaluate your expression
so here it is written about associativity that tells the direction of execution of operators
it can either be left to right or right to left ok
star slash is left to right
plus minus is left to right
plus plus right to left
plus plus or equal to right to left
like if you write something like this
a is equal to b is equal to c
equal to 45
so first b value will be 45
then a value will be equal to it
means it runs like this right to left
so it is not necessary that you have left to right
or associatively right to left
now here is a quick quiz for you
that how will you write these expressions
the expressions which I have written
on the basis of these concepts how will you write
so here x-y divided by 2
b square minus 4ac by 2
b square minus u square, a star b minus t
so if I want to write these expressions, I will write it here
quick quiz
so
here what I will do is
I will write this first
a x minus y by 2
so if I write x star Y by 2 here
suppose I wrote int k is equal to
and I will comment out all these above
because I don't want to disturb us
when we are coding below
so I will do this whole precedence associativity
this is a topic
I am commenting this topic completely
and you guys focus down, so I have put it here like this
no problem, no problem, here this comment is actually getting closed
so there is a problem, no problem, I will do it like this
I will comment it like this, you guys have understood
I have just explained it to you, so I have done k is it to you so k is equal to x
x into y is equal to 2
so if i do int x is equal to 5
int y is equal to 1
so what will be the value of this
5 multiplied by 1 is 5
and 5 divided by 2
let's make it 6
6 multiplied by 1 is 6
and divided by 2 is 3
so if i write it like this and 6 multiply by 1 will be 6 and divided by 2 will be 3
so if i write it like this
and system.out.printlnk
so what will be evaluated first
so it will be evaluated first and then divided by 2
so i don't have any risk of getting evaluated before y by 2
so i will write it like this
if i run it then I should get 3
and I am getting 3
so why should I put extra parenthesis
when my work is done without parenthesis
so this thing you should know
now along with that
if I talk to you
about another expression
b square minus 4ac upon 2a
which is your quadratic
quadratic equation the discriminant of quadratic equation
b square minus 4ac
and minus b plus minus under root b square minus 4ac upon 2a
which is the root of it
but b square minus 4ac upon 2a is our question
so if I comment this out and write
k is equal to
see square is nothing
you have to do b into b
and let me
comment out what is going on
so b square
minus 4
a c
and here I will write upon 2 a
and I have not given
value of b, a or c
so I will give that also so I will give here and I have not given value of b, a, c
so I will give that also here
so I will give value of expression here
int b is equal to 1
int c is equal to 4
and int a is equal to 5
like this
so I think I have already defined b and a
so I will comment out these
and this code that I am giving you I am giving you just to refer you it is not like I have defined it, so I will comment it out and this code that I am giving you
I am giving you to refer, it is not like I have distributed any software to you
so don't say that it is defined twice
so I am telling you, so according to that I have given
b is equal to 1 and c is equal to 4
we did, now what will happen first
what will be evaluated first, first these star will be evaluated, star and slash will be evaluated first first of all, these star and slash will be evaluated
in which order they will be evaluated, in this direction
so b square will be evaluated first
then 4, a, c will be evaluated
then it will be divided by 2
and whatever result will be there, it will be multiplied by a
here this is a mess, see the result will be multiplied by a
so a means it will come up
so no, I don't want a so what i will do
to correct this i will put parenthesis
here i will put parenthesis
and here i will put parenthesis
so now see what is the value
now value is coming
minus 7
and if i remove parenthesis then what will it come? then it will come separately
see what is coming here
then here minus 199 is coming
that means something else is happening
something else is happening now
so I want this value
ok
like this
so I put parenthesis
like this
so whenever you have a complicated expression
then parenthesis is recommended
because if you put parenthesis because you will not get any loss by putting parenthesis
but you should know about precedence and associativity
it is a very important concept
so you can write b2-4ac like this
what will it do first
the most precedence is of parenthesis
so parenthesis overwrites this thing
I will show you
highest 14 precedence
see this is of parenthesis
here parenthesis
here parenthesis will take maximum percentage
and after taking
here this value will be first
then this value will be first
then this b square will be first
then this divided by this and this will be minus
and your result will be here
so if i take b and a both as 0
here then my result
should be 0
and it will be 0, see
it will have to be 0
and if I take a as 0
then actually denominator is becoming 0
so I do one thing, I make it 10 and make c as 0
if denominator is 0 then you will get arithmetic exception
exception means that you will have some problem while evaluating your expression
now here it will be 0, see now it is zero
okay
so denominator should not be zero
there is a rule of mathematics that if your denominator is zero
then expression is undefined and undefined will give you trouble in programming languages
sometimes in the form of error
sometimes your program will misbehave
so you definitely don't want this
okay so this was about our operator of precedence, I hope I could explain you associativity,
I could explain you precedence, these two concepts are such that to be very honest
I will be very transparent with you, I also understood this concept very late in my career,
when I used to give exams, I did not understand this,
I want to be very transparent with you you but I understood when I came to know how expressions are evaluated
and that's why I have given priority to this topic and explained it to you
so earlier I used to put a board mask and all at one time
star slash and I used to get very worried about these topics
but time has gone and I understood these things
and today I am explaining to you too, so I want you to understand all these things in a very early stage
and these things become important from the point of view of interview, from the point of view of exam
from the point of view of exam, all these things become very important
so I will explain to you in this way that you should not face any problem
at least you should not face the problem that I have faced
so if you like the video then like it
and if you are taking screenshot of this video
and sharing it on instagram
then tag me so that I can put your story again
and if you like the video then like it
I am not just saying it
many of you like the video from the heart
so if you like the video from the heart then like it
I don't want to irritate you by saying
like it, like it,ate you if you like this video
then like the upcoming videos
thats it for now
thank you so much for watching this video
and i will see you next time Thank you for watching.
5.0 / 5 (0 votes)