File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

operator precedence and associativity

 
Basanti Mathad
Ranch Hand
Posts: 60
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 98
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
ramki srini
Greenhorn
Posts: 26
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 42
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I am not mistaken Operator Precedence & Associativity is not part of the SCJP Exam !!!
Am I Right ???
 
Basanti Mathad
Ranch Hand
Posts: 60
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey vijay,
i guess u need to know that to evaluate such expressions.
basanti
 
anushree ari
Ranch Hand
Posts: 98
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi ramki,
1\2==> 1-2=-1
-1-(-2)=1
so res is 1.
if wrong correct me?
 
Alfred Kemety
Ranch Hand
Posts: 279
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Basanti Mathad
Ranch Hand
Posts: 60
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how is this calculated?
(2 * Byte.MAX_VALUE)
thnx,
basanti
 
John Paverd
Ranch Hand
Posts: 115
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(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
 
Basanti Mathad
Ranch Hand
Posts: 60
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8898
5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 1865
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 60
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic