Kamil Hlubek wrote:
Why the addition in the first example do not end with a compilation error although an binary operator is used.
Kamil Hlubek wrote:
It would be nice if somebody could me explain the differences between the two examples.
( I am pretty sure, that it is something about the literals and non-literals, but I find this statement from this book is generally very abstract,
because every integer is implicity an int ( This should be wrong if you look at the first example, but I want to understand it completely so I write this here down ).
Kamil Hlubek wrote:
Line 5 does not compile, because a method invocation is not a compile-time constant value, so the compiler casts the returned byte-value to an int value, which result in a compilation error, because of the possible loss of precision ( int to byte does not work! ).
-> This line compiles, but why and why line 6 suddenly do not compile?
The literal() method returns a byte value, and that byte value is simply being assigned to a byte variable. There are *no* other operations happening -- it is simply an assignment (byte to byte). Why should it lose precision? Meaning why should it not compile?
Kamil Hlubek wrote:
This was really a careless mistake. Apparently I worked to long with it and did not focused on it. Sorry for that.
Kamil Hlubek wrote:The first line ( Line 1 ) is a compile time constant variable, because it is a final variable, is assigned at the same time as the declaration of it, has a primitive data type and it is initialized with a compile-time constant value.
Kamil Hlubek wrote:Line 2 do NOT compile, because of the use of a non-compile-time constant variable ( "MAX_SIZE" ). The reason why it is not a compile-time contant value is, that it is not a final variable.
Kamil Hlubek wrote:Line 3 compiles without a problem, because, as previously explained, "b1" is a compile-time constant variable and 15 is a literal, which is a component for compile-time constant expressions. The result is a compile-time constant expression denoting a value of the primitive data type int.
Kamil Hlubek wrote:Line 4 do not compile, because of the two operands. The first operator is not a compile-time constant variable, because it is not instantiated at the same time as the declaration . The second operator is not a compile-time constant variable, too, but not because of the same reason. The reason here is that the variable "MAX_SIZE" is not declared as a final variable.
BUT this compilation error would not occur, because a static method can not use a non-static variable, ehich is here size.
Kamil Hlubek wrote:Line 5 does not compile, because a method invocation is not a compile-time constant value, so the compiler casts the returned byte-value to an int value, which result in a compilation error, because of the possible loss of precision ( int to byte does not work! ).
-> This line compiles, but why and why line 6 suddenly do not compile?
Kamil Hlubek wrote:On line 6, b(four) is not a final variable so it is not a compile-time constant variable and so the problem is the same, as on line 5.
Kamil Hlubek wrote:Line 7 compiles, because of the reasons I already mentioned previously ( b1 is a compile-time constant variable and the literals are compile-time constants.
Line 8 compiles, because of the same reason, as line 7.
Line 9 compiles. ( I do not explain it anymore, because I have already written it very often and the variables are still the same )
Kamil Hlubek wrote:Line 10 do not compile , because Byte -> no compile-time constant value .
Without subsidies, chem-ag food costs four times more than organic. Or this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
|