Monday, October 29, 2012

Use Java In Scala

Scala integrates easily with Java.


Scala is a general purpose programming language that is created to transform standard programming patterns in a precise, polished and type-safe manner. Java, on the other hand, is a programming language that runs the latest programs including business applications, games and utilities. Scala and Java can work in the same environment because both languages run on Java Virtual Machine (JVM). Since Java and Scala are fully compatible and interoperable with each other, either program can be used as a substitute for the other depending on the project requirements of the coder. Scala is not a superset of Java, but it is rather a worthy alternative for every Web coder to enjoy.


Instructions


1. Download and install the current version of Scala for your environment. Scala is supported by various operating systems, such as Windows, Unix, Macintosh and Cygwin.


2. Type an expression to start using the Scala code interpreter, which is Scala's interactive "shell" for authoring expressions and programs. Here is an example:


$ scala


This is an interpreter for Scala.


Type in expressions to have them evaluated.


Type :help for more information.


scala>


When you type an expression and hit "Enter" or "Return," Scala interprets it like this: "scala> 1 + 2"


Then the interpreter will print: "unnamed0: Int = 3"


This line of code is comprised of an "automatically designated or user-defined name to refer to the calculated value (unnamed0)," "a colon (:)," "the type of the expression and its output (Int)," "an equals sign (=)" and "the value derived from evaluating the expression (3)"


3. Enter a val definition to see how Scala functions. Scala's method of differentiation is applied between vals, which are variables that are designated once and never change, and vars, which are variables that may be changed or modified over their lifetime. Here is a sample of a val definition:


scala> val msg = "Hello, world!"


msg: java.lang.String = Hello, world!


This val definition designates "msg" as a name for the value "Hello world!" If you are familiar with Java, you may notice that the type of value used here is java.lang.String. As mentioned earlier, Scala and Java are interoperable with each other, so in this instance, Scala strings are likewise Java strings. In reality, every Java class is also found in Scala.


4. Enter the ":quit command" if you are still running the Scala interpreter and save your code into a file named hello.scala. Here is how you do that:


println("Hello, world, from a script!")


Then run the code by typing: ">scala hello.scala"


Scala should respond to you with a greeting: "Hello, world, from a script!"


Noticeably, most of the codes used in this project are derived from the Java language, so this proves that Java is really well-integrated with Scala. Scala and Java will always work hand-in-hand and one can't be without the other.







Tags: Hello world, Java Scala, Scala Java, derived from, each other, expression Enter