DEV Community

Cover image for JAVA Basics #5 - Data Types
Chathumi Kumarapeli
Chathumi Kumarapeli

Posted on • Edited on

JAVA Basics #5 - Data Types

In this article we are going to focus on Data types that are available in Java.

Data types in Java

In java we have two main data types.

  1. Primitive data types
  2. Reference data types

Primitive Data Types

These data types are to store simple values.
Ex: numbers, characters, Booleans ect.
Let’s have an overall look on those primitive data types.

Name Size (bytes) Description
byte 1 Stores whole numbers in the range [-128, 127]
short 2 Stores whole numbers in the range [-32 768 , 32 767]
int 4 Stores whole numbers in the range [-2 147 483 648, 2 147 483 647]
long 8 Stores whole numbers in the range [-9 223 372 036 854 775 808, 9 223 372 036 854 775 807]
float 4 Stores 6 to 7 decimal digits
double 8 Stores around 15 decimal digits
char 2 Stores single characters like A, B, a, ...
boolean 1 Stores true or false

Reference Data Types

This data type refers to objects and is used to store complex data like strings, arrays, classes, etc.

Primitive vs Reference

Primitive Reference
Predefined in Java Not predefined except 'String'. Programmers can create them.
Always has a value Can be 'null' as well
Memory is allocated and released by JRE Need to allocate memory by the programar
Cannot used to call methods Can be used to call the methods
Starts with a lowercase letter (Ex: int) Starts with an uppercase letter (Ex: String)

Since you know how to do variable declaration, I want you to store the value ‘62’ in a variable named ‘physicsMarks’. Go try it and come back!
I guess that you have written your code like this;

int physicsMarks = 62;
Enter fullscreen mode Exit fullscreen mode

Actually, there’s nothing wrong in the above piece of code. However, if you pay attention to the size of the data type that you have used, you can see that it requires 4 bytes to store an integer value. But here we have just stored two digits which only requires 1 byte. Therefore, the most suitable code can be written as;

byte physicsMarks = 62;
Enter fullscreen mode Exit fullscreen mode

With that I think you have realized that paying attention to the size of data type is also required when declaring variables.
We can declare integer variables as follows;

int population = 123_463_000;
Enter fullscreen mode Exit fullscreen mode

When writing large numbers, we use commas ‘,’ in between (123, 456, 000). Like wise we can use underscores as shown above to separate the parts of a given number.

Let's see how to declare variables of type 'long'.

long population = 6_432_736_000L;
Enter fullscreen mode Exit fullscreen mode

As you can see, you need to add L to the end of the number. If not java will think it as an integer and will give an error.

Go through the following code section to get more understanding on primitive data types.

float price = 10.99F; 
// If 'F' or 'f' is not used, java sees this number as a double
char grade = 'A'; 
// need to use single quotes
boolean isMale = false; 
// here true and false are reserved key words in java
Enter fullscreen mode Exit fullscreen mode

Now let's see an example of a reference data type. 'Date' is such an example. If we look at the code it should be like this;

package com.company;
import java.util.Date;
public class Main {
    public static void main(String[] args) {
        Date today = new Date();
        System.out.println(today);
    }
}
Enter fullscreen mode Exit fullscreen mode

Here, Date is the class of the reference type. However, to use that you have to import the library java.util.Date as shown above. Else this will give an error.
We have used the keyword new to allocate memory since this is not a primitive type. That makes 'today' an object (an instance) of the class 'Date'. The output of this program will gives you the exact date and time that you run the program.

alt dateExample

Reference data types have members. Therefore, the object 'now' can access the methods in the class 'Date' by using the dot operator (now.). Give it a try by yourself :)

Top comments (0)