In this tutorial, we will discuss Data types in Java and its first type Primitive with examples. Non-Primitive data type we will discuss in the next tutorial. I will try to understand you in very easy ways so that you do not get any problem to learn data type in Java.

data types in java,Java data types with example,primitive data types with examples,learn java data types

Data Types in Java

Data types are the types of data for which the memory space is allocated for the variables. In other words, Data type defines the different values that variable can take. 
For example:
                       String name="Deep"; // Here String is a Datatype and name is a variable which can take only string values. 
                       int num=10; // Here int is Data type and num is variable which can only take integer values like 10, 20, 30, 40 and so on.

The semicolon is used to end a particular statement in Java like pull stop in the English language so that Java will know that statement is completed.

There are two data types in Java. They are:
1. Primitive data types
2. Non-Primitive data types or Reference data types or object type

Primitive Datatypes

Primitive Data types are predefined in Java. They are named by a Keyword.
Primitive data types are those data types whose variables can store only one value at a time. You can store multiple values of the same type.
For example:
                      int x; // valid
                        x=10; // valid because "x" store only one value at a time because it is the primitive type variable.
                        x=10, 20 ,30 40; // invalid

Primitive data types are not user-defined data-types. i.e Programmers cannot  develope primitive data types. 

Types of Primitive Data types in Java

There are eight primitive data types in Java and they are further categorized as given below:
Conditional category data types
            ➲  boolean               
Character category data type
            ➲  char
Integer category data types
            ➲  byte
            ➲  short
            ➲  int
            ➲  long
Float category data type
            ➲  float
            ➲  double
Note: All the data types are the keywords which are predefined in Java. They are in small letters. So keep in mind.

Now, there is very important to understand the memory limitations which decides that which data type should be used for a particular number. For example,  when you define the age of a person, the age of any person will not cross 120. So in this case, using short data type is enough instead of using long which will take a big memory. Therefore, first, you understand these things for every data types.
      1. Memory size allocated
      2. Default value
     3. Range of values

Memory size allocated

Each data type has some memory size defined in Java. Whenever a variable is declared with a data type, the memory size is automatically defined in the RAM by the JVM. 
For example:
                      If I write int a; then the size of the memory is defined as 4 bytes.

Default value

Every primitive data type has default values defined. When the programmer does not declare any value to the variables, default values will be assigned by the JVM during the object creation.
For example:
                      The default value of int is 0(zero).
                      The default value of the byte is 0.

Range of values

The range of value represents the min. or max. values that a data type can hold. When we assign the value of a variable which is not fit in the range of data type, error or exception will come.
For example:
                     The range of int data type is between the minimum value -2,147,483,648(2^31) and the maximum value +2,147,483,648(2^31-1).

Note: All the data types represent both positive and negative numbers. In the programming language, Zero is considered as a positive number.
Let's understand all the data types one by one.


 A byte is the first data type which has the least memory size allocated and it is mostly used to save memory.
Byte data type is an 8-bit signed two's complement integer. 
The memory size of this data type is 8-bit i.e 1 byte.
byte represents from 0 to 127 on the positive side and from -1 to -128 on the negative side. So it can represent the total 256(2^8) numbers.
The default value is 0.

    class ByteExample { public static void main(String[] args){ byte num=100; // byte is 8 bit value. System.out.println(num); } }
    Output: 100
In above example, when you will assign 140 value to variable num, you would get the type mismatch error because the value is out of the range of byte data type. The range of byte is -128 to +127. 


Short data type has greater memory size than byte and less than Integer.
A short data type is a 16-bit signed two's complement integer.
The default memory size is allocated 16 bit i.e 4 bytes.
It represents 0 to 32767 on the positive side and on the negative side, from -1 to -32768. So it can represent a total of 65536(2^16) numbers.
The default value is 0. 

    class ShortExample { public static void main(String[] args){ short num=200; // short is 16 bits value. System.out.println(num); } }
    Output: 200
You could not use byte data type in the above example because a byte cannot hold 200 value. but short can hold the value 200 because of its wider range.


This data type is mostly used for integer values in the Java programming.
Int data type is a 32-bit signed two's complement integer.
It has wider range from -2,147,483,648 to 2,147,483,648.
The memory size is 32 bits i.e 4 bytes.
The default value is 0.
➝ Example:

    class IntegerExample { public static void main(String[] args){ int a=200; int b=300; System.out.println(a+b); } }
    Output: 500


This data type is mostly used for the huge number.
A long data type is a 64-bit signed two's complement integer.
The default memory size allocated to this data type is 64 bits i.e 8 bytes. 
The default value is 0.
It has wide range from -9,223,372,036,854,775,808(-2^63) to 9,223,372,036,854,775,808(-2^63-1).
               long num=-2334456L;


A float data type is used to represent the decimal number which can hold 6 to 7 decimal digits.
It is used to save the memory in large arrays of floating point numbers.
The float data type is a single-precision 32-bit IEE 754 floating point.
The default memory size allocated for this data type is 32 bits i.e 4 bytes.
The default value is 0.0f.
                     float num=10.6f; // Here if you do not write 'f'' then JVM will consider as double and it will give " Type mismatch: cannot convert from double to float".


A double data type is also used to represent decimal number up to 15 decimal digits. 
The double data type is a double-precision 64-bit IEE 754 floating point.
Memory size is 64 bits i.e 8 bytes.
Default value is 0.0d.
                   double num=1345.6;


char data type is mainly used to store any characters.
Memory size taken by char is 2 bytes.
It is a single 16-bit Unicode character.
It can represent around 65536 characters.
A default value for char is '\u0000' which represents blank space or single space.
➝ '\u' represents that character is a Unicode.
➝ Example:
                    char ch='D';


boolean data type represents one bit of information either true or false. i.e there is only two possibles value true or false.
It is used for the conditional statement.
It takes zero bytes of memory. 
Default value is false.
                    boolean b=false;

Note: In the above all examples, we assigned a value of the variable and it will print output. Suppose if you do not assign the value of the variable in all examples like this:
            int num;
            char b;
So what will be output here?
If you assign the value of variable then the assigned value will be printed but if you do not assign any value of the variable then JVM will assign the default value and the default value will be print. (Always remember this point).

    class DefaultExample { // Declaration of Instance Variables. int a; char b; float c; // Static method or main method. public static void main(String[] args){ // Create the object of the class. DefaultExample obj=new DefaultExample(); // Call the variable and print it. System.out.println(obj.a); System.out.println(obj.b); System.out.println(obj.c); } }
    Output: 0 \u0000 0.0

Why take boolean data types zero bytes of memory?

Boolean data type takes zero bytes of memory space because boolean data type in Java is implemented by Sun Micro System using the concept of a flip-flop. A flip-flop is a general purpose register which stores one bit of information(one for true and zero for false).

Different ways to initialize the values and its output

1. int a=10; // Initialization.
    output: 10

2. int a, b, c; // Initialization.
    Output: o, o , o

3. int a=20, b, c; // Initialization.
     Output: 20, 0, 0

4. int a=10, b=20, c; // Initialization.
      Output: 10, 20, 0

5. int a=10, b=20, c=30; // Initialization.
      Output: 10, 20, 30

6. int a, b, c=100; // Initialization.
     Output: 0, 0, 100

7. int a, b=100, c=200; // Initialization.
     Output: 0, 1oo, 2oo
Final words:
I hope this article will help you to understand the Primitive Data types in Java with Examples. Actually basic is very important to learn the Java. We will always try to provide you all concept of Java from basic to advanced. 

Other topics in Data types:
1. Non-Primitive Data-types with examples
2. Memory allocation of Primitive and Non-primitive Data-type

Next ➝ Non-primitive Data types                                                              Scientech Easy loves you