• 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
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

Implicit casting Q

 
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why does

(or)

work, but

(or)

doesn't?
Its a narrowing cast in both cases, but the second one doesn't compile.

Thanks.
 
Ranch Hand
Posts: 330
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1.0 is double by default. So at float x = 1.0, you are assigning a double to a float. To properly assign to a float it should be: float x = (float)1.0 or float x = 1.0F.
 
Jim Crawford
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by dennis zined:
1.0 is double by default. So at float x = 1.0, you are assigning a double to a float. To properly assign to a float it should be: float x = (float)1.0 or float x = 1.0F.


I know this. That's what those 'or' things were for - sort of to show they are the same.
So... both being narrowing casts, why is the one implicit and the other not?
 
dennis zined
Ranch Hand
Posts: 330
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry I dont have the definitive answers for you Jim. I see floating-point numbers (float,double) as a totally different animal from integer types (byte, short, int, long) and I simply just try to remember the rules when dealing with each. Hope other guys here can shed light on this topic.
[ December 08, 2003: Message edited by: dennis zined ]
 
Jim Crawford
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by dennis zined:
Sorry I dont have the definitive answers for you Jim. I see floating-point numbers (float,double) as a totally different animal from integer types (byte, short, int, long) and I simply just try to remember the rules when dealing with each. Hope other guys here can shed light on this topic.
[ December 08, 2003: Message edited by: dennis zined ]


Thanks, but it seems like people don't really know... or maybe I haven't searched enough. It has to be in the JLS, but that document is just a pain to deceipher.
Regards.
 
dennis zined
Ranch Hand
Posts: 330
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

It has to be in the JLS, but that document is just a pain to deceipher


Yeah, I totally agree. Why dont you wait a while, other ranchers would be waking up soon and see your post. They'll probably have good answers for you.
 
Ranch Hand
Posts: 326
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
from JLS �5.2


Assignment conversion occurs when the value of an expression is assigned (�15.26) to a variable: the type of the expression must be converted to the type of the variable. Assignment contexts allow the use of an identity conversion (�5.1.1), a widening primitive conversion (�5.1.2), or a widening reference conversion (�5.1.4). In addition, a narrowing primitive conversion may be used if all of the following conditions are satisfied:
1)The expression is a constant expression of type byte, short, char or int.
2)The type of the variable is byte, short, or char.
3)The value of the expression (which is known at compile time, because it is a constant expression) is representable in the type of the variable.
If the type of the expression cannot be converted to the type of the variable by a conversion permitted in an assignment context, then a compile-time error occurs.


in:
short x = 32767;
and
short x = (int)1;
and even
final int i = 1;
short x = i;
the value is known to fit into the short at compile time, so it flies.
int i = 1;
short x = i;
and
short x = 32768;
require an explicit cast
The double -> float conversion fails the first condition for narrowing, so either the proper format must be used (ie: 1.0f) or it must be explicitly cast.
[ December 08, 2003: Message edited by: Ray Stojonic ]
 
Jim Crawford
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ray Stojonic:
The double -> float conversion fails the first condition for narrowing, so either the proper format must be used (ie: 1.0f) or it must be explicitly cast.
[ December 08, 2003: Message edited by: Ray Stojonic ][/QB]


Thanks!
That helps.
Cheers.
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jim,
Integral types (byte, short, int, long ) are represented in signed 2's complement integers. Whereas Floating point types float & double values are represented in single precision and double precision formats respectiely. These are IEEE 754 standards.
Which means a Floating type number(may be a double), whose integral part is within the float data type range, may loose its precision once you assign it to a float variable. That's why it is a narrowing coversion.
import java.io.*;
class Test{
public static void main(String a[]) {
float fa=3.03222222F;
double da=3.03222222;
System.out.println("Values fa: "+fa); // prints 3.0322223
System.out.println("Values da: "+da); // prints 3.03222222
}
}
I think this could be reason for not allowing floating type literals to be assigned to float variables.
Regards,
Phani.
 
Jim Crawford
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Sripada Phani:

I think this could be reason for not allowing floating type literals to be assigned to float variables.


Thanks.
Its probably something like that, yes. Its better knowing where a JLS rule comes from... it helps in compiling your own rules at times when there are none to guide you.
 
I didn't do it. You can't prove it. Nobody saw me. The sheep are lying! This tiny ad is my witness!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic