This week's giveaway is in the Spring forum.
We're giving away four copies of Learn Spring Security (video course) and have Eugen Paraschiv on-line!
See this thread for details.
Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Declaring float & int.

 
Srinivasa Raghavan
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
ramprasad madathil
Ranch Hand
Posts: 489
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
.


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
Posts: 1228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 489
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ram & David for the reply. I understood the concept.
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic