• 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

can i assign long var to float?

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdy all:
a question :
in Mr.dan's mock:

class A {
public static void main (String[] args) {
float a = 1; // 1
float b = 1L; // 2
float c = 1F; // 3
float d = 1.0; // 4
}
}

A compile-time error is generated at which lines?
a. 1
b. 2
c. 3
d. 4
e. None of the Above


in the line 2, "long" has 64 bits while "float" has 32, i think , assign long to float is narrow assign ,should cast it.why no error occupy? pls...
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For primitive datatypes, a value of a narrower datatype can be converted to a value of a broader datatype without loss of information. This is called widening primitive conversion.
This is the order of the widening conversion:
byte-->short-->int-->long-->float-->double
char-----------^
This is the reason why the primitive value long can be widen to float. However, since a decimal numeric by default is a double (like 1.0) this would cause a possible loss of precision error. What I can suggest is that you explicitly cast the value so the code would compile.

Hope that makes sense.
[ November 20, 2002: Message edited by: Roan Nicolas ]
 
Keen Chen
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thx!
yes, a narrower datatype can be converted to a broader one, without cast.
but, long and float, which is the broader one?
long has 64 bits and float has 32bits.
so the broader is long. so should convert from float -> long. rightn?
what can i use to discern the broader one? can not use its capability bits?? but what?
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
float has a range greater than long because it can store values with exponents.
Hence,
float f = 1.897e50F is
1.897 * 10^50
Therefore, although float occupies half the byte-size than long, it can in reality store a larger range of values !!!
[ November 21, 2002: Message edited by: Vijay Albuquerque ]
 
Keen Chen
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
oh!
i got it!
thx a lot for ur help
BTW: sth. error in ur words.
Float.MAX_VALUE = 3.4028235E38
and 1789E50 should use double.
 
I think she's lovely. It's this tiny ad that called her crazy:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic