This week's book giveaway is in the Beginning Java forum.
We're giving away four copies of Get Programming with Java (MEAP only) and have Peggy Fisher on-line!
See this thread for details.
Win a copy of Get Programming with Java (MEAP only) this week in the Beginning Java 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
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Liutauras Vilda
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Ron McLeod
  • Ganesh Patekar
  • salvin francis
Bartenders:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst

Java8 specification - Poly Expression  RSS feed

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Java8, specification, I see the below lines:

15.8.5 Parenthesized Expressions
A parenthesized expression is a primary expression whose type is the type of the contained expression and whose value at run time is the value of the contained expression. If the contained expression denotes a variable then the parenthesized expression also denotes that variable.
The use of parentheses affects only the order of evaluation, except for a corner case whereby (-2147483648) and (-9223372036854775808L) are legal but - (2147483648) and -(9223372036854775808L) are illegal.

I did not understand 2 things here

1) As per my understanding "int a = (b + c)" --> The type of (b + c) is the type of the expression, which will be stored in "a" and the value of (b + c) is the value of the expression which is stored in "a". Is my understanding right ?

2) If - is kept outside of the bracket, then why is - (2147483648) and -(9223372036854775808L) illegal ?

I know this may be a very small question, but understanding the specification, I feel is a bit challenging.
 
Marshal
Posts: 62259
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

srini saitala wrote:. . .  "int a = (b + c)" --> The type of (b + c) is the type of the expression

Correct.

which will be stored in "a"

No, the type of the expression is evaluated and the compiler ensures that it is the same as the type of a. That depends on he declared type of the expression in ().

and the value of (b + c) is the value of the expression which is stored in "a". . . .

If it is the correct type, yes.

If - is kept outside of the bracket, then why is - (2147483648) and -(9223372036854775808L) illegal ?

I know this may be a very small question, but understanding the specification, I feel is a bit challenging.

The JLS says somewhere that the edge cases of integer literals 2147483648 and 9223372036854775808L must be |the operand of the sign change operator/unary minus. That doesn't allow even brackets or comments after the −. Try this JLS section. It is a notorious corner case, which many people don't know about, but it can cause you trouble. It appears in the book Java Puzzlers by Bloch and Gafter as the only occasion where you can break Java® code by adding “redundant” ().
 
srini saitala
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank You.
 
yeah, but ... what would PIE do? Especially concerning this tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!