my dog learned polymorphism*
The moose likes Beginning Java and the fly likes Declaring float & int. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Declaring float & int." Watch "Declaring float & int." New topic
Author

Declaring float & int.

Srinivasa Raghavan
Ranch Hand

Joined: Sep 28, 2004
Posts: 1228
Hello all,
In the following lines of code.

1) float f=1/3;
2) int i=1/3;


Line one returns a float and so a float value can be assigned to a variable of type float.

But in line 2 a float value gets directly assigned to an int wont there be any possible loss of precision. Why java compiler dint report an error.

Can any one throw some light on these type of basic things.


Thanks & regards, Srini
MCP, SCJP-1.4, NCFM (Financial Markets), Oracle 9i - SQL ( 1Z0-007 ), ITIL Certified
ramprasad madathil
Ranch Hand

Joined: Jan 24, 2005
Posts: 489

.


In the following lines of code.

1) float f=1/3;
2) int i=1/3;

Line one returns a float and so a float value can be assigned to a variable of type float.

But in line 2 a float value gets directly assigned to an int wont there be any possible loss of precision. Why java compiler dint report an error.

Can any one throw some light on these type of basic things


1/3 evaluates to a double (and not to a float as mentioned in your post. To check out try float f = 0.333 and you would get an error). java automatically narrows (is the usage right?) to

1. float in case i (without loss of precision)
2. int in case ii (with loss of precision)

You may want to read the jls documentation about widening and narrowing conversions here

Thanks,
ram.
Srinivasa Raghavan
Ranch Hand

Joined: Sep 28, 2004
Posts: 1228
I'm sorry if i'm asking a silly question.
java automatically narrows right then why does this piece of code wont work.

int i=66;
char j = i; // this gives error.
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
Originally posted by ramprasad madathil:
java automatically narrows (is the usage right?)
Java automatically widens primitive values to larger primitive types (and object references actually). The following would work.To go the other way you need to cast.
ramprasad madathil
Ranch Hand

Joined: Jan 24, 2005
Posts: 489

JLS states


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.


In your code below,



the type of i is known at compile time to be 'int'. However #1 in the quote above is not satisfied and it fails. It would require an explicit cast.

Try the following



ram.
Srinivasa Raghavan
Ranch Hand

Joined: Sep 28, 2004
Posts: 1228
Thanks Ram & David for the reply. I understood the concept.
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
I think everyone's missed an important point.

Since the variable and all the constants are integers, the expression "i=1/3" will be evaluated using integer arithmetic. In integer arithmetic, 1 divided by 3 is 0, a perfectly valid integer.

I suspect that if you wrote "i=1f/3f", for instance, it would not compile, because 1f/3f would be computed using float arithmetic. Haven't tried though. Go on, someone.

Just out of interest, in your float expression "f=1/3", what value did f get? It could get 0.0 or 0.333..., depending on whether the integers 1 and 3 get converted to float before the division. I guess a careful reading of the Java specs would tell us, if we could be bothered!


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
float f=1/3;

This results in f equal to 0.0f;

In Java, the result of an operation never depends on how the result will be used.

In this case,
1/3 is 0 (type int)
0 is cast to 0.0f (type float)
0.0f is stored in f


Mike Gershman
SCJP 1.4, SCWCD in process
 
 
subject: Declaring float & int.
 
Similar Threads
overriding
Marcus Green Exam 3, Q 1
thie output is different in C and JAVA
Increment Operator
java literals