Java, like mathematical expressions, follows operator precedence and associativity rules to determine the order of operations, ensuring predictable and consistent evaluation of expressions.
Mind Map
클릭해서 펼치기
클릭해서 인터랙티브 마인드맵 전체 보기
[Music]
Welcome back to CodeChef.
Have you ever solved a math expression
like 2 + 3 * 4 and wondered why the
answer isn't 20 but 14? That's because
Java, just like math, follows operator
precedence. Rules that decide which
operation runs first when multiple
operators appear together.
Think of operator precedents like rules
in a restaurant kitchen. Chefs prepare
main dishes before desserts. And if two
chefs are making the same dish, they
work left to right in order. Java does
the same. It decides which operation
gets served first and in what direction
to evaluate them. In Java, operator
precedence determines which operation is
evaluated first in an expression. While
associivity decides the direction when
operators have the same precedence
either left to right or right to left.
For example, multiplication and division
have higher precedence than addition and
subtraction and most operators are
evaluated from left to right except
assignments which go from right to left.
Let's see this in action. Suppose we
write the following code. In the first
case, multiplication happens before
addition. So the result is 14. In the
second parentheses change the order
addition happens first giving 20. For 10
- 2 + 5 since both plus and minus have
the same precedence Java evaluates from
left to right giving 13. But in 10 minus
open bracket 2 + 5 close bracket
parentheses force addition first. Result
three. A few quick tips. Use parenthesis
to make complex expressions clear and
bug-free. Assignment operators like
equals and plus equals have right to
left associivity evaluated from the end.
Logical and comparison operators also
follow specific precedence rules. So in
expressions like a and nb or or c, the
and runs before or. Now it's your turn.
Try the code chef problem called
operator precedence practice. You'll
read an expression and figure out how
Java evaluates it. The perfect way to
test your understanding.
Today you learned that precedence
decides which operation happens first
and associivity decides the direction of evaluation.
evaluation.
Together they make sure Java expressions
always produce predictable results. Keep
practicing, keep coding and remember
code, debug, repeat. Build mastery with CodeChef.
텍스트나 타임스탬프를 클릭하면 동영상의 해당 장면으로 바로 이동합니다
공유:
대부분의 자막은 5초 이내에 준비됩니다
원클릭 복사125개 이상의 언어내용 검색타임스탬프로 이동
YouTube URL 붙여넣기
YouTube 동영상 링크를 입력하면 전체 자막을 가져옵니다
자막 추출 양식
대부분의 자막은 5초 이내에 준비됩니다
Chrome 확장 프로그램 설치
YouTube를 떠나지 않고 자막을 즉시 가져오세요. Chrome 확장 프로그램을 설치하면 동영상 시청 페이지에서 바로 자막에 원클릭으로 접근할 수 있습니다.