• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

float gets truncated to int even without warning

 
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I knew that Java compiler protects the developers from data truncation. But what's happening here?



Why not Java even warned me about the data truncation? Is this a bug from Java?
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, it's not a bug. It's specified in the JLS here http://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.26.2
 
Rancher
Posts: 1093
29
Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Prosenjit,

First off, learn number systems and what to expect from them. I see your comment that the number is truncated and I think: "Duh! What did you expect when it was converted to an integer type--real numbers not only whole numbers?"

Here is what I think is happening.

int = int + float; doesn't compile because you are doing an integer operation and the float is not an integer--Java now "protects you" from yourself in that reguard.

int += float; works fine because you have a float operation and the int is converted to a float yielding a float number, then it is assigned to an int and the whole number, not the real number, is retained as instructed for the result.

When I say learn number systems, you are obviously come a point in your life that is past the 3rd grade where I remember learning about integers. That was a half century ago, and I still remember it.

There are some expectations on some things, like when you program you know how to manipulate numbers. Without basic knowledge of numbers, you are running along a path that is basically unpredictable for you to follow--do you know what underflow is? Overflow? Do you know what accuracy errors are? What's the difference between a float and a double? Do you understand the built in inaccuracies of non integer numbers in a binary based system and how to deal with that?

So take up a little study and save yourself a lot of grief in a discipline that is based in numbers, math, with manipulation and use of such.

Les
 
Prosenjit Banerjee
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:No, it's not a bug. It's specified in the JLS here http://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.26.2


Thank you so much for such a prompt reply. And yes, it has been stated so clearly in JLS. Thank you again.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Les see the JLS section I cited for the explanation. Compound assignment statements of the form op= have an implicit cast to the type of the variable being assigned to.
 
Prosenjit Banerjee
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:@Les see the JLS section I cited for the explanation. Compound assignment statements of the form op= have an implicit cast to the type of the variable being assigned to.


I have understood the rule. But what can be the motivation behind it? I mean, if there is a possibility of some data to be lost if the programmer is not very careful, does not that go against the motto of Java?
 
Marshal
Posts: 79177
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't know; that was decided 21 years ago by people who aren't posting here. Maybe it is because that is how C and C++ did it. Maybe, and maybe not.
 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Prosenjit Banerjee wrote:
I have understood the rule. But what can be the motivation behind it? I mean, if there is a possibility of some data to be lost if the programmer is not very careful, does not that go against the motto of Java?


considering the probability of such sort of carelessness, i think it's more worthwhile to keep the cleanness of the java system.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

drac yang wrote:. . . considering the probability of such sort of carelessness, . . .

Whom are you accusing of carelessness?
The designers of the language? But there is an example in the Java® Language Specification (=JLS) showing exactly the same sort of result as we have here. Beware: the JLS can be difficult to read. The compound operators are not mentioned in the Java™ Tutorials; maybe they shou‍ld be, but I would say, “Not guilty, M'Lud.”
Whoever teaches the language? Well, they ought to be aware of the operators and their effects.
Whoever is writing the code? Again they shou‍ld remember the effects of the operators.

Does this mean the buzzword about “Simple” is incorrect? Maybe it does, maybe it doesn't. Remember that simple doesn't mean easy, but straightforward; the effect of the casts shou‍ld be easy enough to remember.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Prosenjit Banerjee wrote:I have understood the rule. But what can be the motivation behind it? I mean, if there is a possibility of some data to be lost if the programmer is not very careful, does not that go against the motto of Java?



I don't think so. There are many obvious places where data is "lost" in Java without causing the compile to nag the programmer. One we see here quite frequently is this:



As you know the result of this statement is to assign 0 to the oneThird variable, which is surprising to many new programmers. Actually I'm not sure what this "motto" is which you're referring to but Java certainly allows data to be "lost" without warning.

 
Ranch Hand
Posts: 460
6
Netbeans IDE Oracle Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The compiler does things stealthily.

 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dana Ucaed wrote:The compiler does things stealthily.

No, it doesn't. It follows the rules. The rules of integer division were fixed before I was born, and you need to know them. It is no good forgetting the rules and then blaming the javac tool for remembering them. Look at this code:-You can use the javap tool to view the bytecode:-

javac DivisionDemo.java
javap -c DivisionDemo

I shall leave you to work out which rule it is that means neither 1 nor 3 appears in that bytecode, only 0 in lines 4‑5.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic