Why is it a Java class even if I pass a type parameter in a scala class in scala?

Question: Question:

We become indebted to. I'm new to scala. I'm worried because I don't understand the reason for the following code operation.

scala> case class MyVector[A]() {
  def test(init:A) = {
    println("type=" + init.getClass)
scala> (new MyVector[Int]()).test(3)
type=class java.lang.Integer

Since I passed Int, I want it to be Int instead of Integer. .. ..

I look forward to working with you.

Answer: Answer:

It depends on how the JVM works.

First and foremost, the JVM does not allow primitive types to be specified as type parameters. Therefore, in Java, if you specify int as a type parameter, it is automatically replaced with Integer , which is a non-primitive type corresponding to int , at compile time.

This assumption cannot be overturned even in Scala using the JVM. So in the illustrated code, A (= init.getClass ) is an Integer instead of an Int .

Scroll to Top