• 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

primitive casting

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear All,

While I was trying to digest the topic of casting, I got one doubt.
I'm following K&B book. On Page 183, there is this example -



The large-value-into-small-container conversion is referred to as narrowing and requires an explicit cast.
My question is ... why there is a chance of losing info?
Why explicit casting is required?
Even though both int and float are same size containers - 32bits.

Thanks,
-A
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


In the above example the trailing data after the point is truncated. That is:



The above code prints 100 in the output truncating any data after the decimal place. So you lost 0.001 in the integer variable that got truncated.


Secondly, explicit casting is required if you need to assign a bigger variable into smaller variable such in the example above, you need to do an explicit cast. Thats kind of assuring the compiler that I know what I am doing (Bert)...

Hope this helped!
[ June 20, 2008: Message edited by: Zaheer Ahmed ]
 
Arnav Velimala
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Zaheer for the reply.


Secondly, explicit casting is required if you need to assign a bigger variable into smaller variable



How float is considered a bigger variable than int?
Because both have the same 32bits.
 
Zaheer Ahmed
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Secondly, explicit casting is required if you need to assign a bigger variable into smaller variable



Let me clarify this more... Casting is not required only when you are assigning a variable of 8 bits to another one that is 16 bits, but its also required when there is an expected loss of data.

When assigning a float to an int we have possible loss of precision due to the fact float sets aside memory for representing decimal places but int does not.

Hope this helps...
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

How float is considered a bigger variable than int?
Because both have the same 32bits.



It's not based on bits, it's based on range -- interestingly, Java doesn't really care much about precision either.

Anyway, the range of an int is from -2,147,483,648 to 2,147,483,647. And the range of a float is from -3.40282346638528860e+38 to +3.40282346638528860e+38.

So, as you can see even though an int and a float are both 32 bits, a float covers a much bigger range. In fact, a float has a bigger range than a long, even though a long is 64 bits and a float is 32 bits.

Henry
 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rancher ,

However both int and float have 32-bit width .
But float can store integer as well as floating point number whereas
int can contain only integer numbers .

When you say
float somefloat = 25.45f ;
int a = somefloat ;
There is a risk that digit after decimal point will be lost .
That is why , compiler forces you to da explicit cast .
 
rubbery bacon. rubbery tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic