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

General Tech Learning Aids/Tools . 2 years ago

  0   2   0   0   0 tuteeHUB earn credit +10 pts

5 Star Rating 5 Rating

Posted on 16 Aug 2022, this text provides information on Learning Aids/Tools related to General Tech. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Write Your Comments or Explanations to Help Others



Tuteehub forum answer Answers (2)


profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years 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)
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!

0 views   0 shares
profilepic.png
manpreet 2 years 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++)

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

0 views   0 shares

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.

tuteehub community

Join Our Community Today

Ready to take your education and career to the next level? Register today and join our growing community of learners and professionals.

tuteehub community