Speak now
Please Wait Image Converting Into Text...
Embark on a journey of knowledge! Take the quiz and earn valuable credits.
Challenge yourself and boost your learning! Start the quiz now to earn credits.
Unlock your potential! Begin the quiz, answer questions, and accumulate credits along the way.
General Tech Learning Aids/Tools . 1 year ago
Turn Your Knowledge into Earnings.
Actually i am a kickbhut in C. I just started to learn Java. And directly preparing for OCJP6 Certification. In Kathy-Sierra book, and as well as in exam syllabus also, there's no Operator's precedence matter. But i was in fully confused when i saw the Java Operators precedence table from orablce-sun Documentation.
From my C-Language: Operators(44) precedence table 1) () [] .(dot) 2)Unary: ++pre/--pre, -, +, (cast) 3)Arithmetic: 4)bitshift 5)relational: 6)bitwise: 7)logical: 8)ternary Operator ?: 9)Assignment operators = += -== etc 10) unary post++/post-- //In C if i say.. int main() { int i = 1, j; j = i++; /* The above expression is solved based on the operators precedence! 2 operators i have used! one is unary post, another is assignment. here, assignment is higher precedence then unary post. So, first i value is assigned to j. then, i value is incremented because of post increment operator! */ printf("i = %d, j = %d", i, j);// i = 2, j = 1 return 0; } Java Operators precedence table 1)postfix expr++ expr-- 2)unary ++expr --expr +expr -expr ~ ! 3) arithmetic 4)shift << >> >>> 5)relational < > <= >= instanceof equality == != 6)bitwise AND & bitwise exclusive OR ^ bitwise inclusive OR | 7)logical AND && logical OR || 8)ternary ? : 9)assignment = += -= *= /= %= &= ^= |= <<= >>= >>>= //What my biggest doubt is...! class Test { public static void main(String[] args) { int i = 1, j; j = i++; /* according to the operators precedence table! unary post operator is 1st precedence than assignment operator! This way first i value should be increment! then after assignment should happen! How come here also i am getting the same values as in C language? */ //System.out.printf("i = %d, j= %d", i, j); //i = 2, j = 1 System.out.println("i = "+i+", j = "+j); // i = 2, j = 1 } }
Please some one clarify me!
I think you are confused as to what precedence means. It doesn't mean order of execution in all cases. It means order of nested or association e.g. it means that
j = i++
is the same as
j = (i++)
not
(j = i)++
like
a = b + c * d
is
(a = (b + (c * d)))
As Jacob notes: the ++ means increment this value after using or saving the original value. In Java this is always done at the end, whereas in C and C++ it is not defined as to when this will happen.
++
EDIT: A more complex example is as follows
int i = 3; int j = 4; int k = i-- * j++; // same as int k = i * j; i--; j++; System.out.println(k);
prints
12
and it is not the same as
int i = 3; int j = 4; int k = (i = i - 1) * (j = j + 1); System.out.println(k);
which prints
10
General Tech 9 Answers
General Tech 7 Answers
General Tech 3 Answers
General Tech 2 Answers
Banking, Finance & Insurance
Mobile Technologies
Career Talk
General Tech
Commerce & Accounting
Career Talk0 Answers
General Tech0 Answers
Digital Marketing0 Answers
Internet of Things0 Answers
Interview Questions0 Answers
Your experience on this site will be improved by allowing cookies.