Syntax is the grammar for a programming language. In this video, learn about the syntax for a Scala program by reviewing some of the common syntax rules needed to write Scala code.
- [Instructor] Before we can start coding in Scala, we need to understand the Scala syntax. Every programming language has its own grammar referred to as the syntax for that language. For example, we know that a period indicates the end of a sentence in English. In Java or C plus plus, the semicolon is used to indicate the end of a statement. And in Scala the semicolon is optional. Instead it reads a line of code until it encounters an end line symbol. As you can imagine, we cannot cover a complete list of all the syntax for Scala in this one video, but this is an introduction to some of the more common commands that you'll need.
As part of the course though, I did create two quick reference sheets. The first is the Scala Reference Guide where I tried to include as much of the syntax as possible. And the second is especially designed for programmers who have previous background in maybe Java or C plus plus, and it gives you the differences between the two. Now I feel that the best way to learn the syntax for a new programming language is to take a look at a program. So let's switch over to my IDE. If you're using the exercise files provided for this course, you first need to create a new project in the Scala IDE.
You can see here in my example, I already created a project called SyntaxExample and I have Helloworld.scala copied into that project. When you create the new project, you have two choices. You can copy and paste the program into the package or you can actually import the Helloworld.scala directly from the Exercise files folder. Also, when you run this program, if you get an error that there is no main class, go into the Run Configuration and make sure that the main class specified says Hello world.
What do you notice about this program? My first question was where were all the semicolons. Remember, I come from a Java background where the first thing I learned was to add a semicolon to the end of each statement. Alright, let's take a closer look and walk through the code line by line. On line one, we declare an object called Helloworld and we have an open curly bracket to indicate the start of our code. Now notice one of the nice things about an IDE is if I click to the right of this open curly bracket, it shows me what the matching closing curly bracket is and where it's located.
So all the code from line two to line 16 is part of the Helloworld object because the closing curly bracket is on line 17. On line two, I'm defining my main method. Again, if you have a background in Java, you probably notice a few things about the main method. There's no static keyword, the arguments have the data type and the variable name reversed compared to a Java method, there's no return type in front of the word main. Well let's take a look at the Scala definition of the main method.
It's starts with def which is a keyword which means to find a method, in this case main, which starts with a lower case m. We have an open parentheses. This is where we define our arguments or the values that we want to pass to this program. In this case, the first thing that you see there is args which is my variable name. I'll refer to that as args. It stands for arguments. It has a colon and on the right hand side of the variable name is the data type.
So the array bracket string bracket goes with the args. It's telling the compiler that args might have more than one string. It might have an array of strings. Then we have our close parentheses and then we have a colon. Again, very different from what we'd see in other languages. After the colon is the return type for this method. In this case, it's unit. Unit is similar to void meaning it doesn't really return anything. Then we have an equals and an open curly bracket.
So now again if I click to the right of the open curly bracket, I can see where the matching closed curly bracket is on line 16. On line three is our first comment. Slash slash indicates a single line comment. Line four is my first statement that will be executed which is a print line statement that says print out the words Hello, world. Lines five through seven is a multi-line comment. What's really nice again about the IDE is if you type slash asterisk, it automatically puts in the asterisk slash to close your comment.
On line eight I'm declaring a variable. Now on line eight and line nine, I have var and val. They both declare variables, the difference will be that var allows the variable to change. It is a mutable variable. Val is immutable. Let's take a closer look at line eight where I declare a variable. Again, firstName is the name of my variable. We're going to use camel case starting with a lower case letter and capitalizing the first letter of each word when we put some words together, in this case firstName.
I have explicitly specified the return type. Remember, the colon after the variable name indicates the return type and in this case is string. And then I am initializing this to the word Peggy. On line nine is my immutable variable age. What does immutable mean? If you said it cannot change, you are correct. And again, I specify that this is a data type of int and I'm giving it a value of 21. Then I have a print line statement just to print out those variables. On line 11, I'm declaring a variable called sum but notice I don't specify the data type.
I just have var sum equals 10 plus 25. One of the things we'll learn about Scala is that it can infer the data type. Since 10 and 25 do not have any fractional portion to their numbers, they're automatically assigned sum, the data type of int. And on line 12, I define another function called function1. It does not take any arguments so that is an optional thing when you're declaring a function. It has a colon and I'm specifying the return type of unit which means it does not return anything and I have equals open curly bracket and it just has a print line statement and in order to invoke the function1 method, I have to call it.
So line 15 is calling that method. The main method is called by default. Let's run this and see what the output looks like. So I'm going to click on this white arrow inside this green circle here to run, and on the bottom it says Hello world. Peggy is 21 years old, which is not quite true, but that's okay. And then finally, this is function1. So you can see there's no semicolons and in this little example, we've looked at a lot of syntax. We saw how to declare variables and specify their data types.
We saw how to define a function using the curly bracket. There will be times when you want the code to continue to the next line, mostly for readability, since the compiler doesn't care if it's all on one line. In order to indicate that something is going to the next line, we can end the statement with an operator such as plus, minus, equals. So let's take a look at line 11 where we have sum equals. If I try to put 10 plus 25 on the next line, that's fine because the equals sign tells the compiler there's more data to come.
And I also could, let me undo that. I could even just put the 25 on the next line and that would be fine because sum equals 10 plus, it knows that there's something else that needs to be added to the 10. Scala has a list of keywords and we've seen quite a few. The keywords in this case have that teal greenish color such as object, d-e-f, v-a-r, and val. For a complete list of all the keywords, please go to the scala/lang.org website because each version of Scala might be adding additional keywords to the list.
Scala also has the same set of something called escape sequences which are special combinations of letters and symbols used to create non-printable characters that the compiler uses to indicate things like a new line. Maybe you want to put a tab between two variables. The escape sequences are always going to be included inside of a string. So let's for example let's say we wanted to print the age on a separate line. Inside the string where I have is, I can use a backslash n.
Backslash n is called an escape sequence to print a new line. So now it will print my name, it'll say is and age will be on the next line. Let's run it. And we can see if I expand the window a little bit, we'll still see the hello world, Peggy is, and it goes to the next line. For a complete list of all the escape sequences, take a look at my reference guide. Now these are just a few of the new syntactical commands that you will see and use in Scala. As I introduce new concepts in Scala, I will continue to identify the syntax along the way.
Using an IDE provides other features that are useful especially for beginning programmers. The IDE that we used here has semantic highlighting, identifiers are colored based on their meaning and we saw that it will give us compile time errors as we type. Now also if you prefer Netbeans, maybe if you're coming from a Java background or IntelliJ, they both support Scala programming through plugins but for this series, I'm going to be using the Scala IDE for Eclipse.
Join Peggy Fisher as she helps get you started with Scala, so you can leverage the unique capabilities it offers. First, learn the basics of type inference, variables, loops, functions, and operators. Then, find out how to read files using a console, perform pattern matching, handle exceptions, and more. Finally, learn how to use classes, fields, methods, and objects.
- Integrating with IDEs
- Scala worksheets
- Scala repl sessions
- Type inference
- Creating variables
- Working with loops
- Higher-order functions
- Scala operators
- Working with decision statements
- Handling exceptions using try or catch
- Working with tuples and arrays
- Classes, fields, and methods