Professional Documents
Culture Documents
Scala
Scala
Ex:
Car myCar1 = new Car("blue");
Here myCar1 and myCar2 point to the same object but myCar3
points to the different object though all three have same content
https://dzone.com/articles/object-identity-and-equality-in-java
Whenever hashCode() mehtod is invoked on the same object more than once within single execution
of application, hashCode() must return same integer provided no information or fields used in
equals and hashcode is modified. This integer is not required to be same during multiple execution
of application though
—————————————————————————————————————————————————————————————
——————————————
Case classes:
Classes with ‘case’ modifier are called case classes - this
modifier makes the Scala compiler add some syntactic
conveniences to the class
Which includes:
1.It adds the factory method with the name of the class
2.All arguments in the parameter list of a case class
implicitly get a val prefix, so they are maintained as fields
3.Compiler adds natural implementations of methods like -
to print, hash and compare a whole tree consisting of the class and
all its arguments
toString
hashCode
equals
4. Compiler adds a copy method to class for making
modified copies - Useful for making a new instance of the class that
is the same as another one except that one or two attributes are
different (works by using named and default parameters)
All the above 4 features come along with the case classes - With
this the class and objects become a bit larger as additional method
are generated and an implicit field is added for each constructor
parameter.
Kinds of Patterns:
————————
1.Wildcard patterns
2. Constant patterns
3. Variable patterns
4. Constructor patterns
5. Sequence patterns
6. Tuple patterns
7. Variable binding
Companion objects
In Scala, it is common to use syntax that looks like a function call. For example, if firstName
is a string, then
Equivalent to firstName.apply(i)
Option/some/None -
https://commitlogs.com/2016/09/17/scala-option-some-and-none/