Monday, 29 May 2017

JAVA VS SCALA

JAVA VS SCALA


















POJO IN JAVA VS SCALA


The Java version of a simple POJO:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
public class PostJ {
    private String title;
    private String text;
    private String author;
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getText() {
        return text;
    }
    public void setText(String text) {
        this.text = text;
    }
    public String getAuthor() {
        return author;
    }
    public void setAuthor(String author) {
        this.author = author;
    }
}


And the Scala version.

1
case class Post(title: String, text: String, author: String)



Variables :

Java Syntax:
------------------

<data type>  <variable name> = <value> ;
   int                    a                    = 10;
   String               s                   = "Big Data";

Scala Syntax:
------------------

val <variable name> : <data type> = <value>
val            a                :  Int              = 10
val             s               :   String        = "Spark"

var is immutable.

NOTE: SCALA SUPPORTS " TYPE INFER"= automatically find the data type from value.
 val           a    = 10;
val            s=   "Spark"
var <variable name> : <data type> = <value>

var is mutable.