Before writing Scala Programming Language, we need to know basic Scala concepts:
Basic Scala Concepts |
Meaning |
Example |
Class |
a blueprint that defines the states and behaviors
related to it. |
Fields, variables, function objects, methods |
Object |
an instance of a Class. it has states and
behavior. It is created using the new
keyword. |
var obj = new Class(); |
Methods |
a block of code that can perform a certain amount
of tasks. |
def methodName (parameters) : (return Type) |
Fields |
variables defined inside the Class body. states
of Objects. |
Mutable fields (var) and Immutable fields (val). |
Closure |
a function whose value depends on the value of
multiple variables that are declared outside the function. |
val multiplier = (i:Int) => i * 10 |
Traits |
just like
classes they have fields and methods but multiple traits can be used
together(inherited) to increase the functionality of a class. |
trait trait_name { |
Wrapper Class |
used to put primitive data value into an object. |
Byte, Short, Integer, Long, Float, Double,
Character, Boolean |
Identifiers |
are the names given to a class, method, variable
or object to identify them in the program. |
myObject, main, args, value1. etc. (not keywords
/ digits) |
Code block |
multiple lines of code that have only one entry
and one exit point. |
{ } used in methods, class definition, loop
statements and logic. |
Variables |
reference to a memory location where data is
stored. |
val, var & lazy. |
Data Types |
tells the compiler about the type of data that is
used by the programmer. |
Boolean, Integer,
Float, Double, Char, String, Nothing, Any, anyRef, anyVar, etc. |
any' class |
Superclass of all classes in Scala. Root of the class hierarchy. |
Two subclasses they are: 1) anyRef, 2) anyVar |
Nothing |
subtype of all types in Scala. |
Nothing is a subtype of Char, Float, Int. |
Lazy variable |
saves memory when variables declared are not used
while object creation. |
lazy val |
Type interface |
used to make the declaration of data type
optional. |
compilers work to detect the data type. |
Operators |
tell the compiler to perform a specific
operation. |
Plus (+), And (&), Or (I), etc. |
Numeric type |
deals with all types that are stored in Integers
and Decimals. |
Float, Double, Int, Short, Long |
Unit type |
used as a return statement for a function when no
value is to be returned. |
compared
to void data type and is a subclass of anytype trait. |
Option |
container that contains one single value which
can be one of the two distinct values. |
One of the two values is 'none' and others can be
any object valid in the program. |
Basic program |
three parts - the Class definition, Main()
function and the Print statement. |
object MyClass { |
Method parameters |
hose variables which are passed to the method
while calling the method. |
add(a:Int , b:Int) |
Literals |
basic constants value that can be assigned to a
variable. |
var i = 10L; var I = 123.54d |
Expressions |
A line of
code that can be compiled and has some resultant value. These are displayed
on screen using println statements. |
println(45 * 24332); |
Function |
a reusable chunk of code that can perform the
operation on the value passed as a parameter. |
val result = (mark: Float ) |
Block |
multiple statements that are enclosed between
curly braces. |
{ // statement
1 |
No comments:
Post a Comment