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 in Java programming.

So, let’s start Java Data Types Tutorial.

Java Data Types - Primitive and Non Primitive Data Type

Java Data Types – Primitive and Non Primitive Data Type

2. Java Data types

Any programming language can be of two types, viz, 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, they cannot be changed 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 Java Syntax and Java Classes.

3. Types of Data Types in Java

There are basically two types of Java data types-

a. The Primitive Data type in Java

b. Non- Primitive Data type in Java

a. Primitive Data Type in Java

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

There are 8 primitive data types in Java, they are-

i. Java boolean

In a java boolean data type, only one bit of information can be represented, 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");
         }
  }

Let’s discuss Packages in Java

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 bit 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’ 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 bit size of 16 bit and a range of -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 range of-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 bit and a range of -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);
                     }
          }

Let’s discuss the Multithreading in Java

vii. Java float

Float Java data type is single precision IEEE floating point with a memory allocation of 32 bit 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 create.

ii. Java Array and object

An array is a group of variables that are similar in nature, they dynamically allocate and can contain both primitive or objects depending on the definition. Their size has to specify in the only int, and their length can be found out by member length. Their indexing always starts with zero, and they order.

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

To learn Java Operators and Java Modifiers follow this link.

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

4. Conclusion

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

Best Java Books for Learn java language.

For reference

Leave a Reply

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