Scala Programming: Print hello world and version of the scala language
Scala Programming Basic Exercise-1 with Solution
Write a Scala program to print "Hello, world" and version of the Scala language.
Sample Solution:
Scala Code-1:
// Define an object named HelloWorld
object HelloWorld {
// Define the main method, which is the entry point of the program
def main(args: Array[String]): Unit = {
// Print "Hello, world!" to the console
println("Hello, world!")
// Print the version of the Scala language to the console
println("Scala language: " + util.Properties.versionString)
}
}
Sample Output:
Hello, world! Scala language: version 2.13.3
Explanation:
Here is the break down of the said Scala code:
- object HelloWorld: This declares an object named HelloWorld. In Scala, an object is a singleton instance, and it can contain methods and fields.
- def main(args: Array[String]): Unit = {: This declares the main method, which serves as the entry point of the program. It takes an array of strings (args) as its parameter. The Unit return type indicates that the method does not return any meaningful value (similar to void in other languages).
- println("Hello, world!"): This line prints the string "Hello, world!" to the console. The println function is used to print a line of text and automatically adds a newline character at the end.
- println("Scala language: " + util.Properties.versionString): This line prints information about the version of the Scala language. It uses string concatenation (+) to combine the string "Scala language: " with the result of calling util.Properties.versionString, which retrieves the version of the Scala runtime.
Scala Code-2:
// Define an object named Hello, which extends the App trait
object Hello extends App {
// Print "Hello, world" to the console
println("Hello, world")
// Print the version of the Scala language to the console
println("Scala language: " + util.Properties.versionString)
}
Sample Output:
Hello, world Scala language: version 2.13.3
Explanation:
Here is the break down of the said Scala code:
- object Hello extends App {: This declares an object named 'Hello' that extends the App trait. In Scala, extending the App trait allows you to write the body of your program directly inside the object without an explicit main method.
- println("Hello, world"): This line prints the string "Hello, world" to the console. The println function is used to print a line of text, and here it is part of the code executed when the program starts.
- println("Scala language: " + util.Properties.versionString): This line prints information about the version of the Scala language. It uses string concatenation (+) to combine the string "Scala language: " with the result of calling util.Properties.versionString,
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Scala Basic Exercises Home.
Next: Compute the sum of the two given integer values. If the two values are the same, then return triples their sum.
What is the difficulty level of this exercise?
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/scala-exercises/basic/scala-basic-exercise-1.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics