score:13

Accepted answer

you have different ways of running scala code:

first create a program with your classes, this is as in java, i use object because it works well without instantianing, like static, just compile with the sbt and run it you can also use the scala interpreter repl

we can use this object in the repl

scala>  object hello {
     |   def main(args:array[string]) {
     |     println("hello, scala !!")
     |   }
     | }
defined object hello

scala> hello.main(array("onlyforwork"))
hello, scala !!

compiling and running it using activator/sbt

> compile
[info] compiling 1 scala source to /home/anquegi/dev/stackoverflow/scalastack/target/scala-2.11/classes...
[success] total time: 2 s, completed 13/04/2015 11:29:42
> run
[warn] multiple main classes detected.  run 'show discoveredmainclasses' to see the list

multiple main classes detected, select one to run:

 [1] org.example.hello
 [2] org.example.scheduledtaskscala
 [3] question1.ques
 [4] scriptworksheet.hello

enter number: 4

[info] running scriptworksheet.hello 
hello, scala !!
[success] total time: 19 s, completed 13/04/2015 11:30:04

the second is that if we add the scala code as a script or file hello.scala, you can save your scala code in the file with .scala extension (basically with any file extension but prefered .scala extension) and to run, provide file name with extension as parameter to scala interpreter

/**
 * created by anquegi on 13/04/15.
 */

println("hello, scala !!")

if we call the scala interpreter this file is executed, you do not need to instanciate objects or clases, just executing like a shell script, you can also execute directlyy from intellij, but i use the console with scala installed on the system

[anquegi@localhost scalastack]$ scala src/main/scala/scriptworksheet/helloscript.scala 
hello, scala !!

and finally the worksheet is the most powerfull, i recommend this for increasing your prodductivity at work bacause it is easy to test things is like the repl, ant it evluates the scala exprssions and shows you back the result

following is excerpt from official github repo wiki about the scala worksheet

a worksheet is a scala file that is evaluated on save, and the result of each expression is shown in a column to the right of your program. worksheets are like a repl session on steroids, and enjoy 1st class editor support: completion, hyperlinking, interactive errors-as-you-type, auto-format, etc.

// we can define objects or classes and the worksheet
//will print the sesult of every expression
object hello {
  def main(args:array[string]) {
    println("hello, scala !!")
  }
}

println("hello scala")
val a = 4 + 5

the result

defined module hello





hello scala
res0: unit = ()
a: int = 9

then a capture that shows you working with classe the work sheet and the console for scriptsin the intellij

capture working environment

score:12

scala worksheet

it's the same as scala interpreter (repl) but runs inside intellij. where you may easily and quickly evaluate some expressions. check intellij confluence page for more information.

scala script

if you don't want write script on bash you can do it with scala. it's just sequence of scala statements.

example:

import java.nio.file.{paths, files}

val scalasource = "*.scala"
val path = "path/to/necessary/folder"

val stream = files.newdirectorystream(paths.get(path), scalasource)

val paths = stream.iterator
while (paths.hasnext) {
  println(paths.next.getfilename)
}

running it:

$ scala scala_script_name.scala

to getting started pick up this guide.

classes & object

short answer for scala classes it's similar to pojo and scala objects it's a java singleton class.


Related Query

More Query from same tag