Data types in Java

The data types in java specify the sizes and values stored in the variables.

Types of Data types in Java

  1. Primitive data types
  2. Non-primitive data types

Primitive data types : A primitive datatype is a predefined bt the programming language. The size and the type of variable values are specified and its has no additional methods. These primitive data types include byte, short, int, long, float, double, char and boolean.

  • Byte : The byte data type is smallest datatype among all datatypes. The width of byte data type is 1 byte that is 1 byte is equal to 8    bits.The range of byte datatype is -128 to  127.The default value of byte datatype variable is zero.
    class Test 
    {
       public static void main(String[] args)
       {
          byte a = 10;
          System.out.println(a);
       }
    }

    Output :

    10
    
    === Code Execution Successful ===
  • Short : The short datatype width is 2 bytes,that is 16 bits.The range of short datatype is -32768 to 32767.The default value of short datatype variable is zero.
    class Test
    {
       public static void main(String[] args)
       {
         short a = 32767;
         System.out.println(a);
       }
    }

    Output :

    32767

    === Code Execution Successful ===

 

  • Int : The width of int datatype is 4 bytes that is 32 bits. The default value of int datatype is zero.The range of int datatype is -2,147,483,648 to 2,147,483,647.
class Test 
{
  public static void main(String[] args)
  {
    int a = 3276790;
    System.out.println(a);
  }
}

Output


3276790

=== Code Execution Successful ===
  • Long :  The width of long datatype is 8 bytes that is 64 bits. The default value of long  datatype is zero.The syntax of long datatype is long variable name.

 

class Test 
{
  public static void main(String[] args)
  {
    long a = 327679087;
    System.out.println(a);
  }
}

Output

 :

327679087

=== Code Execution Successful ===
  • Float : The width of float datatype is 4 bytes that is 32 bits. The approximate range of float datatype is  1.4 * 10 ^045 to 3.4 * 10^38.The syntax of long datatype is float variable name.
class Test 
{
  public static void main(String[] args)
  {
    float a = 13.56f;
    System.out.println(a);
  }
}

Output :

13.56f

=== Code Execution Successful ===

Double : The width of the datatype is 64 bits that is 8 bytes.the syntax of  double is double variable name.

class Test 
{
  public static void main(String[] args)
  {
    double a = 13.456789;
    System.out.println(a);
  }
}

Output

 :

13.456789

=== Code Execution Successful ===

Non-primitive data types : The non-primitive data types include Classes, Interfaces and Arrays.

interface Bird {
    void makeSound();
    int getNumberOfLegs();
}

class pigeon implements Bird {
    public void makeSound() {
        System.out.println("kech kech kech!");
    }

    public int getNumberOfLegs() {
        return 2;
    }
}
public class Main {
    public static void main(String[] args) {
        
        Bird pigeon = new pigeon();
        pigeon.makeSound(); 
        System.out.println(pigeon.getNumberOfLegs()); 
       
    }
}

Output

 :

kech kech kech!
2

=== Code Execution Successful ===

Leave a Comment

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

Scroll to Top