Java Data Types – Primitive & Non- Primitive Data Type

1. Objective

In our last session, we discussed Java Syntax. Here, in this Java data types tutorial, we are going to learn about the various data types in Java: primitive data type and non- primitive data type.

So, let’s start the Java Data Types Tutorial.

java-data-types

2. 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 Java variable and Java keyword should be assigned a data type, they are predefined. But before we discuss Java Data Types, we must refer to Java Classes.

3. Types of DataTypes in Java

There are basically two types of Java data types-

a. The Primitive Data type

b. Non- Primitive Data type

a. Primitive Data Type

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, they are-

i. Java boolean

In Java boolean data type, 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");
         }
  }

ii. Java byte

Byte data type in Java 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.

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);
             }
    }

iii. Java char

A char data type in Java 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);
               }
       }

iv. Java short

The short data type in Java is similar to byte and is used to save memory in large arrays. It is also a two’s compliment 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);
                           }
             }

v. Java int

Int data type in Java is two’s compliment with a 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);
                }
        }

vi. Java long

The long data type in Java is two’s compliment with a 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);
                     }
          }

vii. Java float

Float Java data type is a single precision IEEE floating point with a 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);
            }
      }

viii. 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);
                        }
            }

b. Non Primitive Data Types

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.

i. Java String

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

ii. Java Array and object

An array is a group of variables that are similar in nature, they dynamically allocate. 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.

So, this was all about Java Data Types. Hope you like our explanation.

Java Quiz

4. Conclusion

In this Java Data types tutorial, we have learned about the primitive and non-primitive data types in Java and in the upcoming tutorials, we will study more about the Java Variables type. If you have any query, feel free to ask in the comment box.

For reference

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.