What is the difference in Java’s “If..Else” and Scala’s “If..Else”? [closed]

–2 votes
Can anyone explain how the Java’s “If..Else” and Scala’s “If..Else” are different?
closed with the note: basic
Jul 31, 2019 in Apache Spark by Dhiman

closed Jul 31, 2019 117 views
There is no difference in any programming language If-Else.

Syntax:

if(Boolean_expression){
   //Executes when the Boolean expression is true
} else{
   //Executes when the Boolean expression is false
}
0 votes

Hey,

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.

Here is an example you can follow to get the difference:

int year;

if(count == 00

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. You can assign it to a variable.

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

Scala’s “If. Else” works like Java’s Ternary Operator. You 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

answered Jul 31, 2019 by Gitika
• 26,310 points
if you intend to reassign you cannot declare year as val
Thanks, @Divija for your contribution.

Please register at Edureka Community and earn credits for every contribution. A contribution could be asking a question, answering, commenting or even upvoting/downvoting an answer or question.

These credits can be used to get a discount on the course. Also, you could become the admin at Edureka Community with certain points.

Thanks!

Related Questions In Apache Spark

+1 vote
3 answers

What is the difference between rdd and dataframes in Apache Spark ?

Comparison between Spark RDD vs DataFrame 1. Release ...READ MORE

answered Aug 27, 2018 in Apache Spark by shams
• 3,580 points
22,703 views
0 votes
1 answer

How the if-else statement is different for Scala and Java?

Hey, There is one main difference. This statement ...READ MORE

answered Jul 29, 2019 in Apache Spark by Gitika
• 26,310 points
50 views
0 votes
1 answer

What is the difference between spark streaming and spark structured streaming?

Hi@akhtar Generally, Spark streaming  is used for real time ...READ MORE

answered Feb 4 in Apache Spark by MD
• 8,870 points
137 views
0 votes
1 answer

What's the difference between 'filter' and 'where' in Spark SQL?

Both 'filter' and 'where' in Spark SQL ...READ MORE

answered May 23, 2018 in Apache Spark by nitinrawat895
• 10,870 points
11,792 views
+1 vote
1 answer
+1 vote
1 answer

Hadoop Mapreduce word count Program

Firstly you need to understand the concept ...READ MORE

answered Mar 16, 2018 in Data Analytics by nitinrawat895
• 10,870 points
4,559 views
0 votes
1 answer

hadoop.mapred vs hadoop.mapreduce?

org.apache.hadoop.mapred is the Old API  org.apache.hadoop.mapreduce is the ...READ MORE

answered Mar 16, 2018 in Data Analytics by nitinrawat895
• 10,870 points
648 views
+1 vote
11 answers

hadoop fs -put command?

put syntax: put <localSrc> <dest> copy syntax: copyFr ...READ MORE

answered Dec 7, 2018 in Big Data Hadoop by Aditya
25,885 views
0 votes
1 answer

What is the difference between persist() and cache() in apache spark?

Hi, persist () allows the user to specify ...READ MORE

answered Jul 3, 2019 in Apache Spark by Gitika
• 26,310 points
1,117 views
0 votes
1 answer