Data types in Java: Primitive & Non-primitive

In this article, I will explain about datatypes in Java i.e. Primitive data types and Non-primitive data types, types of Primitive data types like int, byte, short, long, float, double, char, Boolean and types of Non-primitive data types like string, class, object, array. Also I will explain about types of variables in Java i.e. Instance variable, Local variable, Static variable.

Data types define the type of data. It also defines the memory allocation of that data type.

Data types in Java

There are two types of data types.
  1. Primitive
  2. Non-primitive

datatypes in java

Primitive data types

Primitive data types are pre-defined data types which are available in most of the programming languages. It represents a single value.
There are eight types of primitive data types. Int, byte, short, long, float, double, char, Boolean.
  • Integer Data types
    It represents integer data types i.e. numbers not having any decimal values. For example, 567, -2345 etc.
    It is sub divided into below types
    Byte, Short, Int, Long
    Data TypeMemory SizeDefault valueMinimum Value Maximum ValueExample
    Byte1 byte0-128+127Byte b=2
    Short2 bytes0-32768+32767Short s=2345
    Int4 bytes0-2147483648+2147483647Int i=25
    long8 bytes0L-9223372036854775808+9223372036854775807Long p=61234

  • Float data types
    This data type is use to save decimal point numbers. For example, 1.5, -456.56 etc.
    Float data types are divided into two sub types i.e. float and double.
    Data TypeMemory SizeDefault valueMinimum ValueMaximum ValueExample
    Float4 bytes0.0f-3.4e38 to -1.4e-451.4e-45 to 3.4e38Float x = 4.4f
    Double8 bytes0.0d-1.8e308 to -4.9e-3244.9e-324 to 1.8e308Double r=23.678

    Difference between float and double:
    Float represent up to 7 digits accurately after decimal point and double represent 15 digits accurately after decimal point.

  • Character data type
    Char is used to store character data type. It stores only one character at a time. For example, x, & etc.
    Data TypeMemory SizeDefault valueMinimum ValueMaximum ValueExample
    char2 bytes'\u0000'065535Char c='t'

  • Boolean data types
    Boolean data type has two values i.e. true and false.
    Example: Boolean b=false;

Non-primitive data type

Non-primitive data types are created by programmer. It is also called as 'Reference Variables' or 'Object reference' because it refers a memory location where data is stored. On the other hand, primitive data types stores value.
  • String
    • It is class in java provided with several methods used to store group of characters.
    • For example, abcd, india123 etc.
    • String is not a primitive data type because it has methods and only class can have methods. Primitive data types cannot have methods. So, String is a class which is non-primitive type.
    • 'String' is used to represent string class.
    • Example: String s="World"
  • Objects and Array
    Objects and Array are non-primitive data types because it refers to the memory location

Variables in Java

Variable is a name of memory location. Each variable has specific data type which determines size of memory.
There are three types of memory location.
  1. Instance variable:
    Variable which is defined within a class but outside the method is called as instance variable. Because each instance of class contains its own copy of these variables.

  2. Local variable:
    It is defined inside the method.

  3. Static variable:
    A variable which is declared as a static is called as static variable.
    public class PrintProgram {
        static int b;
        public static void main(String[] args) {
            int i;
        static void print(){
            int x=1;

In above example, 'x' is a local variable, 'b' is a static variable and i is an instance variable.


Related Articles

Creating new JEE 5 applications (Java and EJB Web Services) using RAD 7.5

Now days, most of the enterprise applications are developed in RAD 7.0+ (Rational Application Developer). The Web service application can be written in Java or in EJB. For creating Web services in Java or EJB, the vast documentation is available. Referring such documentation during speedy development period becomes a hectic job. So to avoid it, this article will help to easily develop a JEE5 application using simple steps.

Program for Addition performer in Java

This article provides a description to the addition performer in Java, that can perform addition operation on two input numbers. Code for the addition performer is provided in the article. The program is based on use of Swings. In Java Frames can be created and different text fields alongwith labels corresponding to them can be added. And use of all these elements can develope an efficient adder.

What is difference between an Interface and Abstract class in Java?

Are you looking for an article which explains the concepts of an Interface and Abstract class in simple language? Then your search ends here. Read this article, herein the detail about abstract class and an Interface is given. Also the important differences between an Interface and Abstract class is in Java is explained. This article highlights the important aspects regarding whether abstract class to be used or an Interface is to be used for given requirement of Enterprise application.

Directory Structure - XML Output

In this article, I have submitted a Java program. We will be able to get the dynamic XML file with treeview represents a folder and file structure of a given folder path using it. This Java program also show how to use a recursive function to read a directory structure and then save it to XML file. I commented properly for your better understanding how the program flow goes. The output XML file is well-formed

More articles: Java


No responses found. Be the first to comment...

  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name: