integers are not objects in Java, just because they are not instances of a class? -
there similar questions (e.g. why info type in java not object?) still not clear.
this definition of objects oracle's document:
an object software bundle of related state , behavior.
consider:
int nnn = 3; i think nnn has type int, , state my value 3, although has no behavior. why it's not object?
then @ next iii object:
integer iii = new integer(3); i found other explanations says "objects instances of class in java". (sorry can't find read it)
so value nnn not object in java, of import reason it's not instance of class?
so definition of object in java should be: object software bundle of related state , behavior, , in java, should instance of class. right?
update:
thanks answering question. found need create question clear since found many people didn't notify explanation in comments, when people commented "it's not object because has no behavior".
we know object has built-in methods, tostring, equals, etc. when define class, say:
class user extends object { // empty body } the instance of user have these methods(behaviors) too. if object hasn't provide these methods? in case, new user() here won't have methods(behaviors), should not consider it's object?
sorry guys, i'm puzzled :(
from java language specification. chapter 4. types, values, , variables.:
4.1. kinds of types , values
there 2 kinds of types in java programming language: primitive types (§4.2) , reference types (§4.3). there are, correspondingly, 2 kinds of info values can stored in variables, passed arguments, returned methods, , operated on: primitive values (§4.2) , reference values (§4.3).
4.2. primitive types , values
a primitive type predefined java programming language , named reserved keyword (§3.9):
primitivetype:
{annotation} numerictype {annotation} boolean
numerictype:
integraltype floatingpointtype
integraltype: byte short int long char
floatingpointtype: float double
4.3. reference types , values
there 4 kinds of reference types: class types (§8.1), interface types (§9.1), type variables (§4.4), , array types (§10.1).
4.3.1. objects
an object class instance or array. reference values (often references) pointers these objects, , special null reference, refers no object.
so, object variable not primitive type. primitive types are: byte, short, int, long, char, float, double , boolean. should not confuse these primitive types class wrappers: byte, short, integer, long, character, float, double , boolean, respectively.
in case object class doesn't define method @ all, creating class no methods won't have behavior @ all, in java, definition of object, or improve term: reference value, variable refers instance of class or array, non-primitive type. note reference value may not need behavior at all, behavior defined in the class, , reference value may utilize or not.
java class object integer primitive
No comments:
Post a Comment