I am using Scala to do some work with Java's reflection API. But I can't seem to figure out how to access in Scala what in Java would be: int.class, float.class, boolean.class.
Basically the classes objects that represent the primitive data types.
So what is the Scala version of int.class?
In Scala, a private top-level class is also accessible from nested packages. This makes sense, since the enclosing scope of both the private class and the nested package is the same (it's the package inside which both are defined), and private means that the class is accessible from anywhere within its enclosing scope.
Defining a classWe call the class like a function, as User() , to create an instance of the class. It is also possible to explicitly use the new keyword, as new User() , although that is usually left out. User has a default constructor which takes no arguments because no constructor was defined.
To extend a class in Scala we use extends keyword. there are two restrictions to extend a class in Scala : To override method in scala override keyword is required. Only the primary constructor can pass parameters to the base constructor.
int.class, float.class, etc. do not exist. The equivalent boxed types each have a static field called TYPE
which represents the primitive type. Is this what you mean?
e.g. for int/Integer:
http://docs.oracle.com/javase/6/docs/api/java/lang/Integer.html#TYPE
If so, you reference it from Scala just like you would in Java:
scala> Integer.TYPE
res0: java.lang.Class[java.lang.Integer] = int
Welcome to Scala version 2.10.0-20120430-094203-cfd037271e (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_25).
Type in expressions to have them evaluated.
Type :help for more information.
scala> classOf[Int]
res0: Class[Int] = int
scala> classOf[Integer]
res1: Class[Integer] = class java.lang.Integer
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With