Labels

Monday, April 11, 2022

Scala Identifiers (Class -28)

Identifiers in a programming language are the names given to a class, method, variable or object to identify them in the program.

  e.g.:- myObjectmainargsvalue1, etc.

Variables in Scala (Class -27)

 variable is named a reference to a memory location. The location stores the data that is used by the program.

 Collect the data and keep them in a Container. There are 2 types of Collections:

a)      Mutable Collections

b)      Immutable Collections

a)      Mutable Variables are those that allow us to change its value any time in the code.

   e.g.: var keyword

b)      Immutable Variables that are made immutable are read-only variables in Scala. This means their value remains unchanged throughout the program. E.g.: val keyword.

 In both the above cases, you can assign value to a variable with / without datatype.



Keywords in Scala (Class -26)

Keywords are special words for a programming language that have some predefined actions in the programming. Thus, these keywords cannot be used as names for defining objects, classes, functions, variables, etc. The compiler will throw an error if we try to use keywords as identifiers.

Scala programming languages also consist of a set of keywords to help in the efficient working of the program.

There are in total 39 keywords present in Scala. Here is a list of all keywords in Scala,

  1. Abstract; 2. Case; 3. Catch; 4. Class; 5. Def; 6. Do; 7. Else; 8. Extends; 9. False; 10. Final; 11. Finally; 12. For; 13. Forsome; 14. If; 15. Implicit; 16. Import; 17. Lazy; 18.  Match;   19. New; 20. Null; 21. Object; 22. Override; 23. Package; 24. Private; 25. Protected;     26. Return; 27. Sealed; 28. Super; 29. This; 30. Throw; 31. Trait; 32. Try; 33. True; 34. Type; 35. Val; 36. Var; 37. While; 38. With; 39. Yield.


Scala Hierarchy (Class -25)

In Scala programming language everything is an object. Even data types are treated as objects and have some method that performs operations of the data stored by them.

So, all values have a type, including numerical value, classes and functions. The diagram below illustrates a subset of the type hierarchy.



Any is the parent data type (supertype) of all types, also called the top type. It defines certain universal methods such as equals, hash Code, and toString

Any has two direct subclasses: AnyVal and AnyRef.

A)     AnyVal represents 9 non-null able value types which are : DoubleFloatLongIntShortByteCharUnit, and BooleanUnit is a value type which carries no meaningful information. 

B)      AnyRef represents non-value reference types. Every user-defined type in Scala is a subtype of AnyRef. If Scala is used in the context of a Java runtime environment, AnyRef corresponds to java.lang.Object.

There are two important types that are at the bottom of the hierarchy. They are 

a) Nothing & b) Null.

The type at the bottom of the hierarchy that is a subtype of all the types in Scala is Nothing. There is no value of this type.

Null type is subtype of reference types (for all subtypes of AnyRef type).

SCALA Basic Program (Class -24)

 There are 3 parts in Scala program-

a) Class definition,

b) Main() function and

c) Print statement.