• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

Primitive Assignments and Casting

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have the following piece of code.


My question is when we are doing the compiler is smart enough to point ou the loss of precision but if we do the compiler does not complain.Why is it so?
 
author
Posts: 23838
141
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


The detection of the "loss of precision" is for implicit casts. In this case, both the left and right side of the expression are ints. There is no casting being done.

Now, the addition does overflow. But overflows are not detected in Java.

Henry
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch!

There are two different things happening here.

In the case of byte c = 120 + 10, the int literals sum to an int value of 130, and the code is trying to assign this int value to a byte variable. In the Java Language Specification under 5.2 Assignment Conversion, it says...

...if the expression is a constant expression (�15.28) of type byte, short, char or int ... A narrowing primitive conversion may be used if the type of the variable is byte, short, or char, and the value of the constant expression is representable in the type of the variable.


In this case, the value is not representable in the type of the variable, so a compiler error results.

In the case of int i = 2147483638 + 20, the addition results in overflow, so the result might not be what's expected. But the value is still an int, and there is no problem in assigning an int value to an int variable. The possibility of overflow is something that programmers need to be aware of.
[ February 24, 2008: Message edited by: marc weber ]
 
Subhankar Kushary
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply.

I understand about the implicit cast during the byte assignment.

Could you please let me know why the java compiler cannot check this also for int.ints are always 32bits and the compiler should be able to identify(in case of literals)that the range is being exceeded?
is there any technical reason behind this?
 
Henry Wong
author
Posts: 23838
141
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Could you please let me know why the java compiler cannot check this also for int.ints are always 32bits and the compiler should be able to identify(in case of literals)that the range is being exceeded?
is there any technical reason behind this?



Not sure what you are asking for. Are you saying that it is so easy to check for overflows, that the compiler should do it, even though the Java specifications states that it is not done?

Or are you asking for the reason why the designers of Java choose to allow overflows/underflows without error?

Henry
[ February 24, 2008: Message edited by: Henry Wong ]
 
Would you like to try a free sample? Today we are featuring tiny ads:
Enterprise-grade Excel API for Java
https://products.aspose.com/cells/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!