Sitemap
Handan Degerli

Handan Degerli

Data Types in Java

3 min readOct 14, 2022

--

In general, every class is a data type, every data type is a class in Object Oriented Programming language. Java programming language follows this general rule.

Why data type?

Java and some other programming languages ​​separate data into types. When defining a variable, it specifies which type of data it will hold. Thus, it allocates enough space for that variable in the main memory and allows operations to be performed according to that data type. As an example, it is possible to do four operations with numbers, but not with texts.

In addition to this, Java programming language is also a statically and strongly type language which means that helps detect errors at compile time.

Therefore, the types of the Java programming language are divided into three: primitive data types, reference data types and null data type.

1. The primitive types are shown on the below table with default size, value range and wrapper classes:

2. The reference types are class types, interface types, and array types. Class type is a set of instructions including data values and methods which means that operates on data. Interface is implemented by classes. And the array gives the fixed-size data structure that keeps the values of the same type. Class type is also template that define objects of the same type. An object represents an entity in the real world that can be distinctly identified. An object has a unique state and behavior: The state of an object consists of a set of data fields with their current values; the behavior of an object is defined by a set of methods.

An object is a dynamically created class instance or an array.

Example of creation an object of class

In the above example, objectName is a reference whose points a specific region in the heap memory . Where it points, there is the object or also called the instance.

Get Handan Degerli’s stories in your inbox

Join Medium for free to get updates from this writer.

What about String?

It is important to identify that String is an Object type and immutable which means that when you assign a value to a string expression, then when you want to make some additions to this value, it generates a new object, takes a new place in the heap, and leaves the previous value to the garbage collector.

3. On the other hand, there is also a special null type, the type of the expression null, which has no name. Because the null type has no name, it is impossible to declare a variable of the null type or to cast to the null type. The null reference is the only possible value of an expression of null type. The null reference can always be cast to any reference type. In practice, the Java programmer can ignore the null type and just pretend that null is merely a special literal that can be any of reference type.

Lastly, the main differences between primitive and reference data types are:

  1. Primitive types are predefined(already defined) in Java. Reference types are created by the programmer(except for String).
  2. Reference types can be used to call methods to perform certain operations, while primitive types cannot.
  3. A primitive type has always a value, while reference types can be null.
  4. A primitive type starts with a lowercase letter, while reference types starts with an uppercase letter.
  5. The size of a primitive type depends on the data type, while reference types have all the same size.
  6. Primitive type values are kept in the stack memory. On the other hand reference type’s references are kept in the stack memory, while values ​​are kept in the heap memory.

Sources: w3schools.com, geeksforgeeks.org, web.deu.edu.tr, berkeley.edu, docs.oracle.com, www.baeldung.com, www.oreilly.com

--

--