0

At this point, you might be wondering why you do not need to use new operator for primitives data types. The answer is that Java’s primitive types are not implemented as objects. Rather, they are implemented as “normal” variables. This is done in the interest of efficiency. For object versions of the primitive data types, refer Wrapper Classes.

What does "normal" variable mean? And why Java treat primitive types as "normal" variables?

4
  • 3
    I've never seen the term used in this way -- reference please Mar 28, 2020 at 17:52
  • I think it was just explaining that Java's object variables are references, whereas primitives are "normal" in the sense that they behave like variables generally do in other languages (passed by value). To my knowledge, the Java world gives no importance to the exact term "normal variables", though I could be wrong. Mar 28, 2020 at 17:53
  • But fyi, "normal" is not a standard term for Java primitives Mar 28, 2020 at 17:55

1 Answer 1

1

We do use new for instantiating reference types using a constructor. And we don’t use new for primitive types. Which was what the referenced text is trying to get across, I think.

However: You don’t declare things like ints and floats with new not because they aren’t implemented as objects but because the language provides a literal syntax for you to use.

You don’t use new when making a string (unless you’re doing something deliberately strange) because the language accepts a special syntax for describing string literals. But strings are definitely reference types.

There are languages where everything really is an object, unlike java. They have things that look similar to java primitive types but they are objects, and you don’t need new to instantiate them.

It is good to be aware there is a distinction between reference types and primitive values. There is an attempt to paper over it with auto boxing but it is still something that can cause problems, such as when using streams some ways of manipulating the elements may be better because they don’t force boxing and unboxing for each value.

Not the answer you're looking for? Browse other questions tagged or ask your own question.