0 votes
in Scala Constructs by
What is the difference between Java’s “If…Else” and Scala’s “If…Else”?

1 Answer

0 votes
by
Java’s “If…Else”: In Java, “If…Else” is a statement, not an expression. It does not return a value and cannot assign it to a variable. Example:-

 int year;

 if( count == 0)

   year = 2014;

 else

   year = 2015;

Scala’s “If…Else”: In Scala, “If…Else” is an expression. It evaluates a value i.e. returns a value. We can assign it to a variable.

 val year = if( count == 0) 2014 else 2015

**NOTE:-**Scala’s “If…Else” works like Java’s Ternary Operator. We can use Scala’s “If…Else” like Java’s “If…Else” statement as shown below:

 val year = 0

 if( count == 0)

   year = 2014

 else

   year = 2015

Related questions

0 votes
asked Sep 12, 2022 in Scala Constructs by Robin
0 votes
asked Sep 11, 2022 in Scala Constructs by sharadyadav1986
...