Home java The main differences between primitive types and non-primitive types in Java

The main differences between primitive types and non-primitive types in Java




Integer or int .

Character or char .

I understand and use them when needed, but I don’t know why so.

Answer 1, authority 100%

I will assume that you are asking the difference between primitive types and wrapper classes.

Primitive types

  • Performance gain.

Type wrappers

  • Are part of the object hierarchy.
  • Needed in cases where the class can only work with objects.
  • Ease of use of fields such as MAX_VALUE , etc.

Nice answer about the reasons for introducing automatic auto-packing / unpacking.

Answer 2, authority 70%

Non-primitive are classes that are descendants of Object. Accordingly, they are transmitted by reference. All the rules for working with classes and objects apply to them. This is often needed if they need to be passed as an argument to a method that takes Object. Or specify it as a type parameter for Generic. For example, List & lt; Character & gt; . But there cannot be List & lt; char & gt; .

In turn, primitive types are passed by value. They take up less memory and are deprived of object overhead processing. In many cases, when assigning or passing a value, Java converts primitive types to and from their object wrappers automatically.

More details can be found in the language specification: http://docs.oracle.com/javase/specs/jls/se8/html/jls-4.html#jls-4.1

EDIT: It is important not to confuse what we are passing by value and what by reference. Any variable of a reference type stores a reference to an object or null if it is not associated with an object. As in the case of a variable of primitive type, a variable of a reference type is passed by value. In this sense, there is no difference between them.

However, a big difference arises when thinking about passing a primitive and an object (or array).

By passing a primitive value, in the called method, we lose all connection with the previous variable containing this value, and we cannot influence it in any way.

However, passing a reference, we get a key to an object or an array, which generally creates a shared mutable state problem and can lead to all sorts of “surprises” when the object-owner of the variable suddenly discovers (or does not even detect), that the value of its attribute has changed.

By using special class design techniques (or special libraries with such classes), it is possible to achieve that once created instances of reference types are (almost) impossible to change. Keywords: immutable type, persistent type, value object. In functional programming languages ​​of the JVM, these types are included in the base library. In Java itself, for example, wrappers over primitive types and String are implemented in this way.

Answer 3, authority 30%

There are 2 groups of types in Java: reference and primitive. There are only 8 primitives:

  • Integers – byte, short, char, int, long.
  • Floating point numbers (otherwise fractional) – float, double.
  • Boolean – boolean.

The rest of the types are derived from the Object class and are reference types. References are passed to methods (functions) by reference, i.e. if you change the reference type in the function, then they will change at the point of the call (in the place where you passed it from). Primitives are passed by value: i.e. whatever you do with the passed primitive type in the function / method, all this will forever remain inside the function / method, and outside (at the point of call) it (and its value) will remain the same after the function has been processed.

Read more here .

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions