Java Data Types

Data types represent which data should be stored in a variable. In the previous chapter, we have worked with some data types like int, String, etc. In this chapter, we will learn Java Data Types in detail.

IMPORTANT: Java is a statically typed language. This means while declaring a variable you must specify which type of data you want to store.

Java Data Types

Java Data Types can be divided mainly into two categories;

  • Primitive Data Types – are the most basic data types in Java. It has no additional methods, which means fundamental data types. Primitive data types can be divided roughly into 8 types.
  • Non-primitive Data Types – are compound data types. It has additional methods and always refers to objects. String, Arrays, Classes are examples of non-primitive data types.

Java Primitive Data Types

Notice the below table to explore the Java primitive data types quickly.

Data TypesReserved SizesValue Ranges
byte1 byte-128 to 127 (whole numbers)
short2 bytes-32,768 to 32,767 (whole numbers)
int4 bytes-2,147,483,648 to 2,147,483,647 (whole numbers)
long8 bytes-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
float4 bytesFractional numbers with up to 7 decimal points
double8 bytesFractional numbers with up to 15 decimal points
char2 bytesStores single character
boolean1 bittrue or false

Primitive Data Types Example

All the primitive data types are declared in the same way. And the basic syntax of declaring a variable is <data-type> <variable-name>;. See the java example below to figure out;

public class dataTypes {
    public static void main(String args[]) {
        byte myByte = 80; // range: -128 to 127
        short myShort = 30000; // range: -32768 to 32767
        int myInt = 500000; // range: -2147483648 to 2147483647
        
        /*Long range: -9223372036854775808 to 9223372036854775807
          Use 'L' at the assigned value while declaring long var*/
        long myLong = 20000000000L;

        /* Float range: use up to 7 decimal point
           use small 'f' after the assigned value */
        float myFloat = 5.40f;

        /* Double range: use up to 15 decimal point
           use small 'd' after the assigned value */
        double myDouble = 120.56d;

        boolean isTrue = true; // range: true & false

        // char holds single character within must single quoted
        char myChar = 'A';

        /* Strings are also declared as primitive data types
           although it is a non-primitive data type.
           But string is refered by object in java.
        */
        String mystr = "Hello";

        //printing values
        System.out.println(myByte);
        System.out.println(myShort);
        System.out.println(myInt);
        System.out.println(myLong);
        System.out.println(myFloat);
        System.out.println(myDouble);
        System.out.println(isTrue);
        System.out.println(myChar);
        System.out.println(mystr);

    }
}

Output

80
30000
500000
20000000000
5.4
120.56
true
A
Hello

Java Non-primitive Data Types

Non-primitive data types are always referred to some built-in objects. We are not familiar with Java objects yet. So before learning about Java non-primitive data types, we will familiar with Java objects.

Objects are concepts of Object-oriented programming features in Java. We will learn Java OOP (Object-oriented programming) and non-primitive data types with their method in the next subsequent chapters.

Some non-primitive data types are Strings, Arrays, Classes, Block, Constructors, etc.

NOTE: In the above example, we have declared a string variable just like other primitive data types. But String is actually a non-primitive data type. But the exception is String is declared as non-primitive variables. But string has many methods to work with. We will learn everything about string in a later chapter.

What is next?

So far, we have learned many numeric data types like byte, short, int, long, float, double, etc. What can we do using these kinds of data? Well, simply we can perform mathematical operations. Right?

But to perform mathematical operations we need to learn how mathematical operators work in Java programming. So in the next chapter, we will study Java Operators in detail.

Posted in