This week's giveaway is in the JDBC forum.
We're giving away four copies of Java Database Connections & Transactions (e-book only) and have Marco Behler on-line!
See this thread for details.
Win a copy of Java Database Connections & Transactions (e-book only) this week in the JDBC forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Q's On My Exam Prep  RSS feed

 
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?
 
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 ]
 
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 ]
 
What's that smell? I think this tiny ad may have stepped in something.
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!