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

Free Java courses with 37 real-time projects - Learn Java

Variables store data. But a fundamental problem arises as to what kind of data is the variable storing? There can be a lot of confusion about the computation of a program if the variables’ individual data types are not known. Hence the concept of data types arises. In this tutorial, we will learn Java Data Types with examples.

Java Data Types

Every individual bit of data that is processed every day is categorized into types. The type of data is known as datatype. Java uses various kinds of data types.

However the data types are mainly of two categories:

a. Primitive Data Types- These data types are already hard coded into the compiler to be recognized when the program is executed. Examples are- int,float etc.

b. Non-Primitive Data Types- These data types are special types of data which are user defined, i,e, the program contains their definition. Some examples are- classes, interfaces etc.

1. Primitive Data Types in Java

Java primitive data types are the ones which are predefined by the programming language which in this case is Java. Without primitive data types it would be impossible to frame programs. Primitive data types are also the building blocks of Non-primitive data types.

Java Primitive Data Types

There are 8 types of Java primitive data types namely:
a. Int
b. Float
c. Char
d. Boolean
e. Byte
f. Short
g. long
h. Double.

a. Integer Datatype in Java

int is used for storing integer values. Its size is 4 bytes and has a default value of 0. The maximum values of integer is 2^31 and the minimum value is -2^31. It can be used to store integer values unless there is a need for storing numbers larger or smaller than the limits

Example- int a=56;

b. Float Datatype in Java

float is used for storing decimal values. Its default value is 0.0f and has a size of 4 bytes. It has an infinite value range. However its always advised to use float in place of double if there is a memory constraint. Currency should also never be stored in float datatype. However it has a single precision bit.

Example- float a=98.7f;

c. Character Datatype in Java

char as the name suggests is useful for storing single value characters. Its default value is ‘\u0000’ with the max value being ‘\uffff’ and has a size of 2 bytes.

Example- char a=’D’;

It must be confusing for you to see this new kind of data ‘/u000’. This is the unicode format which java uses inplace of ASCII.

d. Boolean Datatype in Dava

boolean is a special datatype which can have only two values ‘true’ and ‘false’. It has a default value of ‘false’ and a size of 1 byte. It comes in use for storing flag values.

Example- boolean flag=true;

e. Byte Datatype in Java

It’s an 8 bit signed two’s complement . The range of values are -128 to 127. It is space efficient because it is smaller than integer datatype. It can be a replacement for int datatype usage but it doesn’t have the size range as the integer datatype.

Example- byte a = 10;

f. Short Datatype in Java

This datatype is also similar to the integer datatype. However it’s 2 times smaller than the integer datatype. Its minimum range is -32,768 and maximum range is 32,767. It has a size of

Example- short a= 54;

g. Long Datatype in Java

This datatype primarily stores huge sized numeric data. It is a 64 bit integer and ranges from -2^63 to +(2^63)-1. It has a size of 8 bytes and is useful when you need to store data which is longer than int datatype.

Example- long a= 1273762;

h. Double Datatype in Java

This is similar to the float datatype. However it has one advantage over float datatype i.e, it has two bit precision over the float datatype which has one bit precision. However it still shouldnt be used for precision sensitive data such as currency. It has a range of -2^31 to (2^31)-1.

Example- double DataFlair=99.987d;

Java program to illustrate the different types of datatypes:

import java.io.IOException;
class TypeVariable

<class names should be meaningful and never start with lowercase letter, refer the naming convention of a class>

{
  public static void main(String[] args) throws IOException {
    int a = 10;
    short s = 2;
    byte b = 6;
    long l = 125362133223l;
    float f = 65.20298f;
    double d = 876.765d;
    System.out.println("The integer variable is " + a);
    System.out.println("The short variable is " + s);
    System.out.println("The byte variable is " + b);
    System.out.println("The long variable is " + l);
    System.out.println("The float variable is " + f);
    System.out.println("The double variable is " + d);

  }
}

Output

The integer variable is 10
The short variable is 2
The byte variable is 6
The long variable is 125362133223
The float variable is 65.20298
The double variable is 876.765

2. Non-primitive Data Types in Java

These are the datatypes which have instances like objects. Hence they are called reference variables. They are primarily classes, arrays, strings or interfaces.

Java Non-Primitive Data Types

a. Classes in Java

These are the special user defined data type. It has member variables and class methods. They are blueprinted by objects.

Java program to illustrate classes:

class DataFlair {
  int a,
  b,
  c;
  void teach() {
    System.out.println(“Hi I am teaching at DataFlair”);
  }
  void learn() {
    System.out.println(“Hi I am learning at DataFlair”);.
  }
}

b. Interfaces in Java

These are similar to classes. However there is one prime difference, i,.e the methods are abstract by default. i.e, they have no body.
Similarly, like objects, interfaces are also the blueprints of a class.

If the class implements an interface, then it is supposed to add detail to every function of the interface. If not, then we must declare the class as abstract.

Example program to illustrate interfaces in Java:

interface DataFlair {
  void teach();
  void learn();
}
class DataFlairCls implements DataFlair {
  public void teach() {
    System.out.println("I teach at DataFlair");
  }
  public void learn() {
    System.out.println("I learn at DataFlair");
  }

  public static void main(String[] args) throws IOException {
    DataFlairCls ob = new DataFlairCls();
    ob.teach();
    ob.learn();
  }
}

c. Strings in Java

You may be knowing string as a collection of characters but in Java String is a completely different class altogether. It’s located in java.lang.String. However, strings end with a ‘\0’ character.

Java has a lot of methods for manipulating strings such as substring, length and many more.

Example:
String s=”DataFlair is a fun place to learn”;
String sub=s.substring(0,9);
System.out.println(sub);

Output:

DataFlair

d. Arrays in Java

Arrays are special memory locations that can store a collection of homogeneous data. They are indexed. Arrays always start indexing from 0. Dynamic allocation of arrays is there in Java. Arrays in Java can be passed as method parameters, local variables and static fields.

Example- int arr[] = new int[100];

This creates a storage space for 100 integers.

Java program to explain the concept of arrays:

import java.io. * ;
import java.util. * ;
class DataFlairArray {
  public static void main(String[] args) throws IOException {
    int i;
    Scanner sc = new Scanner(System. in );

    int arr[] = new int[] {
      1,
      2,
      3,
      4,
      5
    };
    int arr1[] = new int[5];
    for (i = 0; i < 5; i++) {
      System.out.println("Enter your number");
      arr1[i] = sc.nextInt();
    }
    System.out.println(“The array which was previously assigned”);
    for (i = 0; i < 5; i++) {
      System.out.println(arr[i]);
    }
    System.out.println("");
    System.out.println("The array you entered is:");
    for (i = 0; i < 5; i++) {
      System.out.println(arr1[i]);
    }
  }
}

Output:

Enter your number
4
Enter your number
1
Enter your number
2
Enter your number
3
Enter your number
4The array which was previously assigned:1
2
3
4
5The array you entered is:
4
1
2
3
4

Java Data Types Quiz

Summary

Summing up, these are major league Data types in Java.Without data types, it would be impossible to classify different varieties of inputs and variables. Hence a solid concept of data types is must for fluid coding in Java.

Hope you created a string base with this Java Tutorial. Do not forget to share feedback in the comment section.

You give me 15 seconds I promise you best tutorials
Please share your happy experience on Google

follow dataflair on YouTube

7 Responses

  1. Rajani says:

    please correct spelling of byte… it is written as btye in pictorial representation of “Types of Data Types in Java”

  2. Abubakar says:

    I like this

  3. devendra says:

    quiz question color is not changing in any tutorial

  4. d.ramya says:

    it’s very usefull for me sir and you have exaplande detailed information for data types

  5. Hima says:

    Boolean Datatype in Dava……….

    Onec check this “Dava” this correct word

Leave a Reply

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