Is int an object in Java?

23,415

Solution 1

  • 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>

Solution 2

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.

Solution 3

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.

Share:
23,415
soandos
Author by

soandos

Student. Third to earn the marshal badge on SuperUser profile for soandos on Stack Exchange, a network of free, community-driven Q&amp;A sites http://stackexchange.com/users/flair/375094.png profile for soandos on Project Euler, which exists to encourage, challenge, and develop the skills and enjoyment of anyone with an interest in the fascinating world of mathematics. http://projecteuler.net/profile/soandos.png

Updated on July 05, 2022

Comments

  • soandos
    soandos about 2 years

    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
    soandos over 12 years
    So you are saying object = reference type?
  • siride
    siride over 12 years
    @soandos: yes. All class instances in Java are reference types.
  • soandos
    soandos over 12 years
    So it does not inherit from object?
  • Hovercraft Full Of Eels
    Hovercraft Full Of Eels over 12 years
    @soandos: it inherits from nothing. It can't inherit period as primatives are not reference types. This is similar to C#'s simple types.
  • soandos
    soandos over 12 years
    In other languages structs can inherit so the mere fact that it is a primitive is not enough
  • Stephen C
    Stephen C over 12 years
    @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.
  • Karan Chadha
    Karan Chadha almost 7 years
    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?
  • Karan Chadha
    Karan Chadha almost 7 years
    I have a doubt - can a method, whose return type is an Object, actually returnint? If yes, then, how can I get the int back from Object in the calling environment?
  • Ryan Stewart
    Ryan Stewart almost 7 years
    @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.