Jakiś pomysł, dlaczego muszę rzucić literał integer do (int) tutaj?

W poniższym przykładzie

int i = -128;
Integer i2 = (Integer) i; // compiles

Integer i3 = (Integer) -128; /*** Doesn't compile ***/

Integer i4 = (Integer) (int) -128; // compiles
Integer i4 = -128; // compiles
Integer i5 = (int) -128; // compiles
Integer i6 = (Integer) (-128); // compiles
Integer i7 = (Integer) 0-128; // compiles

Nie mogę rzucać -128 z (Integer), ale mogę rzucać (int) -128.

Zawsze uważałem, że -128 jest typu int i odlewanie go z (int) powinno być zbędne.

Błąd w linii z i3 to

cannot find symbol variable Integer
[11]}próbowałem tego z Java 6 update 29 i Java 7 update 1.

EDIT: otrzymujesz to samo zachowanie z +128 zamiast -128. Wydaje się, że jest to pomylenie między operatorami uniarnymi i binarnymi.

Author: Roman C, 2011-10-26

8 answers

Kompilator próbuje odjąć 128 z (Integer) zamiast przerzucać -128 na Integer. Add () to fix it

Integer i3 = (Integer) -128; // doesn't compile
Integer i3 = (Integer) (-128); // compiles

Według BoltClock w komentarzach Obsada do int działa zgodnie z przeznaczeniem, ponieważ jest to słowo zastrzeżone i dlatego nie może być interpretowane jako identyfikator, co ma dla mnie sens.

And Bringer128 found the JLS Reference 15.16.

 CastExpression:
    ( PrimitiveType Dimsopt ) UnaryExpression
    ( ReferenceType ) UnaryExpressionNotPlusMinus

Jak widać, odlewanie do prymitywnego typu wymaga dowolnego UnaryExpression, podczas gdy odlewanie do Typ odniesienia wymaga UnaryExpressionNotPlusMinus. Są one zdefiniowane tuż przed wyrażeniem CastExpression w JLS 15.15.

 151
Author: Jens Schauder,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-11-01 17:16:38

Znalazłem odniesienie do JLS. 15.16.

 CastExpression:
    ( PrimitiveType Dimsopt ) UnaryExpression
    ( ReferenceType ) UnaryExpressionNotPlusMinus

Jak widać, odlewanie do typu prymitywnego wymaga dowolnego UnaryExpression, podczas gdy odlewanie do typu referencyjnego wymaga UnaryExpressionNotPlusMinus. Są one zdefiniowane tuż przed wyrażeniem CastExpression w JLS 15.15.

Musisz albo zmienić obsadę na prymitywną:

... (int) -128;

Lub możesz zmienić wyrażenie na prawo od rzutu na wyrażenie nie-plus-minus jednoargumentowe:

... (Integer) (-128);  // Either
... (Integer) 0 - 128; // Or
 48
Author: Bringer128,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-10-26 08:54:45

Kompilator interpretuje - jako operator dwuargumentowy minus, tzn. próbuje odjąć 128 od innej liczby o nazwie Integer, ale nie ma takiej zmiennej w zakresie.

To zestawienie:

Integer i3 = (Integer) (-128)
 12
Author: Barend,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-10-26 08:19:43

Może to mieć związek z analizowaniem składni. Zauważ, że

Integer i4 = (Integer) (-128); 
Działa dobrze.

Ogólnie rzecz biorąc, nie należy rzucać do klasy Integer. Wiąże się to z czymś, co nazywa się auto-boks i może powodować pewne subtelne błędy w kodzie. Preferowaną metodą robienia tego, co chcesz jest:

Integer i6 = Integer.valueOf(-128)
 9
Author: Krystian Cybulski,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-10-26 08:20:41

Analizuje ją jako {[1] } i nie znajduje zmiennej Integer. Musisz zawinąć -128 w nawiasy:

Integer i3 = (Integer) (-128);  // compiles
 9
Author: Bohemian,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-10-26 08:21:10
Integer i3 = (Integer) (-128);

Problem polega na tym, że - kompilator widzi go jako operator.

 7
Author: Brian Roach,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-10-26 08:20:26

Linia 3 jest interpretowana tak, jak próbujesz odjąć 128 od wyrażenia w nawiasie, a wyrażenie w nawiasie nie jest i wyrażenie typu int (traktuje " - "jako operator" -"). Jeśli zmienisz wyrażenie na:

Integer i3 = (Integer) (-128);

Wtedy kompilator zrozumie, że ' - ' jest uniarnym minus, który wskazuje ujemną liczbę całkowitą.

 6
Author: Udi Cohen,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-10-26 08:23:47

Kompilator C # ma takie samo zachowanie. To daje lepszą podpowiedź dlaczego nie kompiluje chociaż:

Aby oddać wartość ujemną, musisz dołączyć wartość w nawiasach

 3
Author: JefClaes,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-05-19 07:27:33