Scala syntax differs from other Java based languages. Learn about those differences in this video.
- [Instructor] If you're like me, one of the first things that I like to look at when I'm learning a new language is what does the syntax look like? So I have a sample program here that I created using the Sublime Text editor, but any text editor would work. Take a look at the first few lines of my program, where it says object Hello down to line six. What do you notice about this little code snippet? I have to be honest, my first question was where are the semicolons? Well, it turns out that in Scala, the semicolon is optional as a delimiter.
Instead, the end of line indicates when a statement is complete, although you will see examples where the code can continue onto the next line. Because the semicolons are optional, it does matter where we start the body of our classes and objects. Notice that the open curly bracket after the word Hello is on the same line as the name of our object. If that curly bracket was moved down to line two, then the compiler would not know that the definition for the method main was part of the object Hello.
And trust me, this is a very hard error to find if you make that mistake, so you need to tell the compiler and tell Scala what's coming next. On line two, after we define the main, again we have the open curly bracket to indicate that what follows is still part of this method called main. Now take a closer look at the main method itself. Do you notice anything odd there? You probably noticed a few things, especially if you're used to programming in Java. There's no static keyword, there's no return type, and the arguments have the data type and the variable name reversed compared to a Java main method.
In Java, we would have public, static, void, main, then in parentheses we would have string, bracket, bracket, space args to indicate our arguments. But in Scala, instead what we have is the keyword def to indicate that this is a function or a method, the name of our function or method, which in this case is main, the variable name args, colon, and then the data type. So as I said earlier, it's switched. Normally in Java we would have the data type before the name of the argument.
Here, we have the name of the argument, args, colon, and then the type, in this case an array of strings. On the next line, we have another variable called name. We use the keyword val, val name:String, where name is the data type string and it's equal to the first value in the argument list. The keyword val creates an immutable variable. So, when defining variables or identifying return types of methods, the type is located after a colon on the right side of the variable name or method.
Below the Hello World object is another small code snippet that's used to print out the current date in Italian format. Notice the import statements. On line eight, the import has some odd notation here where it has a curly bracket with Date, locale. This is syntax in Scala that allows us to indicate that the java.util.Date and java.util.locale are both required for this code. On line 10, we have import java.text.DateFormat._.
Underscore is similar to the .* you might see in java, and it indicates a wild card. A more general note for naming standards in Scala is that by convention, class names and object names should start with an uppercase letter, variable names start with a lowercase letter. Scala is case-sensitive the same as Java. On the other hand, there is a difference from Java when it comes to the class names and file names. Scala does not follow the strict Java standard to have the class name the same name as the file name.
As you can tell here, my file name is HelloWorld, and inside I have two class names, Hello and PrintDate. So when we compile a Scala file, it can create several .class files with different names from one single Scala file, although it is recommended that we name the file the same as the class when possible. Scala has a list of keywords similar to java such as case, class, object, the def that you see here, date, string, to check for a null value in Scala we can use the Scala.null keyword.
On the plus side, comments in Scala are the same as Java, where // indicates a single line comment, as you can see on line 11, and we can have a multi-line comment using /* and */. These are just a few of the new syntactical commands that you will see and use in Scala. As I continue to introduce new concepts in Scala, I will continue to point out any differences in syntax. We're not going to go into detail now on using the interactive Scala session here, but I do want to run this example using using the Scala repl or repl sesison, which stands for read evaluate print loop.
So I'm going to open a command window. I've changed my folder to be the location of where my Helloworld.scala project exists. So I'm going to start this Scala interactive session by typing in the word Scala. Now what I want to do is load my HelloWorld, so I'm going to use the command :load Helloworld.scala. As you can see, it created the object Hello and it created the object PrintDate, as well as importing the libraries.
From here, I can run the Hello, so let's start there. Hello.main, and I need to pass it a string representing the arguments, so I'm going to go ahead and say array, and I'm going to use world. And when I hit enter, it returns Hello World. So we've now satisfied our requirement for Hello World. Let me move this over a little bit, there we go. Notice it did not automatically run the PrintDate object as well.
I need to invoke that separately. Let's do that next. So I'm going to type PrintDate.main. This time I don't need to give it any parameters, so I'm just going to put null in the parentheses and hit enter. And it prints out the current date using the Italian format. As you can imagine, this is not a complete list of all the syntax for Scala, but it's an introduction to some of the more common commands that you will need. As we continue to work through code examples, you will continue to see subtle differences in the syntax compared to Java.
- Installing Scala
- Integrating with IDEs
- Creating variables
- Using special functions
- Basic types and operations
- Tuples, sets, maps, arrays, and lists
- Classes, fields, and methods
- Singleton objects