aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes operator precedence and associativity Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "operator precedence and associativity " Watch "operator precedence and associativity " New topic
Author

operator precedence and associativity

Basanti Mathad
Ranch Hand

Joined: Aug 27, 2002
Posts: 60
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
anushree ari
Ranch Hand

Joined: Sep 30, 2002
Posts: 98
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


anushree
ramki srini
Greenhorn

Joined: Aug 27, 2002
Posts: 26
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.
Vijay Albuquerque
Ranch Hand

Joined: Feb 17, 2002
Posts: 42
If I am not mistaken Operator Precedence & Associativity is not part of the SCJP Exam !!!
Am I Right ???


Vijay Albuquerque
SCJP 1.4, SCWCD 1.3
Basanti Mathad
Ranch Hand

Joined: Aug 27, 2002
Posts: 60
hey vijay,
i guess u need to know that to evaluate such expressions.
basanti
anushree ari
Ranch Hand

Joined: Sep 30, 2002
Posts: 98
hi ramki,
1\2==> 1-2=-1
-1-(-2)=1
so res is 1.
if wrong correct me?
Alfred Kemety
Ranch Hand

Joined: Aug 14, 2002
Posts: 279
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


Alfred Raouf - Egypt - SCJP 1.4<br />Kemety.equals(Egyptian) // returns true
Basanti Mathad
Ranch Hand

Joined: Aug 27, 2002
Posts: 60
how is this calculated?
(2 * Byte.MAX_VALUE)
thnx,
basanti
John Paverd
Ranch Hand

Joined: Nov 17, 2002
Posts: 115
(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


SCJP 1.4
Basanti Mathad
Ranch Hand

Joined: Aug 27, 2002
Posts: 60
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

Joined: Nov 17, 2002
Posts: 115
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.
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8815
    
    5
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...


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
Originally posted by Basanti Mathad:
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<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
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.
Basanti Mathad
Ranch Hand

Joined: Aug 27, 2002
Posts: 60
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.
more comments are welcome.
thanks again,
basanti
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: operator precedence and associativity