General Tech Learning Aids/Tools . 1 year ago

Does Java Operators precedence is working when compare to C language perspective? [closed]

Does Java Operators precedence is working when compare to C language perspective? [closed]

0 views   2   0 likes   0 shares Tuteehub forum manpreet 2 answers
Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.


Tuteehub forum answer Answers (2)

manpreet Tuteehub forum best answer Best Answer 1 year ago


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)
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!

0 views   0 shares
manpreet 1 year ago


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++)


(j = i)++


a = b + c * d


(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++;



and it is not the same as

int i = 3;
int j = 4;
int k = (i = i - 1) * (j = j + 1);

which prints


0 views   0 shares

Related Tags

AskAnything Ask Anything

Ask Me Anything

ask me anything tuteehub

Hello! How can I assist you today?
Whether you have questions, need information, or just want to chat, feel free to let me know