Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Java's int.class in Scala?

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?

like image 544
Sandro Avatar asked May 02 '12 03:05

Sandro


People also ask

How do you access private classes in Scala?

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.

How do you call a class in Scala?

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.

How do I extend a class in Scala?

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.


2 Answers

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
like image 97
Chris B Avatar answered Sep 22 '22 05:09

Chris B


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
like image 20
Eugene Burmako Avatar answered Sep 21 '22 05:09

Eugene Burmako