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

# Q's On My Exam Prep

Ranch Hand
Posts: 58
• Number of slices to send:
Optional 'thank-you' note:
Hi again to all. I have another set of questions that I need to clarify. Please bear with me, friends! Thanks!
Q1: Given:
expr1 - if((x<3) | (y>4) && (z==2))
expr2 - if(((x<3) | (y>4)) && (z==2))]
How does these two differ?
What is the precedence of | and &&? Is it left to right or is && given higher precedence?

Q2. Given:
short sh = new short[(short)32];

Is the cast required even if the integer literal fits any integral data type? And is it also true that array indices can only be int-or-smaller, and NEVER a long data type?

Ranch Hand
Posts: 443
• Number of slices to send:
Optional 'thank-you' note:
Hi K Ville,

Originally posted by K Ville:
Hi again to all. I have another set of questions that I need to clarify. Please bear with me, friends! Thanks!
Q1: Given:
expr1 - if((x<3) | (y>4) && (z==2))
expr2 - if(((x<3) | (y>4)) && (z==2))]
How does these two differ?
What is the precedence of | and &&? Is it left to right or is && given higher precedence?

The 2 expressions are not the same. The operator && has a higher precedence than |. So expr1 will actually be evaluated as:
(x<3) | ( (y>4) && (z==2) )
Precedence has nothing to do with 'left to right'. Precedence will just tell you how to put the parenthesis, or how to group the operands. The evaluation of the expression is still from left to right. If you would replace | with ||(short circuit) in expr1, and x<3 evaluates to true, then the rest of the expression will not continue, which prove this point.
Run the program below and pass a value that would make x < 3 and y>4 to true and you will see the difference.
e.g. java x 2 5

Originally posted by K Ville:

Q2. Given:
short sh = new short[(short)32];

Is the cast required even if the integer literal fits any integral data type? And is it also true that array indices can only be int-or-smaller, and NEVER a long data type?

Yes, you are right, you can only use an int value for an array subscript. By int values includes byte, short, char, but not long. The reason why they are accepted is because unary numeric promotion is performed and they becomes int.
Hope this helps.
[ September 02, 2003: Message edited by: Alton Hernandez ]

Ranch Hand
Posts: 133
• Number of slices to send:
Optional 'thank-you' note:
I thought that | has higher precdence than &&. At least that's what the java tutorial says. So both the expressions should be same. Below is the link:
http://java.sun.com/docs/books/tutorial/java/nutsandbolts/expressions.html
Thanks,
Deep

Alton Hernandez
Ranch Hand
Posts: 443
• Number of slices to send:
Optional 'thank-you' note:
You're right Deep, sorry about that. I mixed up || with |.
Ignore evrything I said then.
I'm going to sleep
[ September 02, 2003: Message edited by: Alton Hernandez ]

 Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters?