How to Check Datatype in Scala? Last Updated : 07 May, 2024 Comments Improve Suggest changes Like Article Like Report In this article, we will learn to check data types in Scala. Data types in Scala represent the type of values that variables can hold, aiding in type safety and program correctness. Table of Content Checking Datatype in ScalaApproach 1: Use Pattern Matching in ScalaApproach 2: Use the getClass Method in ScalaApproach 3: Use Manifest in ScalaApproach 4: Use Type Checking (isInstanceOf) in Scala Checking Datatype in ScalaBelow are the possible approaches to check datatype in Scala. Approach 1: Use Pattern Matching in ScalaIn this approach, we are using Pattern Matching in Scala to dynamically check the type of an object and return a corresponding string representing the detected data type. The approach1Fn method matches the object against different patterns (String, Int, List[_]), providing a flexible way to handle different data types in a concise and readable manner.In the below example, Pattern Matching is used to check the datatype in Scala. Scala object GFG { def main(args: Array[String]): Unit = { val str: Any = "Hello" val num: Any = 42 val lst: Any = List(1, 2, 3) println(approach1Fn(str)) println(approach1Fn(num)) println(approach1Fn(lst)) } def approach1Fn(obj: Any): String = obj match { case _: String => "String" case _: Int => "Int" case _: List[_] => "List[Int]" case _ => "Unknown" } } Output: Approach 2: Use the getClass Method in ScalaIn this approach, we use the getClass method in Scala to retrieve the runtime class of an object as a string. The approach2Fn method takes any object (Any) as input and returns the string representation of its runtime class. In the below example, the getClass Method is used to check the datatype in Scala. Scala object GFG { def main(args: Array[String]): Unit = { val str: Any = "Hello" val num: Any = 42 val lst: Any = List(1, 2, 3) println(approach2Fn(str)) println(approach2Fn(num)) println(approach2Fn(lst)) } def approach2Fn(obj: Any): String = obj.getClass.toString } Output: Approach 3: Use Manifest in ScalaIn this approach, we are using Manifest in Scala to obtain type information at runtime. The approach3Fn method takes a type parameter T with a Manifest context-bounded. It uses manifest[T].toString to get the string representation of the type T. In the below example, Manifest is used to check the datatype in Scala. Scala object GFG { def main(args: Array[String]): Unit = { val str: Any = "Hello" val num: Any = 42 val lst: Any = List(1, 2, 3) println(approach3Fn[Int](str)) println(approach3Fn[Int](num)) println(approach3Fn[List[Int]](lst)) } def approach3Fn[T: Manifest](obj: Any): String = manifest[T].toString } Output: Approach 4: Use Type Checking (isInstanceOf) in Scala In this approach, we are using Type Checking (isInstanceOf) in Scala to determine the type of an object at runtime. The approach4Fn method takes an Any object as input and checks its type using isInstanceOf. It returns a string representing the detected data type ("String", "Int", "List[Int]", or "Unknown").In the below example, Type Checking (isInstanceOf) is used to check the datatype in Scala. Scala object GFG { def main(args: Array[String]): Unit = { val str: Any = "Hello" val num: Any = 42 val lst: Any = List(1, 2, 3) println(approach4Fn(str)) println(approach4Fn(num)) println(approach4Fn(lst)) } def approach4Fn(obj: Any): String = { if (obj.isInstanceOf[String]) "String" else if (obj.isInstanceOf[Int]) "Int" else if (obj.isInstanceOf[List[_]]) "List[Int]" else "Unknown" } } Output: Comment More infoAdvertise with us Next Article How to Check Datatype in Scala? G gauravgandal Follow Improve Article Tags : Scala Similar Reads How to Check the Schema of DataFrame in Scala? With DataFrames in Apache Spark using Scala, you could check the schema of a DataFrame and get to know its structure with column types. The schema contains data types and names of columns that are available in a DataFrame. Apache Spark is a powerful distributed computing framework used for processin 3 min read Data Types in Scala A data type is a categorization of data which tells the compiler that which type of value a variable has. For example, if a variable has an int data type, then it holds numeric value. In Scala, the data types are similar to Java in terms of length and storage. In Scala, data types are treated same o 3 min read Scala Char getType() method with example The getType() method is utilized to return a value of type integer representing the characterâs general category. Method Definition: def getType: Int Return Type: It returns an Integer. Example: 1# Scala // Scala program of getType() // method // Creating object object GfG { // Main method def main( 1 min read How to Overcome Type Erasure in Scala? Type erasure may provide challenges in Scala, particularly when dealing with generics. Type tags and manifests are a means to prevent type erasure. By preserving type information during runtime, these approaches let you operate with genuine types as opposed to erased types. Table of Content What is 3 min read Scala Char ==(x: Char) method with example The ==(x: Char) method is utilized to find if the stated character value is equal to 'x' or not. And the type of 'x' must be Char. Method Definition: def ==(x: Char): Boolean Return Type: It returns true if the stated character value is equal to "x" else it returns false. Example: 1# Scala // Scala 1 min read Scala Double ==(x: Char) method In Scala, Double is a 64-bit floating point number, which is equivalent to Javaâs double primitive type. The ==(x: Char) method is utilized to return true if this value is equal to x, false otherwise. Method Definition - def ==(x: Char): Boolean Returns - Returns true if this value is equal to x, fa 1 min read How to Check the Data Type of a Variable in Ruby? In this article, we will discuss how to check the data type of a variable in Ruby. We can check the data types of a variable through different methods ranging from class to kind_of method in Ruby. Table of Content Using Class method Using is_a? Method Using kind_of? MethodUsing Class method The Clas 3 min read Scala Char ==(x: Int) method with example The ==(x: Int) method is utilized to find if the stated character value is equal to 'x' or not. And the type of 'x' must be Int. Method Definition: def ==(x: Int): Boolean Return Type: It returns true if the stated character value is equal to "x" else it returns false. Example: 1# Scala // Scala pro 1 min read Scala Char ==(x: Double) method with example The ==(x: Double) method is utilized to find if the stated character value is equal to 'x' or not. And the type of 'x' must be Double. Method Definition: def ==(x: Double): Boolean Return Type: It returns true if the stated character value is equal to "x" else it returns false. Example: 1# Scala // 1 min read Like