7

I have a question, why aren't Java primitive data types just called "Java data types" or something similar?

0

9 Answers 9

18

Because Java has more data types than just primitives. The primitive data types are:

  • byte
  • short
  • int
  • long
  • float
  • double
  • boolean
  • char

A data type that is a non-primitive is a reference data type, which are references to objects.

Some examples are:

  • String
  • Integer
  • ArrayList
  • Random
  • JFrame

Here is a simple example of the difference between the two types:

int i1 = 10;
Integer i2 = Integer.valueOf(10);

int i1 is a variable of the primitive data type int, with the primitive int value of 10.

Integer i2 is a variable with a reference data type of Integer, referencing an Integer object which contains the value 10.

Sign up to request clarification or add additional context in comments.

3 Comments

@Joshua: True, but that is because of autoboxing coming into play. Behind the covers, there is an conversion between a primitive and its wrapper class occuring.
@Bert F: You're right, I missed the most obvious one! (I think I'll leave the list as it is for now, but that was definitely a good call!)
Although the Java Language Specification does not mention it, I believe "void" is also a primitive type.
13

alt text

Comments

4

To distinguish between them and Objects.

Comments

2

Because there are two categories of types in Java.

From the Java Language Specification, CHAPTER 4: Types, Values, and Variables:

The types of the Java programming language are divided into two categories: primitive types and reference types. The primitive types (§4.2) are the boolean type and the numeric types. The numeric types are the integral types byte, short, int, long, and char, and the floating-point types float and double. The reference types (§4.3) are class types, interface types, and array types. There is also a special null type. An object (§4.3.1) is a dynamically created instance of a class type or a dynamically created array. The values of a reference type are references to objects. All objects, including arrays, support the methods of class Object (§4.3.2). String literals are represented by String objects (§4.3.3).

Comments

1

To distinguish them from object data types.

Comments

1

Because reference types can also be considered data types. Primitives are considered value types. Both can be considered a data type.

Comments

1

To understand why, I think you need to look at programming languages other than Java. For example:

  • In C++ there are, primitive data types (int, double, etc), constructed data types (struct, etc) and object / reference types.

  • In Ada there are primitive data types, and other data types that are derived from the primitive types; e.g. range types.

So, my understanding is that Java data types are described as "primitive data types" to put them into the context of other languages. They are "data types" in the sense that they have no object identity, and they are "primitive" in the sense that the specific types are defined by (and fundamental to) the Java language.

Comments

1

Objects are also variables as well, so the term "primitive" is used to distinguish those types.

Comments

0

Non primitive types are called java reference types and they have name starting with capital letter. Eg: Integer, Float etc. For non primitives we can create the instances.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.