Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Q's On My Exam Prep Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Q Watch "Q New topic
Author

Q's On My Exam Prep

K Ville
Ranch Hand

Joined: Aug 16, 2003
Posts: 58
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?


Thinking is the talking of the soul with itself...-Plato
Alton Hernandez
Ranch Hand

Joined: May 30, 2003
Posts: 443
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

Joined: Dec 17, 2002
Posts: 133
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

Joined: May 30, 2003
Posts: 443
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 ]
 
 
subject: Q's On My Exam Prep