Simon Roberts wrote:While we're on the topic "rule 1" as presented is incomplete. The convenient assignment of an "int that's small enough for the target type" applies to *compile-time constants", of which literals are merely one example.
Try the following:
Gabriel Bristot wrote:Does this happens because, since the value of onlyTen is constant, the compiler is sure of it's value (and that it won't change) during compilation?
And could you give me some more examples of compile-time constants? All I can think of now are boolean and string literals.
Well doneGabriel Bristot wrote:I finally understood it. . . .
Stephan van Hulst wrote:First, I'm not quite sure where the term "compile-time constant" comes from. It's widely used on CodeRanch, and I have also used the term on occasion, but the technical term is "constant expression".
A compile-time constant expression is an expression denoting a value of primitive type or a String that does not complete abruptly and is composed using only the following:
Campbell Ritchie wrote:Another way of putting it is that casts (I don't like that word) have the same precedence as ++x, higher than * / % + (addition) and - (subtraction).
Don't know. The problem I have with that word is that it is used both of primitives, where it can change the value of the expression, and reference types, where it cannot change its type. Little example, which happens to use unboxing conversion too:-Mike Simmons wrote:. . . Hmmm, what term would you prefer? . . .
Yes, I know it is useless code, but when you write double the cast proceeds normally and when you write Double it won't even compile. The sort of thing we old hands are familiar with, but I think it can confuse beginners.$ jshell
| Welcome to JShell -- Version 22.0.1
| For an introduction type: /help intro
jshell> Double d = 1.234e56;
d ==> 1.234E56
jshell> (int)d
| Error:
| incompatible types: java.lang.Double cannot be converted to int
| (int)d
| ^
jshell> double d = 1.234e56;
d ==> 1.234E56
jshell> (int)d
$3 ==> 2147483647
Agre; this JLS section lists CastExpression as one kind of unary expression.Mike Simmons wrote:. . . ]this Java Precedence table lists other unary operations as higher in precedence than casts, rather than equal. I think that's incorrect . . .
Nor can I.but to be fair, I can't come up with a good code example where this distinction would actually make a difference. . . .
You used a term I don't like in that article: “sign bit”. In S&M (=sign and magnitude, not some other meaning!) integers, there is a true sign bit; changing the leftmost bit converts 123 to −123 and vice versa But I have never seen S&M integers used, only unsigned and two's complement. In two's complement, the leftmost bit has a value, but unlike all the other bits, that value is negative. equivalent to adding −2147483648 (for an int). Changing the first bit in an int usually changes both sign and magnitude.Jhonson Fernando wrote:. . . Read this article for more info.
That leftmost bit determines the sign, but it does more than that so I don't think “sign bit” is an accurate name for it.jshell> {
...> int i = 123456;
...> System.out.printf("i = %d; i with first bit changed = %d%n", i, i ^ 0x8000_0000);
...> }
i = 123456; i with first bit changed = -2147360192
Clowns were never meant to be THAT big! We must destroy it with this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
|