Monday, July 5, 2010

OO Hello World - Scala



Update 1: Porting code examples to Scala 2.10.1 - support for String Interpolation.

The Hello World in Scala, the "scalable language" is here!

Scala is a very powerful multi-paradigm (OO and Functional) language. It targets the Java JVM and can easily interoperate with existing Java code. Its syntax is kind of Java-Like as well as Groovy and other languages.


By the way, you can see my previous post here: http://carlosqt.blogspot.com/2010/06/oo-hello-world.html
where I give some details on WHY these "OO Hello World series" samples.

Version 1 (Minimal):
The minimum you need to type to get your program compiled and running.
class Greet(name: String) {
    var _name: String = name.capitalize
    def salute() = println(s"Hello ${_name}!")
}

object Program {
    def main(args: Array[String]) = {
        val g = new Greet("world")
        g.salute
    }
}

Version 2 (Verbose):
Explicitly adding instructions and keywords that are optional to the compiler.
package com.series
import scala._  

class Greet(name: String) {
    private var _name: String = { name.capitalize }  
    def salute() = { println(s"Hello ${_name}!") }
}

object Program {
    def main(args: Array[String]): Unit = {
        val g = new Greet("world")                
        g.salute
    }
}

The Program Output:









Scala Info:
“Scala is a general purpose programming language designed to express common programming patterns in a concise, elegant, and type-safe way. It smoothly integrates features of object-oriented and functional languages, enabling Java and other programmers to be more productive. Code sizes are typically reduced by a factor of two to three when compared to an equivalent Java application.” Taken from: (http://www.scala-lang.org/node/25)

Appeared:
2003
Current Version:
Developed by:
Martin Odersky
Creator:
Martin Odersky
Influenced by:
Java (James Gosling)
Predecessor Language
Funnel?
Predecessor Appeared
1999
Predecessor Creator
Martin Odersky
Runtime Target:
JVM
Latest Framework Target:
JDK 6
Mono Target:
No
Allows Unmanaged Code:
No
Source Code Extension:
“.scala”
Keywords:
40
Case Sensitive:
Yes
Free Version Available:
Yes
Open Source:
Yes
Standard:
No
Latest IDE Support:
NetBeans 6.9
Eclipse
IntelliJ IDEA
Language Reference:
Extra Info:


1 comment:

  1. hello world. i here saw scale world. i see this really first time. it's really interesting for me. thanks for sharing this information.

    ReplyDelete