Good question, I would like to receive an authoritative answer.
, . , , Groovy ( ), , , (, ).
: , , , -, .
Groovy , .
if (condition) int foo=1
javac
: " ", , , , ? Groovy.
private
.
final
. final int
. 2.5, , . .
final
Groovy 2,4 2,5:
$ ./groovy-2.4.11/bin/groovy -e "final j=0; println(++j)"
1
$ ./groovy-2.5.0-alpha-1/bin/groovy -e "final j=0; println(++j)"
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
script_from_command_line: 1: The variable [j] is declared final but is reassigned
. At [1:20] @ line 1, column 20.
final j=0; println(++j)
^
1 error
, Groovy 2.5 - :
$ ./groovy-2.4.11/bin/groovy -e "println(++1)"
2
$ ./groovy-2.5.0-alpha-1/bin/groovy -e "println(++1)"
2
, ++1
?
, Groovy - Java, Java. -. ++
Groovy Java.
Groovy 9 (. org. codehaus.groovy.control.CompilePhase): , , , , , , , , .
GroovyConsole. :
def foo() {
++1
}
, - :
public java.lang.Object foo() {
++(1)
}
. . , ++(2+2)
5
. ... , ++
- , incrementThis(thing)
, , , , 1 .
IDE, ctrl-click ++
, org.codehaus.groovy.runtime.DefaultGroovyMethods.next(Number self)
:
public static Number next(Number self) {
return NumberNumberPlus.plus(self, ONE);
}
, , self + ONE
, , self
, , . , ++(x)
- DefaultGroovyMethods.next(x)
, .
, , , javac
groovyc
:
public class Wat {
public int foo() {
int i=1;
return ++i;
}
}
def foo() {
++1
}
(Java 1.8, Groovy 2.4, ):
// Java:
0: iconst_1 // load int value 1 onto stack
1: istore_1 // store int value into variable
2: iinc 1, 1 // increment local variable
5: iload_1 // load int value from local variable
6: ireturn // return that integer value
// Groovy:
43: iconst_1 // load int value 1 onto stack
44: iconst_1 // load another int value 1 onto stack
45: iadd // add those 2 ints (result stored on stack in place of first int, stack is popped by 1)
46: invokestatic
49: areturn // Return that new Integer result
Groovy ++1
Integer.valueOf(1+1)
( ).
def foo(i) { ++i }
? , i
, , Object
, -, DefaultGroovyMethods.next
NumberNumberPlus.plus
. , , , .