This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Implicit casting Q Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Implicit casting Q" Watch "Implicit casting Q" New topic
Author

Implicit casting Q

Jim Crawford
Ranch Hand

Joined: Sep 08, 2002
Posts: 127
Why does

(or)

work, but

(or)

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

Thanks.


<img src="cool.gif" border="0"> <img src="graemlins/beerchug.gif" border="0" alt="[beerchug]" /> <br />SCJP 1.4
dennis zined
Ranch Hand

Joined: Mar 07, 2003
Posts: 330
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.


SCJP 1.4<br />SCWCD 1.4
Jim Crawford
Ranch Hand

Joined: Sep 08, 2002
Posts: 127
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

Joined: Mar 07, 2003
Posts: 330
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

Joined: Sep 08, 2002
Posts: 127
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

Joined: Mar 07, 2003
Posts: 330
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.
Ray Stojonic
Ranch Hand

Joined: Aug 08, 2003
Posts: 326
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

Joined: Sep 08, 2002
Posts: 127
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.
Sripada Phani
Greenhorn

Joined: Nov 28, 2003
Posts: 7
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

Joined: Sep 08, 2002
Posts: 127
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.
 
wood burning stoves
 
subject: Implicit casting Q