0

this is the code .

int x=99;
if(x++==x){
System.out.println("x++==x : "+x);//line1
}
x=99;
if(++x==x){
System.out.println("++x==x : "+x); //line2
}
x=99;
if(x==x++){
System.out.println("x==x++ : "+x); //line3
}
x=99;
if(x==++x){
System.out.println("x==++x : "+x); //line4
}
x=99;
if(++x==++x){
System.out.println("++x==++x : "+x);  //line5
}
x=99;
if(x++==x++){
System.out.println("x++==x++ : "+x); //line 6
}
x=99;
if(++x==x++){
System.out.println("++x==x++ : "+x); // line7
}
x=99;
if(x++==++x){
System.out.println("x++==++x : "+x); // line 8

when i compile and run this code i got below out put.

++x==x  : 100
x==x++  :100
++x==x++  :101

this output belongs to line 2,3 and 7.I want to know what happen to other lines, why they haven't output.

2 Answers2

1

This question is an excellent example depicting the differences between post and pre increment. In short, when you write ++ before a variable (pre-increment), the variable is first increased by 1 and then, it is used in the operation. If you write ++ after the variable then it is post increment and first, the variable will be used in the operation and then it will be incremented. You may refer this : How do the post increment (i++) and pre increment (++i) operators work in Java? for knowing about it in detail.

Utkarsh Sahu
  • 409
  • 3
  • 16
0

You must know about Operator priority in Java. I translate your sentences based Java grammar to understand better. It is helpful to read about parsers in compilers and grammars which used to parse them.

/*------------------------------*/
x++==x; 
//line1 equals to 
xl = x; x++; xr = x; xl == xr;  //xl=99; xr=100
/*------------------------------*/
++x==x; 
//line2 equals to 
++x; xl = x; xr = x; xl == xr;  //xl=100; xr=100
/*------------------------------*/
x==x++; 
//line3 equals to 
xl = x; xr = x;  x++; xl == xr;  //xl=99; xr=99;
/*------------------------------*/
x==++x; 
//line4 equals to 
xl = x; ++x; xr = x; xl == xr; //xl=99; xr=100;
/*------------------------------*/
++x==++x; 
//line5 equals to 
++x; xl = x; ++x; xr = x; xl == xr; //xl=100; xr=101;
/*------------------------------*/
x++==x++; 
//line6 equals to 
xl = x; x++; xr = x; x++; xl == xr; //xl = 99; xr = 100;
/*------------------------------*/
++x==x++; 
//line7 equals to 
++x; xl = x; xr = x; x++; xl == xr; //xl = 100; xr = 100;
/*------------------------------*/
x++==++x; 
//line7 equals to 
xl = x; x++; ++x; xl == xr; //xl = 99; xr = 101;
/*------------------------------*/

The following is an example grammar and you can see in equalityExpression which left expression and right expression is calculated before comparison.


primaryExpression
    :   Identifier
    |   Constant
    |   StringLiteral+
    |   '(' expression ')'
    ;


postfixExpression
    :
    (   primaryExpression )
    ('[' expression ']'        
    | ('++' | '--')
    )*
    ;

unaryExpression
    :
    ('++' |  '--' )*
    (postfixExpression
    |   unaryOperator castExpression        
    )
    ;
    
unaryOperator
    :   '&' | '*' | '+' | '-' | '~' | '!'
    ;

castExpression
    :   unaryExpression    
    ;

multiplicativeExpression
    :   castExpression (('*'|'/'|'%') castExpression)*
    ;

additiveExpression
    :   multiplicativeExpression (('+'|'-') multiplicativeExpression)*
    ;

shiftExpression
    :   additiveExpression (('<<'|'>>') additiveExpression)*
    ;

relationalExpression
    :   shiftExpression (('<'|'>'|'<='|'>=') shiftExpression)*
    ;

equalityExpression
    :   relationalExpression (('=='| '!=') relationalExpression)*
    ;
Majid Hajibaba
  • 3,105
  • 6
  • 23
  • 55