• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Q's On My Exam Prep

 
K Ville
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Alton Hernandez
Ranch Hand
Posts: 443
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Deep Chand
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic