How to use Pattern Matching In Scala
object Main {
def main(args: Array[String]) {
val str: String = null
str match {
case null => println("String is null")
case _ => println("String is not null")
}
}
}
Output:
String is null
It’s generally recommended to use the eq method or pattern matching for checking null references, as they provide a more concise way of handling null values in Scala.
How to check String is null in Scala?
In this article, we will learn how to check if a string is null in Scala. In Scala, you can check if a string is null using the following methods:
Table of Content
- 1. Using the == operator:
- 2. Using the eq method (recommended):
- 3. Using Pattern Matching: