Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Basic Assignment-Problem

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
i tried the following:


OK this doesnt work, because the double cant be cast to float.

And than i tried this:

This doestn work, because int cant be cast to short.

Than i tried this:

Why does this work? 12 is an integer!


Thanks for your help
 
Ranch Hand
Posts: 41
Java ME Spring Objective C
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Micha,

This assignment works cause the value 12 is small enough to be hold by a short.
Remember that a short can hold a value from -32768 à 32767.

If you use a value directly, and not a variable to variable assignment like your second example,
the compiler will permit the assignation without complaining.

So this will work
short s1 = 32767; // Max value for a short

But this will cause the same compiling error as your others example:
short s1 = 32768; // out of range for a short

Test.java:8: possible loss of precision
found : int
required: short


 
micha koern
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sylvain Bouchard wrote:

This assignment works cause the value 12 is small enough to be hold by a short.
Remember that a short can hold a value from -32768 à 32767.






This Double-Value is also small enough to fit in a float (32 Bit).
But this wont compile.

So there must be something different, that is responsible for this.
 
Sylvain Bouchard
Ranch Hand
Posts: 41
Java ME Spring Objective C
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Widening primitive conversions do not lose information about the overall magnitude of a numeric value.
Indeed, conversions from an integral type to another integral type do not lose any information at all; the numeric value is preserved exactly.

however, an assignation to a float may lose information about the overall magnitude of the converted value in a non strctfp environment !
float and double are two special case.

You can read more about it, from this documentation
http://java.sun.com/docs/books/jls/third_edition/html/conversions.html
 
micha koern
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK- Thanks a lot!
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic