简体   繁体   中英

Why Data type in java are not object?

I have studied about java datatype very earlier and found that they are primitive type. As java is object oriented but its data types are not. why is it so?

It is said that everything in Java is an Object at the same time the Java Programming Language is meant to be simple. Since the primitive data types consume less memory and can be accessed faster, they are not objects. The equivalent Wrapper classes are also available in java like "Integer" "Short" etc. They can be used as objects if you want. However, the wrapper classes will be stored in Heap and they are slow.

It is more efficient to deal with primitive types than making each variable a full fledged object. The most common types (ie. int, char, float etc.) are simple primitives. The rest are objects.

In Java all its Object types can be defined in terms of primitives and references to objects.

The primitive types and references are the types which cannot be defined in terms of other types.

What some languages do is hide the distinction, this effectively makes the fundamental types higher level which can make the developers life easier if they are not interested in the difference, but much harder if they are.

Few real languages are pure object orientated, pure functional or pure logic languages, you have to many some pragmatic choices to make a language usable.

It is a combination of two things.

  1. It is a language design preference / choice. The Java language designers clearly thought that primitives were different from objects ... and array types were different from classes. They are not alone in this view. Many other OO language designs make this kind of distinction.

  2. There is a penalty in making primitive types such as number types full object types.

Jesper points out (in a comment on another Answer) that some languages map OO numeric types to hardware supported types under the covers. However, he doesn't mention that this can only be made to work if you place restrictions on them. You can only do this for a numeric type that is known to the compiler, and a leaf type; ie one that doesn't have (custom) subtypes.

If a numeric type is not a leaf type, then it cannot be represented as a native type because there would be no way to implement this:

  class MyInteger extends Integer /* a builtin numeric type */ {
      public ... add(...) { /* a different way of doing addition */ }
  }

Why? Because the builtin type is represented as a native hardware integer type, and you can't do dispatching on such a representation.

In short the penalty is either that OO numeric (etc) types are slow and use more memory that the equivalent Java primitive types, or that they are ringed with restrictions that limit your ability to exploit the OO-ness of the types.

性能和内存消耗是原始数据类型而不是使用完整对象的充分理由。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM