13

More precisely, is int a part of the Integer class (a stripped down version or something) or is it something else entirely?

I am aware that int is a value type and Integer a reference type, but does int inherit from Object anyway?

(I am assuming that in this regard int, long, boolean etc are all similar. int was just chosen for convenience)

soandos
  • 4,978
  • 13
  • 62
  • 96

5 Answers5

22
  • The basic types in Java are not objects and does not inherit from Object.

  • Since Java 1.5 introduced allowed auto boxing between int and Integer(and the other types).

  • Because ints aren't Objects that can't be used as generic type parameters eg the T in list<T>

mikek3332002
  • 3,546
  • 4
  • 37
  • 47
17

From "Primitive Data Types": "Primitive types are special data types built into the language; they are not objects created from a class." That, in turn, means that no, int doesn't inherit from java.lang.Object in any way because only "objects created from a class" do that. Consider:

int x = 5;

In order for the thing named x to inherit from Object, that thing would need to have a type. Note that I'm distinguishing between x itself and the thing it names. x has a type, which is int, but the thing named x is the value 5, which has no type in and of itself. It's nothing but a sequence of bits that represents the integral value "5". In contrast, consider:

java.lang.Number y = new java.lang.Integer(5);

In this case, y has the type Number, and the thing named y has the type Integer. The thing named y is an object. It has a distinct type irrespective of y or anything else.

Ryan Stewart
  • 126,015
  • 21
  • 180
  • 199
  • I have a doubt - can a method, whose return type is an `Object`, actually return `int`? If yes, then, how can I get the `int` back from `Object` in the calling environment? – Chesser Aug 23 '17 at 01:02
  • @KaranChadha You should post this as a new question instead of a comment. Be sure to check around to see if the same question has already been asked before, though. – Ryan Stewart Aug 23 '17 at 01:41
1

Primitive types aren't objects, but are stored directly in whatever context they are needed. If they need to be treated like an object, they can be boxed in an Integer.

siride
  • 200,666
  • 4
  • 41
  • 62
1

If you talk about Integer:

The Integer class wraps a value of the primitive type int in an object. An object of type Integer contains a single field whose type is int.

In addition, this class provides several methods for converting an int to a String and a String to an int, as well as other constants and methods useful when dealing with an int.

int is not object, its a primitive type.

Racooon
  • 1,468
  • 2
  • 10
  • 28
  • So it does not inherit from object? – soandos Oct 09 '11 at 00:19
  • @soandos: it inherits from *nothing*. It can't inherit period as primatives are not reference types. This is similar to C#'s simple types. – Hovercraft Full Of Eels Oct 09 '11 at 00:22
  • In other languages structs can inherit so the mere fact that it is a primitive is not enough – soandos Oct 09 '11 at 00:23
  • 2
    @soandos - I don't know of any language specification that would call structs primitive types. A struct type is a constructed type. But anyway, generalizing concepts and terminology from one language to others is not entirely sound. To really understand a language you need to understand how *it* specifies its terminology. – Stephen C Oct 09 '11 at 01:36
  • I have a doubt - can a method, whose return type is an `Object`, actually return`int`? If yes, then, how can I get the `int` back from `Object` in the calling environment? – Chesser Aug 23 '17 at 01:06
-1
  1. Object has state and behavior
  2. state means fields (variables)
    ex: bicycle (current speed, current gear)
  3. behavior means methods to change that fields ex: changing current speed using methods
  4. Just imagine can you change state of an int if you change you could say that int is an object. we can't change so it not an object