• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

operator precedence

 
mohammad shaid
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Source K&B 5 page 784 .the question on K&B is regarding static import.. i understand the concept of static import but during the coarse of study through that question i rectified the code to see what will the output be.
The code below is similar to the one in K&B,excpet that i have not put the code in 2 different files instead tried it this way.
The whole concept is that i am getting 30 as the output whereas i expected it to be 36. so can someone explain me the logic of operator involvement here??


Is the value 5 substituted before the operation or is it that the n after * gets its value after the operation inside () brackets is over???
 
mohammad shaid
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As far i know the operation is carried from left to right
 
Ankit Garg
Sheriff
Posts: 9521
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(n++)*n first becomes (5)*n and then n's value is incremented so the expression becomes 5*6. Look on the internet for post increment operator related tutorials to get more details...
 
Rufat Piriyev
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In K&B this topic isn't very good described . If you have JLS or Mughal SCJP you must check operator precedence. It says that unary operator doing first
this is from Mughal book:

Postfix operators [] . (parameters) expression++ expression--
Unary prefix operators ++expression --expression +expression -expression ~ !
Unary prefix creation and cast new (type)
Multiplicative * / %
Additive + -
Shift << >> >>>
Relational < <= > >= instanceof
Equality == !=
Bitwise/logical AND &
Bitwise/logical XOR ^
Bitwise/logical OR |
Conditional AND &&
Conditional OR ||
Conditional ?:
Assignment = += -= *= /= %= <<= >>= >>>= &= ^= |=
 
mohammad shaid
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ankit and Rufat.. i get the point now ..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic