Let's write a "Hello world" program to learn how to compile and run a scala code.
Login to the CloudxLab web console. Create a directory 'scala' and go inside it. Create a file hello_world.scala using the command "nano hello_world.scala". You can also use vim editor instead of nano. Type the code as displayed on the screen. Press "control o" to save the file and then press enter. Press "control x" to exit the nano editor.
To compile the code, type "scalac hello_world.scala" and press enter. You can see two class files HelloWorld$.class, HelloWorld.class. These .class files are generated bytecode which gets executed by JVM.
To run the code, type "scala HelloWorld". You can see that "Hello, world!" is printed on the screen.
Let's understand the structure of the code. It consists of a method called "main". The "main" method takes the command line argument as a parameter, which is an array of strings. and prints "Hello world" on the screen. HelloWorld is a singleton object, that is a class with a single instance. We'll discuss "object" later in the course.
If we've just one .scala file, we can also run it using the scala interpreter. Type "scala hello_world.scala" and press enter. We can see that "Hello, world!" gets printed on the screen.
We can evaluate Scala code by the way of arguments using -e option. Type scala -e 'println("Hello, World!")' and "Hello, world!" gets printed on the screen
Taking you to the next exercise in seconds...
Want to create exercises like this yourself? Click here.
Please login to comment
4 Comments
What is the use of -e option ?
Can you please explain indetail about its usage?
Upvote ShareThe
-e
option in Scala is used to evaluate a single line or expression of Scala code directly from the command line. When you runscala -e 'expression'
, Scala interprets and executes the provided expression without the need to create a separate Scala source file.Usage and Explanation:
1. Inline Expression Evaluation:
The primary purpose of
-e
option is to allow quick evaluation of Scala code snippets directly on the command line.You specify the Scala expression or code snippet as a string argument immediately following
-e
.2. Example Usage:
For example, running
scala -e 'println("Hello, World!")'
executes the Scala expressionprintln("Hello, World!")
inline.This will print
Hello, World!
to the console without needing to create a separate Scala source file.3, Simple and Fast Testing:
-e
option is particularly useful for testing or quickly experimenting with Scala code without setting up a complete Scala project or writing a full Scala script.It allows developers to check the behavior of specific Scala expressions or functions immediately from the terminal.
4, Expression Limitations:
The
-e
option expects a single, self-contained Scala expression enclosed in quotes ('...'
or"..."
).Complex or multi-line code is not directly supported with
-e
. However, you can use semicolons (;
) to separate multiple expressions within the same line.5. Example with Multiple Expressions:
You can use semicolons to separate multiple expressions. For instance:
This will evaluate and print the result of
x + y
, wherex
andy
are defined and initialized within the same inline expression.Use Cases:
Quick Testing: Evaluate simple Scala expressions to verify behavior or test small code snippets.
Scripting: Incorporate inline Scala expressions into shell scripts or automation tasks.
Education and Learning: Experiment with Scala syntax and features interactively from the command line.
In summary, the
Upvote Share-e
option in Scala facilitates rapid evaluation of Scala expressions directly from the command line, enabling quick testing and experimentation withoutFor me I am literally confused if we need to solve this in Jupiter why the process of Library Installed not discussed. Also Giri sir is using Terminal . Then why same interfaceis used for practice. please do the needful.
Upvote ShareYou can use web console for the exercise.
Upvote Share