-3

I have just started with Scala and was wondering that if Scala is really inter-operable with Java why are they having their own set of Libraries and not just using the Java libraries?

In other words is there any benefit of writing the same library in Scala that is already available in Java?

Down the line both gets converted to byte code and runs over JVM.

Abhishek
  • 878
  • 12
  • 28
  • They _could,_ but it might not fit well with the language. Different styles of library work better in different languages. – Louis Wasserman Jul 26 '18 at 05:27
  • I think the difference is that java is OOP and scala is functional language and it is based on lamda calculas, and java is based on concept of class and object. In scala everthing is function (apart from datatype) in java everthing is object. so there should be the internal differnece between implementation of the same functionality. – Raman Mishra Jul 26 '18 at 05:28

1 Answers1

3

If you read scala documentation

Scala combines object-oriented(OOP) and functional programming(FP) in one concise, high-level language.

  • In my opinion scala got a JVM market because java used to be only OO until java8; while you could do both OO + fp in scala.

  • Since fp encourages ideas like immutability, it is not ideal to use same Java libraries because Java did not offer immutability or was not practiced.

    Thats why you will see engineers re-writing or porting JVM libraries using idiomatic scala; also called pure fp, instead of calling impure code.

Here's example of scala immutability;

scala> case class Customer (name: String)
defined class Customer

scala> val customer = Customer(name = "Stephen")
customer: Customer = Customer(Stephen)

scala> customer.name = "Lino"
<console>:13: error: reassignment to val
       customer.name = "Lino"
                     ^

java10 now provides some immutable data-structures like List, Map;

java immutable example using final;

jshell> class Customer { final String name = "default customer"; }
|  created class Customer

jshell> var customer = new Customer()
customer ==> Customer@887af79

jshell> customer.name = "Lino"
|  Error:
|  cannot assign a value to final variable name
|  customer.name = "Lino"
|  ^-----------^

Twist: since java is becoming more functional I can not tell what scala would be doing with their OO + FP philosophy?

prayagupa
  • 30,204
  • 14
  • 155
  • 192
  • 2
    "Java does not offer immutability." - Well ... it does, sort of. Some Java types are immutable, and you can define your own immutable types. But Java doesn't have a way to enforce immutability as an intrinsic type system property. (It is an emergent property of the type's fields and methods ...) Note that your example can be implemented in Java by declaring the variable `final`. – Stephen C Jul 26 '18 at 05:45
  • The example you've shown is not immutability, immutability prohibits to modify the state of a variable. You're just trying to reassign a readonly variable. – Lino Jul 26 '18 at 05:45
  • Correct, as I know java now provides some immutable datastructure example of variable definition in java10.0.1 `var cities = List.of("seattle", "portland", "perth")`. – prayagupa Jul 26 '18 at 05:55
  • (That functionality has been in Java since Java 1.2 using `Arrays.asList`. This is NOT new stuff ....) – Stephen C Jul 26 '18 at 06:02
  • @Lino In the example I was simply trying to show mutating the reference. @Stephen thanks for correction what about immutable `Map` in java8+ `var mountains = Map.of(1, "Everest", 2, "Annapurna")` – prayagupa Jul 26 '18 at 06:09