1

What is the difference between these two? A primitive type cannot have "Class", can it - it is primitive. There are no objects to have a class.

Where does these two make a difference?

Jakub Zaverka
  • 8,816
  • 3
  • 32
  • 48
  • 1
    I think you meant `int.class` vs `Integer.TYPE`? – wkl Apr 17 '12 at 16:46
  • @LouisWasserman - `TYPE` is a constant of the Integer class, which returns a `Class` object: http://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html#TYPE – wkl Apr 17 '12 at 16:47
  • https://www.google.ru/search?q=Difference+between+Integer.class+and+Integer.TYPE – Alexei Kaigorodov Apr 17 '12 at 16:48
  • 1
    http://stackoverflow.com/questions/8759305/what-is-difference-between-int-class-and-integer-type-in-java this is what I meant. – Jakub Zaverka Apr 17 '12 at 16:50
  • 2
    Does this answer your question? [What is difference between int.class and Integer.TYPE in java?](https://stackoverflow.com/questions/8759305/what-is-difference-between-int-class-and-integer-type-in-java) – limido Oct 06 '20 at 10:19

1 Answers1

5

I'm not positive what you mean, but -- A Class object is a runtime representative of a type. That said, there is such a thing as int.class that is distinct from Integer.class, even though int is not a class. It's kind of complicated, but it's necessary to make reflection work even on methods that take primitive arguments or return primitive values, or to distinguish a method that accepts an Integer from a method that accepts an int.

Integer.TYPE, I believe, is equivalent to int.class.

Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413
  • We're looking at a distant future, but according to [Oracle's Java roadmap](http://www.javaworld.com/javaworld/jw-03-2012/120315-oracle-s-java-roadmap.html), Java 10 and beyond will get rid of primitive types, thus making more or less everything in Java an object. Now if just those methods (functions!) could be made first class... – darioo Apr 17 '12 at 16:49
  • @darioo So you're basically saying that instead of `int` we will have to write `Integer`? Better get my fingers ready (**NO** not in that sense)! – fireshadow52 Apr 17 '12 at 17:01
  • @fireshadow52 - No, they will probably do what .Net does and make `int` an alias for `Integer` - otherwise pretty much all Java code predating Java10 would fail to compile (breaking the rule that Java is supposed to be code-compatible with previous versions). Transparent `Integer` object creation and usage, you won't have to do anything differently. – wkl Apr 17 '12 at 17:25
  • I really have no idea how they'll make it work with backwards-compatibility...if they plan to maintain backwards compatibility. ::sigh:: – Louis Wasserman Apr 17 '12 at 17:35
  • 1
    @darioo Regarding _"Java 10 and beyond will get rid of primitive types"_, more than seven years later it seems incredible that anyone would have ever made that wildly optimistic claim! – skomisa Aug 22 '19 at 16:28