Is int an object in Java?
Asked Answered
Z

5

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)

Zoril answered 9/10, 2011 at 0:15 Comment(0)
C
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>

Clifford answered 9/10, 2011 at 0:21 Comment(0)
T
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.

Tropous answered 9/10, 2011 at 0:26 Comment(2)
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?Ergot
@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.Tropous
S
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.

Seclusive answered 9/10, 2011 at 0:17 Comment(2)
So you are saying object = reference type?Zoril
@soandos: yes. All class instances in Java are reference types.Seclusive
P
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.

Phosphorylase answered 9/10, 2011 at 0:18 Comment(5)
So it does not inherit from object?Zoril
@soandos: it inherits from nothing. It can't inherit period as primatives are not reference types. This is similar to C#'s simple types.Binate
In other languages structs can inherit so the mere fact that it is a primitive is not enoughZoril
@Zoril - 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.Signore
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?Ergot
I
-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
Interblend answered 22/11, 2022 at 9:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.