java2scala #2: Hello world
December 16, 2009 – 12:05 am by Stefano MAESTRIThe first java2scala samples we are going to discuss is generated by creating project from maven archetype.
Let me remember you that you need an environment with:
- Eclipse with eclipse-scala plugin
- maven wih maven-scala plugin
- git to clone the project from github. This is optional, you can of course download the code directly with download link on github page itself…or digit yourself
I’ve used m2eclipse plugin and created the two submodules using maven-archetype-quickstart (for java module) and scala-archetype-simple (for scala).
Let’s start with code samples (of course all code is available on our github project):
Java class
package it.javalinux.samples.java; /** * Hello world! * */ public class App { public static void main( String[] args ) { System.out.println( "Hello World!" ); } }
Java unit test
package it.javalinux.samples.java; import junit.framework.Test; import junit.framework.TestCase; import junit.framework.TestSuite; /** * Unit test for simple App. */ public class AppTest extends TestCase { /** * Create the test case * * @param testName name of the test case */ public AppTest( String testName ) { super( testName ); } /** * @return the suite of tests being tested */ public static Test suite() { return new TestSuite( AppTest.class ); } /** * Rigourous Test :-) */ public void testApp() { assertTrue( true ); } }
Scala class
package it.javalinux.samples.scala /** * Hello world! * */ object App extends Application { println( "Hello World!" ) }
Scala Unit Test
package it.javalinux.samples.scala import org.junit._ import Assert._ @Test class AppTest { @Test def testOK() = assertTrue(true) // @Test // def testKO() = assertTrue(false) }
Scala BDD specs
package it.javalinux.samples.scala import org.specs._ import org.specs.runner.{ConsoleRunner, JUnit4} class MySpecTest extends JUnit4(MySpec) //class MySpecSuite extends ScalaTestSuite(MySpec) object MySpecRunner extends ConsoleRunner(MySpec) object MySpec extends Specification { "This wonderful system" should { "save the world" in { val list = Nil list must beEmpty } } }
My Comments
It have been your first taste of scala. How do you feel with it?
Of course, since it is your first piece of scala code in this samples you have to note a lot of things. Don’t try to get all aspects right now, I promise I’ll explain you each of them in great detail in next future:
- Scala support two keyword class and object. The first one is the same than in scala, while the object is the keyword to define singleton object. Singleton object is the way scala define “static” methods in a pure object oriented style…don’t ask me to explain in this very first sample singleton object and/or companion object…we will explain their usefulness very sonn, just be a little patience and think it as classes with all static methods. Let me remark they are much more than this, but we will focus on them later in our series.
- Scala method definition syntax. The equals after a method name suggest that a method is function returning a value, while methods without equals symbol, or declaring Unit as return type, are procedure defined only for their side effects.
- Scala val keyword. Scala support two kind of variable definition. val keyword define an immutable variable (like final in java) while var define a mutable variable. Please keep in mind using of val is encouraged and very often used to develop with a functional style.
- Scala conciseness: The App class is more concise in scala, and AppTest is much more concise than Java one. Conciseness can hurt you at a very first stage, you can feel concise code less readable in some cases. But it is really an advantage: less code = less error, and a good code style can help you to keep your code readable even if concise.
- Scala doesn’t need main() method to make a class executable. You just need to extends Application trait. WTF is a trait? We will have a lot of time to explain it better with some more useful samples. For now just think a trait as an interface with some methods implementation.
- Underscore char ( _ ) is used in imports instead of star ( * ) as wild card. Moreover Scala support also imports of classes sets in same package using {firstClass,SecondClass} notation (have a look of that on MySpecTest).
- The scala maven artifact create also a BDD spec. It’s because BDD is supported and suggested by the scala community. I’m not going to introduce you to BDD specs here, at least not today. Just keep in mind BDD is quite used in scala community.
Ok, it has been just your first taste of Scala, I’ve already said too much and put too much things in our plate. In our next post we will take a look to some very basic language’s feature like how to define class, variables, and methods/procedures.
Of course all code is available on our github project
Next post is coming in few days (probably during next week end) and it will be focus on basic variables and methods definition.
Stay tuned.













One Response to “java2scala #2: Hello world”
Brilliant introduction! Keep doing!
By Alice on Dec 16, 2009