Declaring Types in Groovy

前端 未结 5 1707
天命终不由人
天命终不由人 2020-12-19 07:31

When you don\'t declare a type for a variable in groovy, it is my understanding that the java virtual machine has to use reflection in order to figure out what type the obje

5条回答
  •  没有蜡笔的小新
    2020-12-19 08:11

    Hi it was a nice question but don't know whether that the JVM uses reflection to found these types. The thing you have to be through when you code groovy is that both Java and Groovy have differs only in the source code. When they have been run they have been bound to java object model. Regardless of whether you write Groovy classes or scripts, they run as Java classes inside the JVM. So regardless what you do, all these are converted to Java and run in JVM :)

    Although the Java runtime understands compiled Groovy classes without any problem, it doesn’t understand .groovy source files. More work has to happen behind the scenes if you want to load .groovy files dynamically at runtime. Make sure that Groovy syntax is line-oriented, but the execution is not. That is Groovy coed is not processed Line By Line(shocked!,ya have to, some trick is going on) Instead Groovy is fully parsed, and a class is been generated. This generated class act as a bridge between Java And Groovy.Groovy classes are generated such that their format is identical to Java bytecode. So as i mentioned before, since Groovy produce the same bytecode, and it runs in Jvm, the class loader can handle and this bytecode. If it sounds harsh, don't worry, all the things are done by Groovy by it, for us.

    I recommend you to read Groovy in action book. Click here

提交回复
热议问题