Categories

See More
Popular Forum

MBA (4887) B.Tech (1769) Engineering (1486) Class 12 (1030) Study Abroad (1004) Computer Science and Engineering (988) Business Management Studies (865) BBA (846) Diploma (746) CAT (651) B.Com (648) B.Sc (643) JEE Mains (618) Mechanical Engineering (574) Exam (525) India (462) Career (452) All Time Q&A (439) Mass Communication (427) BCA (417) Science (384) Computers & IT (Non-Engg) (383) Medicine & Health Sciences (381) Hotel Management (373) Civil Engineering (353) MCA (349) Tuteehub Top Questions (348) Distance (340) Colleges in India (334)
See More

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

General Tech Learning Aids/Tools

Max. 2000 characters
Tuteehub
Replies

usr_profile.png

User

( 5 months 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!

usr_profile.png

User

( 5 months 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

what's your interest


forum_ban8_5d8c5fd7cf6f7.gif