Lesson 15 of 15

Objects

Objects (Singletons)

A Scala object is a singleton — a class with exactly one instance, created automatically:

object MathUtils {
  def square(x: Int): Int = x * x
  def cube(x: Int): Int = x * x * x
}

println(MathUtils.square(4))  // 16
println(MathUtils.cube(3))    // 27

Objects are useful for utility methods and constants:

object Constants {
  val Pi = 3.14159
  val E  = 2.71828
}

println(Constants.Pi)

Your Task

Define an object StringUtils with a method isPalindrome(s: String): Boolean that returns true if the string equals its reverse.

JS Transpiler loading...
Loading...
Click "Run" to execute your code.