Category : Scala | Sub Category : Scala Programs | By Prasad Bonam Last updated: 2020-10-07 09:51:24 Viewed : 43516
1.
Basic
“Hello ” program of scala
1.1. one way to write with main method
Scala is a general-purpose programming
language providing support for both object-oriented programming and functional
programming.
def defines a method.
Compile: The scalac
command compiles one (or more) Scala source
file(s) and generates Java bytecode which can be executed on any standard JVM. The Scala compiler works similarly to javac
, the Java
compiler of the Java SDK.
> scalac HelloWorld.scala
Execute: The scala
command
executes the generated bytecode with the appropriate options:
> scala
HelloWorld
Example:
Following example illustrates how we can print “Hello Scala”
with main method.
Save the file as − HellowWorld.scala.
HelloWorld.scala //File
name
package runnerdev
object HelloWorld {
def main(args: Array[String]) {
println("Hello Scala")
}
}
compile
and run the above example as follows
scala> scalac HelloWorld.scala
scala> scala
HelloWorld
Output:
Hello Scala
1.2. second way to write with out main method
Another way to write that no need to
define the main method but extends App
The App trait can be used to quickly turn objects into
executable programs. Here is an example:
object Main
extends App {
println("Hello
World: " + (args
mkString ", "))
}
Here, object Main
inherits the main
method of App
.
args
returns the
current command line arguments as an array.
Example:
Following example illustrates how we can print “Hello Scala”
with out main method.
Save the file as − HellowWorld.scala.
HelloWorld.scala //File name
package runnerdev
object HelloWorld extends App {
println("Hello Scala")
}
compile
and run the above example as follows
scala>
scalac HelloWorld.scala
scala>
scala HelloWorld
Output:
Hello Scala
Here is a simple "Hello, World!" program in Scala along with an explanation of each part:
scalaobject HelloWorld { def main(args: Array[String]): Unit = { println("Hello, World!") } }
Explanation:
object HelloWorld
: In Scala, the entry point of a program is an object. In this case, we define an object named "HelloWorld." Objects in Scala are singletons, meaning there is only one instance of the object in the entire application. This is where your program starts executing.
def main(args: Array[String]): Unit = { ... }
: Within the "HelloWorld" object, we define a method named main
. This is the entry point for your program, similar to the public static void main(String[] args)
method in Java. The main
method takes an array of strings args
as its parameter, which can be used to pass command-line arguments to your program. Unit
is similar to void
in other programming languages and indicates that the method does not return any value.
println("Hello, World!")
: Inside the main
method, we use the println
function to output the string "Hello, World!" to the standard output (usually the console). println
is a predefined function in Scala used for printing text with a newline character at the end.
When you run this program, it will print "Hello, World!" to the console.
To run this program, follow these steps:
Make sure you have the Scala programming language installed on your system.
Save the code to a file named, for example, HelloWorld.scala
.
Open your terminal or command prompt and navigate to the directory where you saved the file.
Compile the Scala code using the scalac
compiler, which produces a bytecode file:
scalac HelloWorld.scala
Run the program using the scala
command:
scala HelloWorld
You should see the "Hello, World!" message printed to the console.
This simple program demonstrates the basic structure of a Scala program, with an object, a main
method, and the use of a predefined function for output. Scala is concise and expressive syntax makes it a versatile language for various application types.