`
ihuashao
  • 浏览: 4664905 次
  • 性别: Icon_minigender_1
  • 来自: 济南
社区版块
存档分类
最新评论

Java, Groovy & Scala: side to side 2

阅读更多
Java, Groovy & Scala: side to side 2
Posted By: Andres Almiray on Thu. Jun. 12, 2008

Second part of the series, (first part here). Thanks a lot to Daniel Spiewak who took the time to expand the topics presented about Scala. Without further ado...
Feature
Java
Groovy
Scala
Instance Fields
[access_modifier] Type name where access_modifier is any of
  • public
  • protected
  • private
  • empty - means package protected [default access]
[access_modifier] Type name where access_modifier is any of
  • public [default access]
  • protected
  • private
If no access modifier is provided then the field would be promoted to a property. Properties have their get/set methods auto generated in bytecode which means that the following Groovy class class Person { String name } Is equivalent to the following Java class public class Person { private String name; publicvoid setName( String name ) { this.name = name; } public String getName() { return name; } }
[access_modifier] [definition] name: Type where access_modifier is any of
  • empty - [default access]
  • protected
  • private
where definition is any of
  • var - mutable
  • val - immutable (think final in Java)
private works as in Java protected baffles me, as a subclass can't access its parent's protected fields (perhaps I missed something here), same thing with empty access modifier (strong encapsulation?)
Class Fields (static)
[access_modifier] static Type name where access_modifier is any of
  • public
  • protected
  • private
  • empty - means package protected [default access]
[access_modifier] static Type name where access_modifier is any of
  • public
  • protected
  • private
No static modifier (?) but object can be used object Foo { val aConstant = "Foo" privatevar seed = 42 def foo() = { seed += 1; seed - 1 } } object Bar { import Foo._ def main(args: Array[String]) { var bar = foo() println( aConstant ) // Foo println( bar ) // 42 println( foo() ) // 43 } }
Global Variables
Not supported. Every field/constant must belong to a class
Supported only in scripts, otherwise follows Java rules
Not supported. Every field/constant must belong to a class
Method definition
class Person { [access_modifier] Type name() { // statements } } where access_modifier is any of
  • public
  • protected
  • private
  • empty - means package protected [default access]
class Person { [access_modifier] Type name() { // statements } } where access_modifier is any of
  • public
  • protected
  • private
  • empty - same as public [default access]
class Person { [access_modifier] def name() [:Type] = { // statements } } where access_modifier is any of
  • protected
  • private
  • empty - public? [default access]
The type of the method may be optional No-arg methods can be written and called without parens class Person { def name = { "Scala" } } ... // this is a method call new Person().name
Static Method Definition
class Person { [access_modifier] static Type name() { // statements } } where access_modifier is any of
  • public
  • protected
  • private
  • empty - same as public [default access]
class Person { [access_modifier] static Type name() { // statements } } where access_modifier is any of
  • public
  • protected
  • private
  • empty - same as public [default access]
Follows the rules of Class Fields and Method Definition
Returning from a method
return expression; return;
Same as Java. But can leave the return statement - in that case the last expression evaluated is returned.
Same as Java. But can leave the return statement - in that case the last expression evaluated is returned.
Null
null
null - Guillaume Laforge notes that "Null Object Pattern" is supported in Groovy. See also: NullObject. So, you can call null.toString() for example.
null Which is actually of type Null, the only one of its kind. Can't call methods on it.
Arrays
int[] a = new int[10]; a[0] = 3;
int[] a = new int[10] a[0] = 3
var a:Array[Int] = new Array(10) a(0) = 3 also var a = new Array[Int](10) a(0) = 3
Array Literals
int[] a = {0,1,2}; a[0] = 3;
int[] a = [0,1,2] a[0] = 3
val nums = Array(0,1,2)
Lists
Supported by the Collections framefork (JSL), not really part of the language
List list = [0,1,2] list[0] = 'Foo' list[10] = 11 Lists can be heterogeneous Lists grow as needed
val nums = List(1,2,3,4) Lists are immutable, values can't be reassigned List are homogeneous
Hash Literals
Not supported. (See java.util.Map)
def hash = [key:'value', 'id': 1] hash.key = 'value2' // bean like access hash[key] = 'value3' // hash like access
val nums = Map("one" -> 1, "two" -> 2, "three" -> 3) nums("one") // 1 nums("two") // 2 Daniel explains: Scala does have a syntax for map literals (sort of), but it's not really built into the language. It's actually an implicit conversion on Any (common superclass of all objects incl. primitives) and a symbolic method.

http://groovygrails.com/gg/blog/view/123176;jsessionid=11297C7AB6F87D53BBBC1CEFBCA6B0AB.vhost01

分享到:
评论

相关推荐

    JGSK, Java,Groovy,Scala,Kotlin 四种语言的特点对比.zip

    Java、Groovy、Scala和Kotlin是四种在Java平台上广泛使用的编程语言,它们各自具有独特的特点和优势。这里我们将深入探讨这四种语言的核心特性,并对比它们在实际开发中的应用。 首先,Java作为最古老的成员,自...

    groovy-3.0.9-API文档-中文版.zip

    标签:groovy、codehaus、jar包、java、中文文档; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明精准翻译,请放心使用...

    [Groovy] Making Java Groovy 英文版

    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...

    groovy-all-2.4.5-API文档-中英对照版.zip

    标签:codehaus、groovy、all、中英对照文档、jar包、java; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明精准翻译,...

    Flutter 出现Could not download groovy-all.jar (org.codehaus.groovy:groovy-all:2.4.15)

    在一次代码拉取中,出现了以下问题:Could not download groovy-all.jar (org.codehaus.groovy:groovy-all:2.4.15) 详细的报错信息如下: // 报错信息如下 Could not resolve all files for configuration ':jcore-...

    AndroidDemoIn4Languages, 在Android开发中,比较 Java Groovy Scala Kotlin.zip

    AndroidDemoIn4Languages, 在Android开发中,比较 Java Groovy Scala Kotlin 中文版 日本語 AndroidDemoIn4Languages为了了解Android开发的更好语言,用 Java 。Groovy 。Scala 和Kotlin编写了一个简单的Android应用...

    Java调用Groovy,实时动态加载数据库groovy脚本

    2. 创建GroovyClassLoader:使用这个类加载器可以动态加载和执行Groovy脚本。它继承自Java的ClassLoader,能解析Groovy源码并生成字节码。 3. 加载并执行Groovy脚本:通过GroovyClassLoader的`parseClass()`方法...

    Groovy&Grails准备,收集的Groovy与Grails的书籍

    2. **简洁语法**:Groovy的语法比Java更简洁,例如,可以省略分号和大括号,使得代码更具可读性。 3. **Groovy Shell和 Grape**:Groovy Shell允许交互式地执行Groovy代码,Grape是Groovy的依赖管理系统,用于自动...

    Groovy&Grails - 指南.ppt

    例如,Java 中的匿名内部类在 Groovy 中可以用简洁的闭包语法代替。 Groovy 与 Ruby on Rails (ROR) 比较 1: Groovy 和 Grails 与 Ruby on Rails 一样,都追求开发效率和简洁性,但它们建立在不同的语言基础之上。...

    [Groovy] Groovy & Grails 新手进阶教程 (英文版)

    [Apress] Beginning Groovy and Grails From Novice to Professional (E-Book) ☆ 出版信息:☆ [作者信息] Christopher M. Judd, Joseph Faisal Nusairat, James Shingler [出版机构] Apress [出版日期] 2008年...

    groovy和Java相互调用1

    2. **方法2:反射动态调用** - 使用Java的反射机制,可以在运行时动态加载和执行Groovy类。这种方法的优点是Groovy脚本的修改不需要重新编译整个项目,因为Java代码可以通过反射动态地找到并调用Groovy方法。这里,...

    Making Java Groovy源码

    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 源码

    groovy-all-2.4.15.jar.zip

    groovy-all-2.4.15.jar文件,MAC使用时需存放在/Users/用户名/.gradle/caches/jars-3/某一缓存目录下,找不到就都看一下,我遇到的问题是缓存目录中下载的是2.4.17版本,应该跟gradle版本升级有关

    Making Java Groovy--Kenneth.A.Kousen

    Making Java Groovy--Kenneth.A.Kousen. Java Groovy 很不错的学习资料

    groovy-all-2.4.5-API文档-中文版.zip

    标签:codehaus、groovy、all、中文文档、jar包、java; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明精准翻译,请...

    SpringBoot-Gradle-Maven-Java-Groovy

    2. **Gradle**: Gradle是一种强大的构建自动化工具,适用于Java、Groovy和Kotlin等语言。它基于Groovy编写构建脚本,提供了灵活的构建配置和依赖管理。相比Maven,Gradle支持更复杂的项目结构,允许自定义构建逻辑,...

    Java Groovy

    #### 知识点一:Java与Groovy的关系 - **定义**:Groovy是一种面向对象的编程语言,它直接运行在Java平台上,并且能够与现有的Java代码无缝集成。 - **特点**: - **语法简洁**:Groovy提供了更为简洁、动态的语法...

    groovy-all-2.4.8.jar

    Groovy是一种动态、开源的编程语言,它是Java平台上的一个重要的补充。Groovy All-2.4.8.jar 是一个包含Groovy库的集合包,主要用于简化Groovy环境的搭建和使用。这个版本(2.4.8)是Groovy在2.x系列中的一个稳定...

    通过与Java的比较,迅速掌握Groovy

    - **Java**:Java中必须明确指定变量的数据类型,例如: ```java String name = "山风小子"; ``` **2. 对等方法的差异** - **等价比较**: - **Groovy**:使用`==`进行值的比较。 - **Java**:使用`equals...

Global site tag (gtag.js) - Google Analytics