Values, Variables and Types

Daniel Ciocîrlan
A free video tutorial from Daniel Ciocîrlan
Software Engineer & Best-Selling Instructor
4.7 instructor rating • 11 courses • 40,504 students

Learn more from the full course

Scala & Functional Programming for Beginners | Rock the JVM

Become a Scala programmer and get the skills you need to work with Spark, Akka, and any Scala framework!

15:16:06 of on-demand video • Updated May 2020

  • understand Scala code in production environments or open-source projects
  • think code differently with functional programming
  • write applications in Scala from scratch
  • contribute to products and open-source projects written in Scala
  • skills to start developing with powerful tools on top of Scala: Spark, Akka, Play
English [Auto] Hello hello I'm Daniel. Welcome back to the first video in which we will write some actual code. We're going to be talking about the most basic building blocks of scar. We're going to talk about values and types and also variables. All right. So I'm here in my environment with this project that we created earlier. Now in every lecture starting from now on I'll be creating a new school application and we will write and test code plus do some practice exercises in each application. All right. So for this lecture I'm going to go and create a new school application but first I'm going to create a new package. So I'm going to right click on the lecture's package and I'm going to click on package. I'm going to call this part 1 basics. OK. Now in this lecture's dot part one basic's package I'm going to write click here and we're going to create a new call class I'm going to call this values variables types and I'm going to make this an object in sort of a class. We're going to talk about objects in section 2 when we talk about object oriented programming. But for now don't worry about it. Select this object in the drop down here and click OK. Now after the object values variables and types that you created add at the end extends app the way that we did with the school playground toy app that we created in a previous video. All right so don't worry about the object and extends app things. We'll get to those in due time. But basically this is what we need to be able to run this code in the ID. So if I right click I'm going to have this run option available in the context menu. All right so I've entered presentation mode in intelligence and without any further ado let's create a value. So the syntax is Val VHDL then a name. Let's call this X for simplicity then call in. Notice this one and a type. I'm going to use it for this example. It is a simple integer then an equals and then the value of X say 42. Now to print this to the con. I'm going to use the print line function which is print n. So if I print line X and if I right click and run well we're going to see 42 in the con.. OK so pretty simple. So that's the syntax for declaring of al pretty straightforward. Now a new thing that I wanted to show you some new things already. Watch this. If I try to give a new value to X say X equals 2 This will upset the compiler. It says if I however it says re-assignment Divell. So this is the first takeaway from this lecture vowels the things that you declare with your valid cannot be reassigned once they have a value they're set in stone. That is to say that I'm going to write this in all caps. Vowels are immutable vowels act in a similar way to Constans or finals in Java or C only serve a different purpose that is vowels aren't really regarded as constants to be reused in various parts of the code but more like intermediate computations to later use in bigger computations. Now the skull away and the functional programming way of thinking in general involves working with such vowels. You'll get more familiar with this style as we go through the course. OK so let's delete this little token here because it's not allowed. So keep in mind that vowels are immutable. All right. Now next lesson. Watch what I'm doing. So I'm selecting the type of X.. Like we said call an end and I'm going to delete it. So as you see nobody seems to complain. And if I right click and run the code will run and still print 42. OK so another piece of news. The types of vowels are optional. That's because the compiler can infer the type of. In this case x from the right hand side it looks at the value 42 and the compiler figures out that 42 is an int so X must be an int. If I hover over it you will see that x is an int and that was inferred by the compiler already without needing me to specify explicitly so. Keep in mind that compiler can infer types. Now in practice we don't usually mention types explicitly for values and later variables as the compiler is smart enough to figure types for us. Now if you do mention the type as you say call an end make sure the right unsigned conforms to that type because otherwise the compiler will complain. For example if I use a string which is a piece of text in between double quotes and says hello Scala. Now the compiler complains because it says expression of type string does not conform to expected type. So you basically say that X is an end but you give it a value of type string on the right hand side so the compiler is confused. All right so let's undo this. I'm going to give X the value 42 back OK. Now speaking of strings that we saw earlier let's use some other types. OK so if I declare a value of type string. So I'm declaring avowe called it a string. The type is string with a capital S and values of type strings are pieces of text in between double quotes. So I say hello this is a string and nothing too spectacular. You probably knew about strings already. Now before we move on to other types a small detour so I'll put a semicolon here just so you know that semi-colons are allowed in Scala like you've probably seen in Java or see where C++ but they're not really necessary in school as we write it. Expression on it's own line. So any cones are necessary if you write multiple expressions on the same line but this is usually bad style. So if I say Well another string which is goodbye now the semi-colon is mandatory because it separates the expression. Again this is discouraged. So instead I'm going to delete the semi-colon and put each expression on its own line. This is the recommended course style. All right back from detour let's get acquainted with the basic types in this column. You've used them before in other languages for sure but I just want to walk you through them in Scullin. OK. First of all the Boolean type. OK. This is a Boolean a boolean with the type Boolean with a capital B and the values for billions are the keywords true or false. Ok they are key words that is you can't name things with true or false. Gay characters are denoted by the type car with the capital C and character values are single characters in between single quotes so save the character a. Now since you've seen them before with the type and and you can give them the values of other vowels say X. OK. There are also other integer numerical values shorts and longs. Let's give them an example here. So if I say a short with the type short with a capital S and I'm going to give this value of say four thousand six hundred thirteen if I put in too big of a number score will complain and says expression of type does not come from text but type short because sure it is and with half the representation size so it's represented on two bytes instead of four. The compiler will complain if the number is too big. All right. Also longs for the type long with a capital L and longs are also integer types but with double the representation they represent an 8 bytes instead of 4. And you can put in a numerical value here. But again if the number is too big for type in Skala will complain that integer number is out of range for type. And so to mark that this is actually a number and it should be represented as long you put it a capital L here at the end. This is similar to the Javaris index for longs. Finally we have decimal values so we have floats with a capital have and these are decimal values like 2.0 F.. Notice this little f at the end. This is March to the compiler that this is a float number. If you delete the f. The compiler will say that this number is actually double not a float. So let's introduce doubles double with the type double with a capital D of course and you can write simple numerical decimal values without any markers at the end. This is by default the double representation in this. Again this is consistent with the Java syntax as well. All right so these are the basic types. I'm not going to bore you too much with them. All right I'm going to introduce the concept of variables in SCal they are defined by the keyword. Var V.R.. So if I say variable this is defining a variable that's also a type of end and with capital of course. And let's give it a value of say for now. Think with variables instead of values is that they can be reassigned so if I say a variable equals five. Now the compiler will not complain because this is a VAR not a vowel. Now variables are used for in functional programming we know as side effects side effects are useful because they allow us to see what our programs are doing. Examples of side effects are for example changing a variable like here. We're printing something to the console or displaying something on screen programs without side effects are easier to understand as we will see in the scores because there are no variables and logic to keep track of. However we cannot really eliminate side effects completely because we need more programs to do something to the world. We'll just need to be mindful of side effects from now on and I'll discuss more cases of side effects of dangers of side effects as we go through the course. All right so in this video you've learned about values and how to declare them as in vowel name colon type equals something on the right hand side. You've learned that values cannot be reassigned. That is they are immutable. By contrast variables can be reassigned. We see that they are mutable now. Functional programming involves working less with variables. So we'll start thinking more in terms of vowels rather than VAR's and you will learn the style of thinking along the way. All right. You've also seen that vowels enviro's have types and the types can be inferred by the compiler which is pretty cool from now on we'll use the compiler to determine the type of things you've also learned the basic types in school and how to use them. You've seen Bullen's you've seen numerical values and you've also seen characters and strings. All right. Daniel some good stuff coming. I'm really excited and I'm waiting for you in the next few.