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 and associativity

Ranch Hand
Posts: 60
• Number of slices to send:
Optional 'thank-you' note:
I have gone through the associativity and presedence rules given in M.khalid book.But still am confused with grouping and evaluating an expression that has combination of all types of operators.
Like for eg. 1|2&3>>1/2+3.
can u explain me pls.
thank u.
basanti

Ranch Hand
Posts: 98
• Number of slices to send:
Optional 'thank-you' note:
hi,
exp is: 1 | 2& 3>> 1/2 + 3.
according to the precedence rule, /op first, then + op,and so on,
1 | 2& 3>> ((1/2) + 3).
1 | 2 & 3>>(1+3),
1 | 2 & 3>>4
1 | 2 & 0
1 | (2 & 0)
1 | 0
==> res is 1.
thx

Greenhorn
Posts: 26
• Number of slices to send:
Optional 'thank-you' note:
Anu,
1 | 2& 3>> ((1/2) + 3).
Why 1/2 is returning 1 in your explanation...
int f = 1/2 returns 0 when i tried...pls explain.

Ranch Hand
Posts: 42
• Number of slices to send:
Optional 'thank-you' note:
If I am not mistaken Operator Precedence & Associativity is not part of the SCJP Exam !!!
Am I Right ???

Ranch Hand
Posts: 60
• Number of slices to send:
Optional 'thank-you' note:
hey vijay,
i guess u need to know that to evaluate such expressions.
basanti

anushree ari
Ranch Hand
Posts: 98
• Number of slices to send:
Optional 'thank-you' note:
hi ramki,
1\2==> 1-2=-1
-1-(-2)=1
so res is 1.
if wrong correct me?

Ranch Hand
Posts: 279
• Number of slices to send:
Optional 'thank-you' note:
I don't know how did you do that Anush.. but 1/2 is simply 1 divided by 2, the result must be an int since the operands are int.
1 divided 2 = zero and remainder 1
1%2 equals 1

Ranch Hand
Posts: 60
• Number of slices to send:
Optional 'thank-you' note:
how is this calculated?
(2 * Byte.MAX_VALUE)
thnx,
basanti

Ranch Hand
Posts: 115
• Number of slices to send:
Optional 'thank-you' note:

(2 * Byte.MAX_VALUE)

First, Byte.MAX_VALUE is widened to an int, and then that int is multiplied by 2, which is also an int. The result is an int with the value 2 * 127 = 254

Ranch Hand
Posts: 60
• Number of slices to send:
Optional 'thank-you' note:
what would be the result of this...can anybody explain how to go about with such questions?
class Blue {
public static void main (String[] args) {
byte b1 = (byte)(2 * Byte.MAX_VALUE);
byte b2 = -1;
byte b3 = -2;
byte b4 = -3;
System.out.println((b1==b2)+","+(b1==b3)+","+(b1==b4));
}
}
thank u,
basanti

John Paverd
Ranch Hand
Posts: 115
• Number of slices to send:
Optional 'thank-you' note:

byte b1 = (byte)(2 * Byte.MAX_VALUE);

Here is some code I wrote

and here is the result:

When casting an int to a byte, all but the least significant 8 bits are discarded. Those 8 bits shown above represent -2 when stored in a byte
How did I know that 11111110 represented -2?
First, I know that the number is negative because the first bit is 1, which represents a negative number in 2's complement arithmetic.
Second, to get the absolute value, I flipped all the bits: 00000001 and then added 1, which gives 00000010, which is 2
I'm sure that someone will tell me if I am mistaken.

author
Posts: 9034
21
• Number of slices to send:
Optional 'thank-you' note:
Some of you have heard this rant from me before. I feel compelled to respond to this question because it is after all in the cert forum - therefore I have to assume this question pertains to someone who is studying for the test!
Other than a few key precedence rules like maybe casting and ++ and --, these complex precedence questions are just not on the scjp exam. Alfred, do you agree? These are interesting questions for sure, but unless you have all the other cert topics totally down cold, completely iced, absolutely mastered, you should spend your study time on another topic.
No one would write code like this in real life, and IMHO Sun probably doesn't think you should either. You'll see lots of parentheses on the exam questions! Ok, Ok I'll get off my soapbox now...

Ranch Hand
Posts: 1865
• Number of slices to send:
Optional 'thank-you' note:

what would be the result of this...can anybody explain how to go about with such questions?
class Blue {
public static void main (String[] args) {
byte b1 = (byte)(2 * Byte.MAX_VALUE);
byte b2 = -1;
byte b3 = -2;
byte b4 = -3;
System.out.println((b1==b2)+","+(b1==b3)+","+(b1==b4));
}
}
thank u,
basanti

Byte.MAX_VALUE is 01111111. To multiply by two just shift it to the left one bit. The result is 11111110 which is the two's compliment representation of -2.

Dan Chisholm
Ranch Hand
Posts: 1865
• Number of slices to send:
Optional 'thank-you' note:

Originally posted by Bert Bates:

Other than a few key precedence rules like maybe casting and ++ and --, these complex precedence questions are just not on the scjp exam. Alfred, do you agree? These are interesting questions for sure, but unless you have all the other cert topics totally down cold, completely iced, absolutely mastered, you should spend your study time on another topic.

Your point is valid. If a person wants to get the certification as quickly as possible then Khalid's book and my mock exam are probably not the best choice. A less rigorous certification guide would certainly allow a person to achieve a passing score more quickly. Even so, a more rigorous study of the material may have value beyond the certification.
I was surprised to find that the moderator of the Jobs Discussion message board does not recommend certifications because their value as a ticket to a particular job and career has not been proven. Others on that same message board argue that the value of the certification is not the piece of paper but is instead the knowledge gained from the process because the knowledge enhances the abilities of the programmer and provides the programmer with the knowledge needed to perform well on a job interview exam.
I agree with those that argue that the most valuable result of the certification process is knowledge. On the other hand, I recognize that other views may be equally valid. I suppose each person needs to decide what is most important.
Having said the above, I agree that spending a lot of time on operator precedence is not the best way to improve your score on the certification exam. On the other hand, who knows what might appear on your next job interview exam.

Ranch Hand
Posts: 60
• Number of slices to send:
Optional 'thank-you' note:
Thanks a lot.
I was not sure of the kind of questions in the real exam.So i was very much interested in going through each and every questions in the topic test of Dan's site.I have also gone through some of the post exam comments and it seems to me that we have to prepare well and the exam is difficult.
You r right....i have am thinking of taking the exam somewhere in next month.