Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

operator precedence & associativity

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello
friends,
i have to attend my certification by 8th Nov.
but i still find "Operator precedence & associativity"
very confusing so just suggest me some links which may
be usefull.

please explain following

(1) int n = (- -1-3*10/5-1);

System.out.println("n = " + n);

it prints -6.so what exectly is the order
of evaluation here.

(2) class Test {
public static void main(String[] args) {
int divisor = 0;

try {
int i = 1 / divisor * loseBig();
} catch (Exception e) {
System.out.println(e);
}
}//end of main.
static int loseBig() throws Exception {
throw new Exception("loseBig");
}
}

Here it throws ArithmeticException,it means '1/division'
get evaluated first.But according to operator precedence
'divisor * loseBig()' should be evaluated first.
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yashendrakar
for your first question :
int n = (- -1-3*10/5-1);
I added parenthesis to help you see the order:
int n = ((- -1)-((3*10)/5)-1);
The unary - has highest precedence , so the first thing that happens is that 1 is negated then negated again so it has the value of 1.
Then 3 * 10 is evaluated then divided by 5
At this point the expression would look like this:
1 - 6 - 1, all of the operators now have the same precedence so they are evaluated left to right giving the answer -6.
For your second question:
Both * and / have the same precedence. In a case where two operators have the same precedence they are evaluated left to right. so the division would be evaluated first.
hope that helps

------------------
Dave
Sun Certified Programmer for the Java� 2 Platform
 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Dave:


for your first question :
int n = (- -1-3*10/5-1);
I added parenthesis to help you see the order:
int n = ((- -1)-((3*10)/5)-1);


Shouldn't the unary negative operator be assigned to 3 as well??
I am getting lost here.
Could you please explain why unary negative is applied AFTER evaluating the expression ((3*10)/5)
Thanks
 
Dave Vick
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Gopal
That - is not unary it is a binay operator becasue it is between two operands. Even if it was a unary then the first thing to do would to make it a -3 (negative 3) then multiply by 10 then divide by 5, which result in the same thing, -6.
hope that helps

------------------
Dave
Sun Certified Programmer for the Java� 2 Platform
 
Madan, Gopal
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Vick
It certainly did help. Thanks so much
 
yashendrakar chandrakar
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
vick
thanks.u realy explained properly.
 
We're being followed by intergalactic spies! Quick! Take this tiny ad!
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic