Java, Groovy & Scala: side to side 1
This is the first part of the series. I shamelessly borrowed Sundar's layout and categorization. These are the language versions I'm testing the code with in case anyone is interested
· Java - Java5 (jdk1.6.0_06/jre1.6.0._10)
· Groovy - 1.6-beta-2-SNAPSHOT
· Scala - 2.7.1.final
Disclaimer: I know Groovy better than I know Scala, if someone spots a mistake please let me know
Feature
|
Java
|
Groovy
|
Scala
|
Type System
|
Static with few dynamic checks inserted as needed.
|
Dynamic, optional static types.
|
Static, with type inference.
|
Comments
|
/* * multiline comment */ /** * javadoc comment */ // single line comment
|
/* * multiline comment */ /** * groovydoc comment */ // single line comment
|
/* * multiline comment */ /** * scaladoc comment */ // single line comment
|
End of statement
|
;
|
a new line will do most in most cases. Use ; when ambiguous
|
a new line will do most in most cases. Use ; when ambiguous
|
Control Statements - if
|
if (condition) { // statements } if (condition) { // statements } else if (condition) { // statements } else { // statements }
|
if (condition) { // statements } if (condition) { // statements } else if (condition) { // statements } else { // statements }
|
if (condition) { // statements } if (condition) { // statements } else if (condition) { // statements } else { // statements }
|
Control Statements - ternary operator
|
(condition) ? true_statement : false_statement
|
(condition) ? true_statement : false_statement Elvis operator, a refinement over the ternary operator. If the condition expression is true then said condition expression will be the true_statement condition ?: false_statement
|
if (condition) true_statement else false_statement
|
Control Statements - while
|
while (condition) { // statements } do { // statements } while (condition);
|
while (condition) { // statements }
|
while (condition) { // statements } do { // statements } while (condition)
|
Control Statements - for
|
for (init; condition; increment ) { // statements } for (Type t: iterable ) { // statements }
|
for (init; condition; increment ) { // statements } for (Type t: iterable ) { // statements } for ( variable in iterable ) { // statements } Every single object in Groovy is iterable, default impl returns the object itself
|
for ( t <- list-value ) { // statements } list-value may also be an Array for ( s ) yield statement s may be one of
-
generator: val x <- e e is a list-value expression, binding x to successive values in the list
-
definition: val x = e introduces x as a name for the value of e in the rest of the comprehension
-
filter: a Boolean expression, omits all bindings (values?) for which the expression is false
|
Control Statements - switch
|
switch (target) { case constant_expr: // statements [break] ... default: // statements }
|
switch (target) { case constant_expr: // statements [break] ... default: // statements } * target may be any object * case statements may use same expressions as in Java plus Strings, Matchers, regular expression, closures, ranges, any object that supports isCase()
|
Not supported Scala has a similar feature as Groovy's any object as case (case classes and matchers, they deserve a more detailed explanation)
|
Control Statements - conditions
|
conditions must be evaluated in a boolean context
|
conditions may be evaluated in many contexts, this is known as the Groovy Truth, some rules follow (this is not a complete set, please refer to Groovy in Action or Groovy's site) evaluates to false
- false
- null references
- empty Map
- empty List
- Matcher with no matches
|
conditions must be evaluated in a Boolean context
|
String Literals
|
"Hello Java"
|
"Hello Groovy" // double quote 'Hello Groovy' // single quoute """Hello Groovy""" // multiline // variable interpolation name = "Groovy" "Hello ${name}" // evals to Hello Groovy
|
"Hello Scala" """Hello Scala""" // multiline @Daniel mentions Scala supports triple-quote too
|
Class declaration
|
class HelloWorld { // fields // methods }
|
class HelloWorld { // fields // methods }
|
class HelloWorld { // fields // methods } // defines a singleton object object HelloWorld { // fields // methods }
|
原文链接http://groovygrails.com/gg/blog/view/123147;jsessionid=11297C7AB6F87D53BBBC1CEFBCA6B0AB.vhost01
分享到:
相关推荐
Java、Groovy、Scala和Kotlin是四种在Java平台上广泛使用的编程语言,它们各自具有独特的特点和优势。这里我们将深入探讨这四种语言的核心特性,并对比它们在实际开发中的应用。 首先,Java作为最古老的成员,自...
Making Java Groovy is a practical handbook for developers who want to blend Groovy into their day to day work with Java It starts by introducing the key differences between Java and Groovy and how you...
AndroidDemoIn4Languages, 在Android开发中,比较 Java Groovy Scala Kotlin 中文版 日本語 AndroidDemoIn4Languages为了了解Android开发的更好语言,用 Java 。Groovy 。Scala 和Kotlin编写了一个简单的Android应用...
Groovy和Grails是两个密切相关的开源技术,主要用于构建现代、高效的Java平台应用程序。Groovy是一种动态、灵活的编程语言,它与Java高度兼容,但语法更为简洁,提供了更多的灵活性。而Grails则是一个基于Groovy的...
1. **方法1:直接调用** - 如果你的开发环境(如IntelliJ IDEA或Eclipse)已经安装了Groovy解释器插件,那么可以直接在Java代码中像调用Java类一样调用Groovy类。Java编译器会自动处理Groovy类的编译,并将其转换为...
Groovy 与 Ruby on Rails (ROR) 比较 1: Groovy 和 Grails 与 Ruby on Rails 一样,都追求开发效率和简洁性,但它们建立在不同的语言基础之上。Ruby on Rails 是基于 Ruby 语言,而 Grails 则是基于 Groovy。虽然 ...
标签:groovy、codehaus、jar包、java、中文文档; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明精准翻译,请放心使用...
1. 引入Groovy库:在Java项目中添加Groovy的相关依赖,通常是`groovy-all`,确保Java能够访问Groovy运行时环境。 2. 创建GroovyClassLoader:使用这个类加载器可以动态加载和执行Groovy脚本。它继承自Java的...
[Apress] Beginning Groovy and Grails From Novice to Professional (E-Book) ☆ 出版信息:☆ [作者信息] Christopher M. Judd, Joseph Faisal Nusairat, James Shingler [出版机构] Apress [出版日期] 2008年...
Groovy环境搭建教程中的例子工程,纯Java、纯Groovy以及Java+Groovy混合 教程参考:http://blog.csdn.net/rcom10002/archive/2011/06/26/6568557.aspx
Making Java Groovy Kenneth A. Kousen 1.Easier Java 2.Closures, builders, and metaprogramming 3.Gradle for builds, Spock for testing 4.Groovy frameworks like Grails and Griffon 源码
Making Java Groovy--Kenneth.A.Kousen. Java Groovy 很不错的学习资料
标签:codehaus、groovy、all、中英对照文档、jar包、java; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明精准翻译,...
在一次代码拉取中,出现了以下问题:Could not download groovy-all.jar (org.codehaus.groovy:groovy-all:2.4.15) 详细的报错信息如下: // 报错信息如下 Could not resolve all files for configuration ':jcore-...
### Java与Groovy:融合的力量 #### 知识点一:Java与Groovy的关系 - **定义**:Groovy是一种面向对象的编程语言,它直接运行在Java平台上,并且能够与现有的Java代码无缝集成。 - **特点**: - **语法简洁**:...
在Java开发中,Groovy是一种强大的、动态类型的脚本语言,它可以无缝地与Java代码集成,为开发者提供了更简洁、灵活的语法。本文将深入探讨在Java项目中使用Groovy的三种主要方式,并阐述它们各自的优势和应用场景。...
在Java编程中,`java.lang.ClassNotFoundException` 是一个常见的运行时异常,通常发生在尝试通过类加载器加载指定类时,但找不到对应的字节码文件。在这个特定的问题中,`ClassNotFoundException` 引发的原因是缺少...
SpringBoot、Gradle、Maven、Java和Groovy是Java生态系统中的重要组成部分,它们在现代软件开发中扮演着至关重要的角色。这篇详细的知识点解析将深入探讨这些技术及其相互关系。 1. **SpringBoot**: SpringBoot是...
1. **Groovy的简洁语法**:Groovy的语法比Java更加简洁,比如它允许省略括号、类型声明等,这使得代码更易读写。例如,方法调用可以不带括号,变量声明可以省略类型,如: ```groovy def sayHello() { println '...