• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Operator precedence

 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
The output of the following code:
class Test3{
public static void main(String args[]){
int i=0,j=1;

if((i++==1)&&(j++==2)){ //5
i = 42;
}

System.out.println("i = " +i+" j ="+j);
}
}

is i = 1 j=1
I was expecting the output of i=42 j=2
As the prcedence of the postfix operator(++) is greater than the prcedence of the equality operator (==). I thought that the increment should take place before the comparision in the condtion on line 5. Can someone explain the reason for the output.
Thx in advance.
bye
sk
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Test3{
public static void main(String args[]){
int i=0,j=1;
if((i++==1)&&(j++==2)){ //5
i = 42;
}
System.out.println("i = " +i+" j ="+j);
}
}

hello dear i think u have not read the Q
is i = 1 j=1
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
consider this, it would probably help u:
class Test3{
public static void main(String args[]){
int i=0,j=1;
boolean b = (i++==1);//i becomes 1 after the assignment
if((b)&&(j++==2)){ //5
System.out.println("I am in if" +i +j);
i = 42;
}
System.out.println("i = " +i+" j ="+j);
}
}
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As a public service to all you SCJP candidates out there, please note:
1) These detailed precedence discussions are very interesting...
2) Complex precedence questions are NOT on the exam!!!
So, if you want to pursue this arcane knowledge for the sheer joy of learning, then go nuts...
If however, you are studying for the exam, focus on the hard stuff that's actually going to be in there...
GC, threads, inner classes...
Bert
 
Ranch Hand
Posts: 443
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by sai kumar:

I was expecting the output of i=42 j=2
As the prcedence of the postfix operator(++) is greater than the prcedence of the equality operator (==). I thought that the increment should take place before the comparision in the condtion on line 5. Can someone explain the reason for the output.


Because of the && operator, your program performed a short-circuit evaluation.
When the left condition was NOT satisfied (i++==1), it did not perform the remaining condition, hence the output.
[ April 03, 2004: Message edited by: Alton Hernandez ]
 
C. Magmanum
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Crusty Magmanum:
consider this, it would probably help u:
...boolean b = (i++==1);//i becomes 1 after the assignment
}


Just in-case I didnt make sense in my last posting, here is what I meant.
this operation (i++==1) results in false if the initial value of i is 0.
i=0 gets compared with 1,
i
becomes incremented after the operation -as is the case with b = (i++==1); .
++someVariable means the value of someVariable is someVariable + 1
someVariable++ means the value of someVariable is someVariable (1 is added to it after operation)
 
sai kumar
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thx a lot guys
It clarified
bye
sk
 
We're being followed by intergalactic spies! Quick! Take this tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic