-1

I'm studying java by Herbert Schildt's "Java for beginners" book. It is said that, being a destiny variable compatible and sufficiently big to store an origin one, an automatic conversion is done.

That being said, an Int should be able to store an Float and vice-versa, since they both have a 4 bytes size.

public class MyClass {
    public static void main(String args[]) {
        int i = 10;
        float f = i;

        float ff = 10;
        int ii = ff; 
    }
}

However, when compiled, this piece of code generetes the following error:

/MyClass.java:15: error: incompatible types: possible lossy conversion from float to int
        int ii = ff; 
                 ^
1 error

Why is there that, being a compatible type and sufficiently big to store each other, a float can store an int but an int cannot store a float?

abcson
  • 157
  • 9
  • 1
    This question is liable to downvotes as the `ii` and `ff` are not shown in the main code. – Ṃųỻịgǻňạcểơửṩ Feb 20 '19 at 21:47
  • 1
    How can 1234.5678 be stored as an 'int'? – CryptoFool Feb 20 '19 at 21:48
  • You could have just advised me instead of insta-downvoting. I edited the topic with the right piece of code. My intention here is learning the way Java deals with conversions, not simply posting a question. – abcson Feb 20 '19 at 21:50
  • 1
    We can't read your mind and know your intention. You have to ask what you really want to know if you expect an answer that satisfies you (I didn't downvote you as you're new here and that's the convention) – CryptoFool Feb 20 '19 at 21:51
  • It seems that, even with the right code, none of you tried to answer the question anyway. Of course you can't read minds, but as you spend your time frequenting a learning (and not a telepathy) forum, and since edits are free and fast to do, there's no reason to free downvote when you can just advice that the code is missing. – abcson Feb 20 '19 at 21:55
  • I think I tried to answer his question. I just asked how 1234.5678 can be stored as an 'int', hoping to appeal to his common sense vs some formal mumbo jumbo he was reading in a book. When he said we should have "advised him", I simply said that he had to tell us what sort of advice he wanted. I frankly still don't know what he's looking for if it isn't obvious to him that you can't store 1234.5678 in an 'int'. – CryptoFool Feb 20 '19 at 21:58
  • When stored in an int, divisions (that not necesseralyintegers results ) are automatically int casted with tollower. It is (Very) far from being a common sense issue. Following what java already does to deal with not integers numbers being stored in ints, 1234.5678 would have to simply be stored as 1234. – abcson Feb 20 '19 at 22:05
  • Being stored as '1234' is NOT storing '1234.5678'. That's storing '1234'. It IS common sense. You can't store the decimal portion of a number in a variable that only stores whole numbers. – CryptoFool Feb 20 '19 at 22:06
  • Explaining the implementional difference of storing 0 when you atribute int i = 3/4 and not being able to atribute 1234 when trying to fit a float on an int would be very helpful, since you seem to know that. – abcson Feb 20 '19 at 22:07
  • I was responding to two things in his post: 1) he says "That being said, an Int should be able to store an Float and vice-versa, since they both have a 4 bytes size.". 2) The error message says "possible lossy conversion from float to int". All I was is pointing out that 1) is wrong, and 2) is what happens. 1) is wrong because 1234 != 1234.5678, and 2) happens because 1234.5678 becomes 1234, resulting in loss of data. Why "int i = 3/4" does not produce a warning IS an interesting question. Maybe it should. But the fact that it doesn't is another matter, as the OP's Q has no math in it. – CryptoFool Feb 20 '19 at 22:24
  • There IS the question of why storing 10.0 in an 'int' causes an error. That's a worthy question, I suppose. The answer to that is that the compiler just isn't that smart. It's looking at the types of the two sides of the assignment, not looking at the actual value being assigned. – CryptoFool Feb 20 '19 at 22:26
  • It looks to me like [the compiler is forbidden from allowing that assignment](https://docs.oracle.com/javase/specs/jls/se8/html/jls-5.html#jls-5.2). ```ff``` is not a "constant expression" despite the fact it never changes :-( –  Feb 21 '19 at 00:21
  • Are you comming from c/c++? –  Feb 24 '19 at 15:12

4 Answers4

1

Because float contains numbers also after the decimal point and int does not. Without this you shouldn't be able to simply trim the decimal part from the number by explicitly casting the float to int too.

  • More exactly, converting from a `float` to an `int` causes a loss of precision, which is a loss of information. Accordingly you have to use an explicit cast: `int i = (int)floatVariable;` – Zachary Hamm Feb 20 '19 at 22:00
  • But isn't it what the compiler does when you atribute int i = 3/4, making it 0? – abcson Feb 20 '19 at 22:13
  • Not exactly. "int divided by int" is defined as an operation that results in an int; truncation of the result is part of the [specification of the operator](https://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.17.2). Computers generally have an instruction that does precisely that (not withstanding the fact that Java executes in a virtual machine). So the result of the division is an int, and it is straightforward to store that in an int variable. –  Feb 21 '19 at 00:10
0

an Int should be able to store an Float and vice-versa, since they both have a 4 bytes size.

The size of the data is not the important part. Rather it is about the values which can be stored in the give number of bytes. int can only store whole number values between -2^31 and 2^31-1. On the other hand, float can store decimal values.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
0

As you can see, you can store a int into a float, but you can't put a float inside a int.

This heappens because the internal structure of float is a decimal number and int, as its name says, is integer.

For more information you can take a look into this discussion about How are floating point numbers are stored in memory.

Lucas Sousa
  • 192
  • 3
  • 14
-1

Int to float is never a lossy conversion if the integer value will be within the range of maximum integer value that float can store as value will always be stored/converted to <intvalue>.0 like 4 will be stored as 4.0. But in case of conversion from float to int, your fraction value will be lost so JVM gives you such error. If you can take such risk then you have to explicitly convert the float value into int.

Ankit Agrawal
  • 596
  • 5
  • 12