Java Data Types – Learn Primitive & Non-Primitive Data types with Examples

Data types are the type of data in which variables are stored. Programming languages are incomplete without them. Java data types are an essential concept for every beginner without them, you cannot initialize or declare any variable in your code.

Here we will discuss two types of data types in Java and their syntax with example.

Java Data Types

Any programming language can be of two types- statically typed language or dynamically typed language. While the statically typed language is the type of language where every variable and keyword is already known at the compile time, the former language can receive different data types over time.

Java programming language is a statically typed language, i.e. it does not allow to change the data type once declared and also variable and keyword should be assigned a data type, they are predefined.

Before we start our discussion, you must familiar with Java Classes and Object.

Types of Data Types in Java

There are basically two types of Java data types-

Types of Java Data Types

 

1. Primitive Data Types in Java

Java Primitive data types are the simplest type, i.e. they don’t have any special capabilities and can have only single values.

There are eight primitive data types in Java-

1.1 Java boolean

It can represent only one bit of information, i.e. true or false. They cannot be changed either explicitly or implicitly, but the programs for conversion can be easily written.

Example-

// A Java program to demonstrate boolean data type
class boolean
  {
     public static void main(String args[])
        {
             boolean b = true;
             if (b == true)
             System.out.println("true");
         }
  }

1.2 Java byte

It is useful in saving memory for large arrays, it is a signed two’s complement integer. It has a size of 8 bits with a range from -128 to 127.

It’s the right time to discuss what is Array in Java? 

Example-

// Java program to demonstrate byte data type in Java
   class byte
     {
        public static void main(String args[])
            {
                byte a = 126;
                System.out.println(a);
             }
    }

1.3 Java char

It is also a single character with a memory of 16 Bit Unicode character. It has a range from ‘\u0000’ or 0 to ‘\uffff’ or 65535.

Example-

// Java program to demonstrate char data type in Java
     class short
        {
            public static void main(String args[])
               {
                    char a = ‘G’;
                    System.out.println(a);
               }
       }

1.4 Java short

Short is similar to byte and is used to save memory in large arrays. It is also a two’s complement with a size of 16 bit and ranges from -32,768 to 32,767 (inclusive).

Example-

// Java program to demonstrate short data type in Java
         class short
             {
                  public static void main(String args[])
                           {
                                    short a = 56;
                                    System.out.println(a);
                           }
             }

1.5 Java int

Int is two’s complement with memory allocation of 32 bits and ranges from -2^31 to 2^31-1.

Example-

// Java program to demonstrate int data type in Java
    class int
        {
            public static void main(String args[])
                {
                    int a = 56;
                    System.out.println(a);
                }
        }

Do you know how Variables are declared in Java?

1.6 Java long

Long is two’s complement with memory allocation of 64 bits and ranges from -2^63 to 2^63-1.

Example-

// Java program to demonstrate long data type in Java
       class long
          {
              public static void main(String args[])
                     {
                            long a = 100000L;
                            System.out.println(a);
                     }
          }

1.7 Java float

Float is a single-precision IEEE floating point with memory allocation of 32 bits and uses a suffix as F/f.

Example-

// Java program to demonstrate float data type in Java
   class float
      {
         public static void main(String args[])
            {
               float a = 4.5541132f;
               System.out.println(a);
            }
      }

1.8 Java double

The double Java data type is a default choice when it comes to decimal, as it is a double-precision 64 bit IEEE floating-point variable.

Example-

// Java program to demonstrate double data type in Java
      class double
            {
                  public static void main(String args[])
                        {
                              double a = 1.33526252726;
                              System.out.println(a);
                        }
            }

2. Non Primitive Data Types in Java

The non-primitive Java data types are created by the programmer during the coding process, they are known as the “reference variables” or “object variables” as they refer to a location where data is stored.

2.1 Java String

Strings are basically a collection of characters, they cannot change once they are created.

Wait, take a break and learn Strings in Java

2.2 Java Array and object

An array in Java is a group of variables that are similar in nature and we can allocate them dynamically. Their size has to specify in int, and their length can be found by member length. Their indexing always starts with zero.

They have methods, which a primitive data type cannot have.

Summary

Now you know why Data Type is essential and why we use it. The two types of data type: primitive and non-primitive, in which primitive data types are predefined and non-primitive are user-defined. In short, we can say data types are the soul of a program.

It’s not the end!!

Learn Operators in Java to become an expert 

If you have any query or suggestion, feel free to ask in the comment box.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.