Scala Tutorial (2)

in scala •  7 years ago 

We are going to start this tutorial by explaining the diferent ways in which we can name variables in Scala, by using the keyword var or val.

var denotes that it is a variable, and as such, its value can change in different points of our program. For example:

var myNewVariable = 4
... // some time later...
myNewVariable = 40

As we can se, we are using the same variable to hold two different values 4 and 40.

On the other hand, a val defines a value, which is something that will not change in the entire life of the application or real life. For example, the value 4 will never change, since it would make no sense that it meant something diffent.

val gravity = 9.81

If you ever try to modify a value, the compiler will throw and error and abort compilation.

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

Congratulations @egosum! You received a personal award!

1 Year on Steemit

Click here to view your Board of Honor

Support SteemitBoard's project! Vote for its witness and get one more award!

Congratulations @egosum! You received a personal award!

Happy Birthday! - You are on the Steem blockchain for 2 years!

You can view your badges on your Steem Board and compare to others on the Steem Ranking

Vote for @Steemitboard as a witness to get one more award and increased upvotes!