我回答错误的 scala 问题 - noonecare/spark GitHub Wiki

Is Scala a Pure OOP Language? Is Java a Pure OOP Language?

我认为 scala 融合了 FP 和 OOP ,所以 scala 不是 纯正的 OOP 语言。我记忆中, Java 是纯正的 OOP 语言,只是 JAVA8 添加了函数编程的特性。

正确答案是: Pure Object-Oriented Programming Language means that everything should be an Object.

Java is not a Pure Object-Oriented Programming (OOP) Language because it supports the following two Non-OOP concepts:

  • Java supports primitive data types. They are not objects.
  • Java supports Static members. They are not related to objects.

Yes, Scala is a Pure Object-Oriented Programming Language because in Scala, everything is an Object and everything is a value. Functions are values and values are Objects.

Scala does not have primitive data types and also does not have static members.


What is the Main drawback of Scala Language?

Apart from many benefits of Scala, it has one major Drawback: Backward Compatibility Issue. If we want to upgrade to latest version of Scala, then we need to take care of changing some package names, class names, method or function names etc.

For instance, If you are using old Scala version and your project is using BeanProperty annotation. It was available in “scala.reflect” like “scala.reflect.BeanProperty” in old versions. If we want to upgrade to new Scala versions, then we need to change this package from “scala.reflect” to “scala.beans”.


What is the main motto of Scala Language?(这个问题不重要,但是还是记住的好)

Like Java’s Motto “Write Once Run Anywhere”, Scala has “Do More With Less” or “Do More With Less Code” Motto. “Do More With Less” means that we can develop more complex program or logic with less code.


What is “Type Inference” in Scala?

Types can be inferred by the Scala Compiler at compile-time. It is known as “Type Inference”. Types means Data type or Result type. We use Types at many places in Scala programs like Variable types, Object types, Method/Function Parameter types, Method/Function return types etc.

In simple words, determining the type of a variable or expression or object etc at compile-time by compiler is known as “Type Inference”


final class ?

不能被继承的类称为 final class


Like Java’s java.lang.Object class, what is the super class of all classes in Scala?

As we know in Java, the super class of all classes (Java API Classes or User Defined Classes) is java.lang.Object. In the same way in Scala, the super class of all classes or traits is “Any” class.

Any class is defined in scala package like “scala.Any”


Use Cases of Nothing


What is REPL in Scala? What is the use of Scala’s REPL? How to access Scala REPL from CMD Prompt?

REPL stands for Read-Evaluate-Print Loop. We can pronounce it as ‘ripple’. In Scala, REPL is acts as an Interpreter to execute Scala code from command prompt. That’s why REPL is also known as Scala CLI(Command Line Interface) or Scala command-line shell.

The main purpose of REPL is that to develop and test small snippets of Scala code for practice purpose. It is very useful for Scala Beginners to practice basic programs.

We can access REPL by using “scala” command. When we type “scala” command at CMD Prompt, we will get REPL shell where we can type and execute scala code.


What are the Scala Features?

Scala Language supports the following features:

Supports both OOP-style(Imperative-Style) and Functional-Style Programming Pure Object-Oriented Programming Language Supports all Functional Features REPL(Read-Evaluate-Print Loop) Interpreter Strong Type System Statically-Typed Language Type Inference Supports Pattern Matching Supports Closures Supports Persistent Data Structures Uses Actor Model to develop Concurrency Applications Interoperable with Java Available all development tools – IDEs, Build Tools, Web Frameworks, TDD and BDD Frameworks


Does Scala support Operator Overloading? Does Java support Operator Overloading?

原来 JAVA 不支持 operator overloading 的。


What is the difference between Java’s “If..Else” and Scala’s “If..Else”?

java if else is statement, scala's if else is expression. statement have no return value, expression has return value.


Is Scala an Expression-Based Language or Statement-Based Language? Is Java an Expression-Based Language or Statement-Based Language?

In Scala, everything is a value. All Expressions or Statements evaluates to a Value. We can assign Expression, Function, Closure, Object etc. to a Variable. So Scala is an Expression-Oriented Language.

In Java, Statements are not Expressions or Values. We cannot assign them to a Variable. So Java is not an Expression-Oriented Language. It is a Statement-Based Language.


Tell me some features which are supported by Java, but not by Scala and Vice versa?

Java does not support Operator Overloading, but Scala supports it. Java supports ++ and — operators , but Scala does not support them. Java has Checked and Unchecked Exceptions, but Scala does not have Checked Exceptions. Scala does not support break and continue statements, but Java uses them. Scala does not have explicit Type casting, but Java supports this feature. Scala supports Pattern Matching, but Java does not. Java uses Primitive Data types, but Scala does not have. Java supports static members, but Scala does not have static members concept. Scala supports Implicits and Traits, Java does not support them.


How many public class files are possible to define in Scala source file?

In Java, we can define at-most one public class/interface in a Source file. Unlike Java, Scala supports multiple public classes in the same source file.

We can define any number of public classes/interfaces/traits in a Scala Source file.


Like Java, what are the default imports in Scala Language?

We know, java.lang is the default package imported into all Java Programs by JVM automatically. We don’t need to import this package explicitly.

In the same way, the following are the default imports available in all Scala Programs:

java.lang package scala package scala.PreDef


How many operators are there in Scala and Why?

Unlike Java and like C++, Scala supports Operator Overloading. Scala has one and only operator that is “=” (equalto) operator. Other than this all are methods only.

For instance 2 + 3, here “+” is not an Operator in Scala. “+” is method available in Int class. Scala Compiler observes 2 and 3 are Integers and tries to find that “+” method in Int class. So Scala Compiler converts “2 + 3” expression into “2.+(3)” and make a call to “+” method on integer object “2” and pass integer object “3” as parameter to “+” method.

Both “2 + 3” and “2.+(3)” are equal. It’s just Scala’s syntactic sugar to write programs in Functional style.


Mention Some keywords which are used by Java and not required in Scala? Why Scala does not require them?

Java uses the following keywords extensively:

‘public’ keyword – to define classes, interfaces, variables etc. ‘static’ keyword – to define static members. Scala does not required these two keywords. Scala does not have ‘public’ and ‘static’ keywords.

In Scala, default access modifier is ‘public’ for classes,traits, methods/functions, fields etc. That’s why, ‘public’ keyword is not required. To support OOP principles, Scala team has avoided ‘static’ keyword. That’s why Scala is a Pure-OOP Langauge. It is very tough to deal static members in Concurrency applications.


What is Monad in Scala?

A monad is an object that wraps another object. You pass the Monad mini-programs, i.e functions, to perform the data manipulation of the underlying object, instead of manipulating the object directly. Monad chooses how to apply the program to the underlying object.


Explain ‘Scala higher order’ functions?

Scala allows the definition of higher order functions. These are functions that take other functions as parameters, or whose result is a function. In the following example, apply () function takes another function ‘f’ and a value ‘v’ and applies function to v.