概述
官网:
http://www.groovy-lang.org/differences.html
Groovy试图让Java开发人员尽可能自然。在设计Groovy时,我们尝试遵循最小惊喜的原则,特别是对于那些学习Groovy的开发人员来说,他们来自Java背景。
这里我们列出了Java和Groovy之间的所有主要区别。
1.默认导入
默认情况下导入所有这些包和类,即您不必使用显式import
语句来使用它们:
-
java.io. *
-
的java.lang。*
-
java.math.BigDecimal中
-
java.math.BigInteger中
-
java.net。*
-
java.util中。*
-
groovy.lang。*
-
groovy.util。*
2.多种方法
在Groovy中,将在运行时选择要调用的方法。这称为运行时调度或多方法。这意味着将根据运行时参数的类型选择方法。在Java中,这是相反的:在编译时根据声明的类型选择方法。
以下代码编写为Java代码,可以在Java和Groovy中编译,但它的行为会有所不同:
<span style="color:#343437"><span style="color:#333333"><code class="language-java"><span style="color:#000088">int</span><span style="color:#000000"> method</span><span style="color:#666600">(</span><span style="color:#660066">String</span><span style="color:#000000"> arg</span><span style="color:#666600">)</span> <span style="color:#666600">{</span>
<span style="color:#000088">return</span> <span style="color:#006666">1</span><span style="color:#666600">;</span>
<span style="color:#666600">}</span>
<span style="color:#000088">int</span><span style="color:#000000"> method</span><span style="color:#666600">(</span><span style="color:#660066">Object</span><span style="color:#000000"> arg</span><span style="color:#666600">)</span> <span style="color:#666600">{</span>
<span style="color:#000088">return</span> <span style="color:#006666">2</span><span style="color:#666600">;</span>
<span style="color:#666600">}</span>
<span style="color:#660066">Object</span><span style="color:#000000"> o </span><span style="color:#666600">=</span> <span style="color:#008800">"Object"</span><span style="color:#666600">;</span>
<span style="color:#000088">int</span><span style="color:#000000"> result </span><span style="color:#666600">=</span><span style="color:#000000"> method</span><span style="color:#666600">(</span><span style="color:#000000">o</span><span style="color:#666600">);</span></code></span></span>
在Java中,您将拥有:
<span style="color:#343437"><span style="color:#333333"><code class="language-java"><span style="color:#000000">assertEquals</span><span style="color:#666600">(</span><span style="color:#006666">2</span><span style="color:#666600">,</span><span style="color:#000000"> result</span><span style="color:#666600">);</span></code></span></span>
在Groovy中:
<span style="color:#343437"><span style="color:#333333"><code class="language-java"><span style="color:#000000">assertEquals</span><span style="color:#666600">(</span><span style="color:#006666">1</span><span style="color:#666600">,</span><span style="color:#000000"> result</span><span style="color:#666600">);</span></code></span></span>
这是因为Java将使用静态信息类型,该类型o
被声明为Object
,而Groovy将在运行时选择实际调用该方法。由于它是用a调用的String
,因此调用该 String
版本。
3.数组初始值设定项
在Groovy中,{ … }
块被保留用于闭包。这意味着您无法使用以下语法创建数组文字:
<span style="color:#343437"><span style="color:#333333"><code class="language-groovy"><span style="color:#000088">int</span><span style="color:#666600">[]</span><span style="color:#000000"> array </span><span style="color:#666600">=</span> <span style="color:#666600">{</span> <span style="color:#006666">1</span><span style="color:#666600">,</span> <span style="color:#006666">2</span><span style="color:#666600">,</span> <span style="color:#006666">3</span><span style="color:#666600">}</span></code></span></span>
你实际上必须使用:
<span style="color:#343437"><span style="color:#333333"><code class="language-groovy"><span style="color:#000088">int</span><span style="color:#666600">[]</span><span style="color:#000000"> array </span><span style="color:#666600">=</span> <span style="color:#666600">[</span><span style="color:#006666">1</span><span style="color:#666600">,</span><span style="color:#006666">2</span><span style="color:#666600">,</span><span style="color:#006666">3</span><span style="color:#666600">]</span></code></span></span>
4.包装范围可见性
在Groovy中,省略字段上的修饰符不会导致像Java一样的包私有字段:
<span style="color:#343437"><span style="color:#333333"><code class="language-groovy"><span style="color:#000088">class</span> <span style="color:#660066">Person</span> <span style="color:#666600">{</span>
<span style="color:#660066">String</span><span style="color:#000000"> name
</span><span style="color:#666600">}</span></code></span></span>
相反,它用于创建属性,即私有字段,关联的getter和关联的 setter。
可以通过以下方式对其进行批注来创建包私有字段@PackageScope
:
<span style="color:#343437"><span style="color:#333333"><code class="language-groovy"><span style="color:#000088">class</span> <span style="color:#660066">Person</span> <span style="color:#666600">{</span>
<span style="color:#006666">@PackageScope</span> <span style="color:#660066">String</span><span style="color:#000000"> name
</span><span style="color:#666600">}</span></code></span></span>
5. ARM块
Groovy不支持Java 7中的ARM(自动资源管理)块。相反,Groovy提供了依赖于闭包的各种方法,这些方法具有相同的效果,同时更具惯用性。例如:
<span style="color:#343437"><span style="color:#333333"><code class="language-java"><span style="color:#660066">Path</span><span style="color:#000000"> file </span><span style="color:#666600">=</span> <span style="color:#660066">Paths</span><span style="color:#666600">.</span><span style="color:#000000">get</span><span style="color:#666600">(</span><span style="color:#008800">"/path/to/file"</span><span style="color:#666600">);</span>
<span style="color:#660066">Charset</span><span style="color:#000000"> charset </span><span style="color:#666600">=</span> <span style="color:#660066">Charset</span><span style="color:#666600">.</span><span style="color:#000000">forName</span><span style="color:#666600">(</span><span style="color:#008800">"UTF-8"</span><span style="color:#666600">);</span>
<span style="color:#000088">try</span> <span style="color:#666600">(</span><span style="color:#660066">BufferedReader</span><span style="color:#000000"> reader </span><span style="color:#666600">=</span> <span style="color:#660066">Files</span><span style="color:#666600">.</span><span style="color:#000000">newBufferedReader</span><span style="color:#666600">(</span><span style="color:#000000">file</span><span style="color:#666600">,</span><span style="color:#000000"> charset</span><span style="color:#666600">))</span> <span style="color:#666600">{</span>
<span style="color:#660066">String</span><span style="color:#000000"> line</span><span style="color:#666600">;</span>
<span style="color:#000088">while</span> <span style="color:#666600">((</span><span style="color:#000000">line </span><span style="color:#666600">=</span><span style="color:#000000"> reader</span><span style="color:#666600">.</span><span style="color:#000000">readLine</span><span style="color:#666600">())</span> <span style="color:#666600">!=</span> <span style="color:#000088">null</span><span style="color:#666600">)</span> <span style="color:#666600">{</span>
<span style="color:#660066">System</span><span style="color:#666600">.</span><span style="color:#000000">out</span><span style="color:#666600">.</span><span style="color:#000000">println</span><span style="color:#666600">(</span><span style="color:#000000">line</span><span style="color:#666600">);</span>
<span style="color:#666600">}</span>
<span style="color:#666600">}</span> <span style="color:#000088">catch</span> <span style="color:#666600">(</span><span style="color:#660066">IOException</span><span style="color:#000000"> e</span><span style="color:#666600">)</span> <span style="color:#666600">{</span><span style="color:#000000">
e</span><span style="color:#666600">.</span><span style="color:#000000">printStackTrace</span><span style="color:#666600">();</span>
<span style="color:#666600">}</span></code></span></span>
可以像这样写:
<span style="color:#343437"><span style="color:#333333"><code class="language-groovy"><span style="color:#000088">new</span> <span style="color:#660066">File</span><span style="color:#666600">(</span><span style="color:#008800">'/path/to/file'</span><span style="color:#666600">).</span><span style="color:#000000">eachLine</span><span style="color:#666600">(</span><span style="color:#008800">'UTF-8'</span><span style="color:#666600">)</span> <span style="color:#666600">{</span><span style="color:#000000">
println it
</span><span style="color:#666600">}</span></code></span></span>
或者,如果你想要一个更接近Java的版本:
<span style="color:#343437"><span style="color:#333333"><code class="language-groovy"><span style="color:#000088">new</span> <span style="color:#660066">File</span><span style="color:#666600">(</span><span style="color:#008800">'/path/to/file'</span><span style="color:#666600">).</span><span style="color:#000000">withReader</span><span style="color:#666600">(</span><span style="color:#008800">'UTF-8'</span><span style="color:#666600">)</span> <span style="color:#666600">{</span><span style="color:#000000"> reader </span><span style="color:#666600">-></span><span style="color:#000000">
reader</span><span style="color:#666600">.</span><span style="color:#000000">eachLine </span><span style="color:#666600">{</span><span style="color:#000000">
println it
</span><span style="color:#666600">}</span>
<span style="color:#666600">}</span></code></span></span>
6.内部课程
匿名内部类和嵌套类的实现遵循Java主导,但是您不应该删除Java语言规范并且不断地讨论不同的事情。完成的实现看起来很像我们的工作groovy.lang.Closure ,有一些好处和一些差异。例如,访问私有字段和方法可能会成为一个问题,但另一方面,局部变量不一定是最终的。 |
6.1。静态内部类
这是静态内部类的一个例子:
<span style="color:#343437"><span style="color:#333333"><code class="language-groovy"><span style="color:#000088">class</span><span style="color:#000000"> A </span><span style="color:#666600">{</span>
<span style="color:#000088">static</span> <span style="color:#000088">class</span><span style="color:#000000"> B </span><span style="color:#666600">{}</span>
<span style="color:#666600">}</span>
<span style="color:#000088">new</span><span style="color:#000000"> A</span><span style="color:#666600">.</span><span style="color:#000000">B</span><span style="color:#666600">()</span></code></span></span>
静态内部类的使用是最受支持的。如果你绝对需要一个内部类,你应该把它变成一个静态类。
6.2。匿名内部类
<span style="color:#343437"><span style="color:#333333"><code class="language-groovy"><span style="color:#000088">import</span><span style="color:#000000"> java</span><span style="color:#666600">.</span><span style="color:#000000">util</span><span style="color:#666600">.</span><span style="color:#000000">concurrent</span><span style="color:#666600">.</span><span style="color:#660066">CountDownLatch</span>
<span style="color:#000088">import</span><span style="color:#000000"> java</span><span style="color:#666600">.</span><span style="color:#000000">util</span><span style="color:#666600">.</span><span style="color:#000000">concurrent</span><span style="color:#666600">.</span><span style="color:#660066">TimeUnit</span>
<span style="color:#660066">CountDownLatch</span><span style="color:#000000"> called </span><span style="color:#666600">=</span> <span style="color:#000088">new</span> <span style="color:#660066">CountDownLatch</span><span style="color:#666600">(</span><span style="color:#006666">1</span><span style="color:#666600">)</span>
<span style="color:#660066">Timer</span><span style="color:#000000"> timer </span><span style="color:#666600">=</span> <span style="color:#000088">new</span> <span style="color:#660066">Timer</span><span style="color:#666600">()</span><span style="color:#000000">
timer</span><span style="color:#666600">.</span><span style="color:#000000">schedule</span><span style="color:#666600">(</span><span style="color:#000088">new</span> <span style="color:#660066">TimerTask</span><span style="color:#666600">()</span> <span style="color:#666600">{</span>
<span style="color:#000088">void</span><span style="color:#000000"> run</span><span style="color:#666600">()</span> <span style="color:#666600">{</span><span style="color:#000000">
called</span><span style="color:#666600">.</span><span style="color:#000000">countDown</span><span style="color:#666600">()</span>
<span style="color:#666600">}</span>
<span style="color:#666600">},</span> <span style="color:#006666">0</span><span style="color:#666600">)</span>
<span style="color:#000088">assert</span><span style="color:#000000"> called</span><span style="color:#666600">.</span><span style="color:#000000">await</span><span style="color:#666600">(</span><span style="color:#006666">10</span><span style="color:#666600">,</span> <span style="color:#660066">TimeUnit</span><span style="color:#666600">.</span><span style="color:#000000">SECONDS</span><span style="color:#666600">)</span></code></span></span>
6.3。创建非静态内部类的实例
在Java中,您可以这样做:
<span style="color:#343437"><span style="color:#333333"><code class="language-java"><span style="color:#000088">public</span> <span style="color:#000088">class</span><span style="color:#000000"> Y </span><span style="color:#666600">{</span>
<span style="color:#000088">public</span> <span style="color:#000088">class</span><span style="color:#000000"> X </span><span style="color:#666600">{}</span>
<span style="color:#000088">public</span><span style="color:#000000"> X foo</span><span style="color:#666600">()</span> <span style="color:#666600">{</span>
<span style="color:#000088">return</span> <span style="color:#000088">new</span><span style="color:#000000"> X</span><span style="color:#666600">();</span>
<span style="color:#666600">}</span>
<span style="color:#000088">public</span> <span style="color:#000088">static</span><span style="color:#000000"> X createX</span><span style="color:#666600">(</span><span style="color:#000000">Y y</span><span style="color:#666600">)</span> <span style="color:#666600">{</span>
<span style="color:#000088">return</span><span style="color:#000000"> y</span><span style="color:#666600">.</span><span style="color:#000088">new</span><span style="color:#000000"> X</span><span style="color:#666600">();</span>
<span style="color:#666600">}</span>
<span style="color:#666600">}</span></code></span></span>
Groovy不支持y.new X()
语法。相反,你必须写new X(y)
,如下面的代码:
<span style="color:#343437"><span style="color:#333333"><code class="language-groovy"><span style="color:#000088">public</span> <span style="color:#000088">class</span><span style="color:#000000"> Y </span><span style="color:#666600">{</span>
<span style="color:#000088">public</span> <span style="color:#000088">class</span><span style="color:#000000"> X </span><span style="color:#666600">{}</span>
<span style="color:#000088">public</span><span style="color:#000000"> X foo</span><span style="color:#666600">()</span> <span style="color:#666600">{</span>
<span style="color:#000088">return</span> <span style="color:#000088">new</span><span style="color:#000000"> X</span><span style="color:#666600">()</span>
<span style="color:#666600">}</span>
<span style="color:#000088">public</span> <span style="color:#000088">static</span><span style="color:#000000"> X createX</span><span style="color:#666600">(</span><span style="color:#000000">Y y</span><span style="color:#666600">)</span> <span style="color:#666600">{</span>
<span style="color:#000088">return</span> <span style="color:#000088">new</span><span style="color:#000000"> X</span><span style="color:#666600">(</span><span style="color:#000000">y</span><span style="color:#666600">)</span>
<span style="color:#666600">}</span>
<span style="color:#666600">}</span></code></span></span>
但是请注意,Groovy支持使用一个参数调用方法而不提供参数。然后该参数的值为null。基本上相同的规则适用于调用构造函数。例如,您可能会编写新的X()而不是新的X(this)。由于这也可能是常规方式,我们尚未找到防止此问题的好方法。 |
兰布达斯
Java 8支持lambda和方法引用:
<span style="color:#343437"><span style="color:#333333"><code class="language-groovy"><span style="color:#660066">Runnable</span><span style="color:#000000"> run </span><span style="color:#666600">=</span> <span style="color:#666600">()</span> <span style="color:#666600">-></span> <span style="color:#660066">System</span><span style="color:#666600">.</span><span style="color:#000088">out</span><span style="color:#666600">.</span><span style="color:#000000">println</span><span style="color:#666600">(</span><span style="color:#008800">"Run"</span><span style="color:#666600">);</span><span style="color:#000000">
list</span><span style="color:#666600">.</span><span style="color:#000000">forEach</span><span style="color:#666600">(</span><span style="color:#660066">System</span><span style="color:#666600">.</span><span style="color:#000088">out</span><span style="color:#666600">::</span><span style="color:#000000">println</span><span style="color:#666600">);</span></code></span></span>
Java 8 lambdas可以或多或少地被视为匿名内部类。Groovy不支持该语法,但有闭包:
<span style="color:#343437"><span style="color:#333333"><code class="language-groovy"><span style="color:#660066">Runnable</span><span style="color:#000000"> run </span><span style="color:#666600">=</span> <span style="color:#666600">{</span><span style="color:#000000"> println </span><span style="color:#008800">'run'</span> <span style="color:#666600">}</span><span style="color:#000000">
list</span><span style="color:#666600">.</span><span style="color:#000000">each </span><span style="color:#666600">{</span><span style="color:#000000"> println it </span><span style="color:#666600">}</span> <span style="color:#880000">// or list.each(this.&println)</span></code></span></span>
8. GStrings
由于双引号字符串文字被解释为GString
值,如果String
使用Groovy和Java编译器编译包含美元字符的文字的类,Groovy可能会因编译错误而失败或产生微妙的不同代码。
虽然通常情况下,Groovy会在两者之间自动转换GString
,String
如果API声明了参数的类型,请注意接受Object
参数的Java API,然后检查实际类型。
9.字符串和字符文字
Groovy中单引号的文字用于String
,双引号结果 String
或GString
取决于文字中是否有插值。
<span style="color:#343437"><span style="color:#333333"><code class="language-groovy"><span style="color:#000088">assert</span> <span style="color:#008800">'c'</span><span style="color:#666600">.</span><span style="color:#000000">getClass</span><span style="color:#666600">()==</span><span style="color:#660066">String</span>
<span style="color:#000088">assert</span> <span style="color:#008800">"c"</span><span style="color:#666600">.</span><span style="color:#000000">getClass</span><span style="color:#666600">()==</span><span style="color:#660066">String</span>
<span style="color:#000088">assert</span> <span style="color:#008800">"c${1}"</span><span style="color:#666600">.</span><span style="color:#000000">getClass</span><span style="color:#666600">()</span> <span style="color:#000088">in</span> <span style="color:#660066">GString</span></code></span></span>
常规将自动投单字符String
到char
指定给类型的变量时只char
。当使用类型参数调用方法时,char
我们需要显式转换或确保已提前转换值。
<span style="color:#343437"><span style="color:#333333"><code class="language-groovy"><span style="color:#000088">char</span><span style="color:#000000"> a</span><span style="color:#666600">=</span><span style="color:#008800">'a'</span>
<span style="color:#000088">assert</span> <span style="color:#660066">Character</span><span style="color:#666600">.</span><span style="color:#000000">digit</span><span style="color:#666600">(</span><span style="color:#000000">a</span><span style="color:#666600">,</span> <span style="color:#006666">16</span><span style="color:#666600">)==</span><span style="color:#006666">10</span> <span style="color:#666600">:</span> <span style="color:#008800">'But Groovy does boxing'</span>
<span style="color:#000088">assert</span> <span style="color:#660066">Character</span><span style="color:#666600">.</span><span style="color:#000000">digit</span><span style="color:#666600">((</span><span style="color:#000088">char</span><span style="color:#666600">)</span> <span style="color:#008800">'a'</span><span style="color:#666600">,</span> <span style="color:#006666">16</span><span style="color:#666600">)==</span><span style="color:#006666">10</span>
<span style="color:#000088">try</span> <span style="color:#666600">{</span>
<span style="color:#000088">assert</span> <span style="color:#660066">Character</span><span style="color:#666600">.</span><span style="color:#000000">digit</span><span style="color:#666600">(</span><span style="color:#008800">'a'</span><span style="color:#666600">,</span> <span style="color:#006666">16</span><span style="color:#666600">)==</span><span style="color:#006666">10</span>
<span style="color:#000088">assert</span> <span style="color:#000088">false</span><span style="color:#666600">:</span> <span style="color:#008800">'Need explicit cast'</span>
<span style="color:#666600">}</span> <span style="color:#000088">catch</span><span style="color:#666600">(</span><span style="color:#660066">MissingMethodException</span><span style="color:#000000"> e</span><span style="color:#666600">)</span> <span style="color:#666600">{</span>
<span style="color:#666600">}</span></code></span></span>
Groovy支持两种类型的转换,在转换的情况下,char
在转换多字符串时会有细微差别。Groovy样式转换更宽松,将采用第一个字符,而C样式转换将失败,但异常。
<span style="color:#343437"><span style="color:#333333"><code class="language-groovy"><span style="color:#880000">// for single char strings, both are the same</span>
<span style="color:#000088">assert</span> <span style="color:#666600">((</span><span style="color:#000088">char</span><span style="color:#666600">)</span> <span style="color:#008800">"c"</span><span style="color:#666600">).</span><span style="color:#000088">class</span><span style="color:#666600">==</span><span style="color:#660066">Character</span>
<span style="color:#000088">assert</span> <span style="color:#666600">(</span><span style="color:#008800">"c"</span> <span style="color:#000088">as</span> <span style="color:#000088">char</span><span style="color:#666600">).</span><span style="color:#000088">class</span><span style="color:#666600">==</span><span style="color:#660066">Character</span>
<span style="color:#880000">// for multi char strings they are not</span>
<span style="color:#000088">try</span> <span style="color:#666600">{</span>
<span style="color:#666600">((</span><span style="color:#000088">char</span><span style="color:#666600">)</span> <span style="color:#008800">'cx'</span><span style="color:#666600">)</span> <span style="color:#666600">==</span> <span style="color:#008800">'c'</span>
<span style="color:#000088">assert</span> <span style="color:#000088">false</span><span style="color:#666600">:</span> <span style="color:#008800">'will fail - not castable'</span>
<span style="color:#666600">}</span> <span style="color:#000088">catch</span><span style="color:#666600">(</span><span style="color:#660066">GroovyCastException</span><span style="color:#000000"> e</span><span style="color:#666600">)</span> <span style="color:#666600">{</span>
<span style="color:#666600">}</span>
<span style="color:#000088">assert</span> <span style="color:#666600">(</span><span style="color:#008800">'cx'</span> <span style="color:#000088">as</span> <span style="color:#000088">char</span><span style="color:#666600">)</span> <span style="color:#666600">==</span> <span style="color:#008800">'c'</span>
<span style="color:#000088">assert</span> <span style="color:#008800">'cx'</span><span style="color:#666600">.</span><span style="color:#000000">asType</span><span style="color:#666600">(</span><span style="color:#000088">char</span><span style="color:#666600">)</span> <span style="color:#666600">==</span> <span style="color:#008800">'c'</span></code></span></span>
10.原始和包装
由于Groovy使用对象的一切,它autowraps到原语的引用。因此,它不遵循Java的扩展行为优先于拳击。这是一个使用的例子int
<span style="color:#343437"><span style="color:#333333"><code class="language-groovy"><span style="color:#000088">int</span><span style="color:#000000"> i
m</span><span style="color:#666600">(</span><span style="color:#000000">i</span><span style="color:#666600">)</span>
<span style="color:#000088">void</span><span style="color:#000000"> m</span><span style="color:#666600">(</span><span style="color:#000088">long</span><span style="color:#000000"> l</span><span style="color:#666600">)</span> <span style="color:#666600">{</span> <span style="color:#000000">
println </span><span style="color:#008800">"in m(long)"</span>
<span style="color:#666600">}</span>
<span style="color:#000088">void</span><span style="color:#000000"> m</span><span style="color:#666600">(</span><span style="color:#660066">Integer</span><span style="color:#000000"> i</span><span style="color:#666600">)</span> <span style="color:#666600">{</span> <span style="color:#000000">
println </span><span style="color:#008800">"in m(Integer)"</span>
<span style="color:#666600">}</span></code></span></span>
这是Java可以调用的方法,因为扩展优先于取消装箱。 | |
这是Groovy实际调用的方法,因为所有原始引用都使用它们的包装类。 |
11.行为 ==
在Java中==
意味着对象的基本类型或标识的相等性。在Groovy ==
中a.compareTo(b)==0
,如果是Comparable
, 则转换为a.equals(b)
。为了检查身份,有is
。例如 a.is(b)
。
12.转换
Java会自动扩展和缩小 转换次数。
转换为 | ||||||||
转换自 | 布尔 | 字节 | 短 | 烧焦 | INT | 长 | 浮动 | 双 |
布尔 | - | ñ | ñ | ñ | ñ | ñ | ñ | ñ |
字节 | ñ | - | ÿ | C | ÿ | ÿ | ÿ | ÿ |
短 | ñ | C | - | C | ÿ | ÿ | ÿ | ÿ |
烧焦 | ñ | C | C | - | ÿ | ÿ | ÿ | ÿ |
INT | ñ | C | C | C | - | ÿ | Ť | ÿ |
长 | ñ | C | C | C | C | - | Ť | Ť |
浮动 | ñ | C | C | C | C | C | - | ÿ |
双 | ñ | C | C | C | C | C | C | - |
* 'Y'表示Java可以进行的转换,'C'表示Java可以在存在显式转换时进行的转换,'T`表示Java可以进行的转换但是数据被截断,'N'表示Java可以转换'做。
Groovy对此有很大的扩展。
转换为 | ||||||||||||||||||
转换自 | 布尔 | 布尔 | 字节 | 字节 | 短 | 短 | 烧焦 | 字符 | INT | 整数 | 长 | 长 | 的BigInteger | 浮动 | 浮动 | 双 | 双 | BigDecimal的 |
布尔 | - | 乙 | ñ | ñ | ñ | ñ | ñ | ñ | ñ | ñ | ñ | ñ | ñ | ñ | ñ | ñ | ñ | ñ |
布尔 | 乙 | - | ñ | ñ | ñ | ñ | ñ | ñ | ñ | ñ | ñ | ñ | ñ | ñ | ñ | ñ | ñ | ñ |
字节 | Ť | Ť | - | 乙 | ÿ | ÿ | ÿ | d | ÿ | ÿ | ÿ | ÿ | ÿ | ÿ | ÿ | ÿ | ÿ | ÿ |
字节 | Ť | Ť | 乙 | - | ÿ | ÿ | ÿ | d | ÿ | ÿ | ÿ | ÿ | ÿ | ÿ | ÿ | ÿ | ÿ | ÿ |
短 | Ť | Ť | d | d | - | 乙 | ÿ | d | ÿ | ÿ | ÿ | ÿ | ÿ | ÿ | ÿ | ÿ | ÿ | ÿ |
短 | Ť | Ť | d | Ť | 乙 | - | ÿ | d | ÿ | ÿ | ÿ | ÿ | ÿ | ÿ | ÿ | ÿ | ÿ | ÿ |
烧焦 | Ť | Ť | ÿ | d | ÿ | d | - | d | ÿ | d | ÿ | d | d | ÿ | d | ÿ | d | d |
字符 | Ť | Ť | d | d | d | d | d | - | d | d | d | d | d | d | d | d | d | d |
INT | Ť | Ť | d | d | d | d | ÿ | d | - | 乙 | ÿ | ÿ | ÿ | ÿ | ÿ | ÿ | ÿ | ÿ |
整数 | Ť | Ť | d | d | d | d | ÿ | d | 乙 | - | ÿ | ÿ | ÿ | ÿ | ÿ | ÿ | ÿ | ÿ |
长 | Ť | Ť | d | d | d | d | ÿ | d | d | d | - | 乙 | ÿ | Ť | Ť | Ť | Ť | ÿ |
长 | Ť | Ť | d | d | d | Ť | ÿ | d | d | Ť | 乙 | - | ÿ | Ť | Ť | Ť | Ť | ÿ |
的BigInteger | Ť | Ť | d | d | d | d | d | d | d | d | d | d | - | d | d | d | d | Ť |
浮动 | Ť | Ť | d | d | d | d | Ť | d | d | d | d | d | d | - | 乙 | ÿ | ÿ | ÿ |
浮动 | Ť | Ť | d | Ť | d | Ť | Ť | d | d | Ť | d | Ť | d | 乙 | - | ÿ | ÿ | ÿ |
双 | Ť | Ť | d | d | d | d | Ť | d | d | d | d | d | d | d | d | - | 乙 | ÿ |
双 | Ť | Ť | d | Ť | d | Ť | Ť | d | d | Ť | d | Ť | d | d | Ť | 乙 | - | ÿ |
BigDecimal的 | Ť | Ť | d | d | d | d | d | d | d | d | d | d | d | Ť | d | Ť | d | - |
* 'Y'表示Groovy可以进行的转换,'D'表示Groovy在动态编译或显式转换时可以进行的转换,'T`表示Groovy可以进行转换,但数据被截断,'B'表示装箱/拆箱操作,'N'表示Groovy无法进行的转换。
转换为/ 时,截断使用Groovy Truth。从数字转换为字符会转换为。Groovy的构建体和 使用从一个转换时或者,否则的构造方法使用。其他转换的行为由其定义。boolean
Boolean
Number.intvalue()
char
BigInteger
BigDecimal
Number.doubleValue()
Float
Double
toString()
java.lang.Number
13.额外的关键字
Groovy中有一些关键字比Java更多。不要将它们用于变量名称等。
-
as
-
def
-
in
-
trait
最后
以上就是害羞缘分为你收集整理的Groovy与Java的差异的全部内容,希望文章能够帮你解决Groovy与Java的差异所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复