variables and datatypes

Variables and DataTypes in Java – Programming Example

variables and datatypesIn this chapter you will learn:

What is variable and DataType in java?
How many types of DataType is available in java?
Programming example of Variables and DataType in java

Variables and datatype is the basic building block of any programming language. Without it, you can’t do a single job in programming language. So, in this chapter you are going to learn very important part of Java Programming.

What is variable?

Variable is nothing just a location in memory which keeps store data on it. All the programming language needs the help of variable to store data while processing or executing program. For example, when you do a simple job in program in which you need to add two numbers and show output, you need a memory location in which you could store the value before showing it on the console.

 For Example:

Memory Location

In the above pictorial example, when you initialize an integer variable to keep a numeric value in it, it allocates a memory location and store numeric value passed by program.

What is Data Type in Java?

Data Types is basically a term which defines which type of value is going to be stored in variable. For example if you define variable with integer data type it only holds numeric value in it.

How many data types are available in Java?

There are two types of datatype available in Java – Primitive Data Type and Non Primitive Data Type

Primitive DataTypes

Primitive data types are reserved keyword and already defined by Java. Primitive data types directly hold the value. There are 8 types of primitive data types in java.


DataType Size Range Default Value
byte 8 bit signed -128 to 127 0
short 16 bit signed -32,768 to 32,767 0
int 32 bit signed -2,147,483,648 to 2,147,483,647 0
long 64 bit signed -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 0L
float single-precision 32-bit 0.0f
double double-precision 64-bit 0.0d
boolean 1 bit true or false false
char 16-bit Unicode character ‘\u0000’ (or 0) to ‘\uffff’ (or 65,535 inclusive) ‘\u0000’


Programming Example of Java DataTypes

class VariablesAndDatatypes
  public static void main(String[] args)
    byte bt=20;
    short st=30;
    int it=40;
    long lg=50L;
    float ft=60;
    double db;
    boolean bn=true;



D:\JavaProgram>java VariablesAndDatatypes

D:\JavaProgram> __


Non-Primitive Data Type

In java, non-primitive data types are simply called objects. It is sometimes called referenced variables or object references. Non primitive data type doesn’t store the value directly instead of it stores the location of variables. Strings, array etc are the non-primitive data types.



In this chapter you have learned what is variable and datatypes in java. You have also seen a complete example of java data types. In the next chapter you will learn about operators in Java.

Leave a Reply

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