Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why is a good idea do casting if there is a possibility of lose precision?

 
André Asantos
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



Why is a good idea do casting if I can less precision?

André AS
 
Jonas Isberg
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about if you want to lose precision?
 
André Asantos
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I heard about that if is not a specific primitive type for specified situation I can lose precision... got it?
 
André Asantos
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
look at an exemple:




It will print a non-exact number... "1" intead of 1.5
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65111
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look up integer.
 
Henry Wong
author
Marshal
Pie
Posts: 21385
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

You should also understand the distinction between implicit and explicit casting. With explicit casting, it means that the programmer wants the data type casted, and will take responsibility for any lost of precision.

So... why cast? Because the program needs the data in that type, and the programmer took care of possible lost of precision.

Henry
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic