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.
So to answer your question, an explicit cast is not required.
Hope this helps.
[ September 02, 2003: Message edited by: Alton Hernandez ]