DEV Community


Java - Data Types: Primitive and Non Primitive

thexdev profile image M. Akbar Nugroho ・2 min read

Java is one of many popular language in this world and now I take my time to learn this language for sure.

One of the many things I've learned is Java data types. But, what is data types in Java?.

To put it simply, data types in Java is the type of value in your variable. There are many data types in Java such integer, boolean, character, arrays, etc.

But, what is primitive and non primitive data types in Java?

Primitive Data Types

Java Primitive Data Types

The primitive data type means the type and its value is exactly same in behavioral perspective. For example let's see the example code below.

public class PrimitiveDataTypeExample {
    public function void main(String[] args) {
        byte age = 19;
        short score = 10_000;
        int balance = 1_000_000_000;
        long people = 1_000_000_000_000;
        float salary = 20.123;
        double coordinate = 113.9213;
        // why girl is true? because she's never false.
        boolean girl = true;
        char a = 'a';
Enter fullscreen mode Exit fullscreen mode

Every value on variables above has primitive data types because we declare the variables and its values using these keyword: byte, short, int, long, float, double, boolean and char that represent the primitive data types.

But, what's mean the behavioral I mentioned before?. It's refer to the actual type of a value and how we treat these value.

When you assign a value into a variable, for example a variable called age is used to store the age of the user which has type called byte, which is a pure number.

It has no method and the only way to process these value is using arithmetic operators.

This also applies to char data type. When you assign a character into a variable, the type of your value is a char (a single character or symbol wrapped in single quote ).

It has no method to use and it can only use + of arithmetic operators to concatenate multiple characters.

Last, we have a boolean data type. A data type that has only two value: true and false.

This data type has no method and can not use the arithmetic operators.

Non Primitive Data Types

The non primitive data types are almost same like primitive data types, but its value is an object.

Discussion (0)

Forem Open with the Forem app