-
Notifications
You must be signed in to change notification settings - Fork 16
Java Vs Scala
Java is not pure Object Oriented: It has Primitive Types and Reference Types. Eg. System.out.println(1.toString)
would fail.
In Scala, everything is a Reference Type. So println(1.toString)
works. Scala uses the correct type at compile time when generating bytecode i.e. in this case it either generates an Int
object or a Java int
in bytecode.
In Java you have to declare specific variables that you do not want to change as final
.
In Scala, by default variables defined using val
or def
are final. You can use var
to define variables that can change.
In Java you have to implement Singleton manually, also to keep track of certain things (eg. instance count, etc) you have to use static
variables or methods.
In Scala, a singleton can be defined by just writing a regular class with the keyword object
instead of class
.
Also if you have a class of which you want to keep track of certain things (eg. instance count, etc), then just declare an object
with the same name as that of the class. In this case, the object becomes the "companion object" of the class. All definitions inside this object will be static by default i.e. these will be Class Variables.
class Car {
}
object Car {
def someMethod = { ... }
val someString = "Hello"
}
// usage:
def c1 = new Car
def c2 = new Car
println(Car.someString)
Java requires classname and filename to be the same.
Scala does not have this requirement. Only requirement is a class and its companion object be defined in the same file.
Week 1
Week 2
- Higher Order Functions
- Classes and Objects
- Substitution Model (CBV, CBN) with Classes
- Operators, Precedence and Types
Week 3
- Class Hierarchies and Dynamic Binding
- Organizing Classes and Scala Class Hierarchy
- Polymorphism (Subtyping and Generics)
Week 4
- Objects-Everywhere
- Subtyping and Generics (Bounds and Covariance)
- Decomposition
- Pattern Matching
- Collections (Lists)
Week 5
- Collections (List Methods)
- Pairs and Tuples
- Collections (Lists - Applying functions to elements)
- Collections (Lists Reduction)
- Collections Theory (Lists concat and reverse)
Week 6