Skip to main content

Data types in Java



     The name itself defines it as Data type. It tells the compiler or interpreter that the data as belonging to this type. Let's discuss it's type in detail.


Types of data types

  1. Primitive datatype
  2. Non-primitive datatype

1. Primitive datatype

    The main reason for this type is to make the compiler knows the type of data. This is predefined and in java, it is mandatory to mention data type for the variables.

Below are the primitive data types
  • Integer
    • byte - Stores number from -128 to 127
    • short - Stores number from -32768 to 32767
    • int - Stores data between  -2,147,483,648 to 2,147,483,647
    • long - Stores data between -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
  • Float
    • float - Stores fractional number and can store upto 6 to 7 digits
    • double - Stores fractional number and can store upto 15 digits
  • boolean - Can store true of false
  • char - Can store single character or ASCII value
Example

byte a = 1;
short b = 2;
int c = 8;
long d = 345343435;
float e = 66.3f;
double f = 12E5d;
boolean g = true;
char h = 'a';

2. Non-primitive datatype

    These data types are created by programmers to ease user experience. These data types are object reference of predefined classes and also known as reference types.  Below are the types
  • Strings - This is the object reference of the class  java.lang.String. It contains a sequence of the characters
  • Arrays - This is also one of the object references in which it can store one or more values of the same data type. For more details check this Array
    • Classes - It contains variables and methods and acts as a blueprint and it describes the behavior of the object.
  • Interfaces - This is the same as a class but it contains only the method declaration. As itself is abstract it doesn't have method implementation in it.
    Post your questions in the comment to get clarified and support us to improvise us.

Comments