Data Types in JAVA

Every variable in java have some particular data types. Data types in a programming language specify the size and type of value any variable can store. In java language, declaring the variable with the particular data type, changes can’t apply to some other data type in the complete life cycle of the program. Therefore it is called statically written language. Java is a strongly-typed language because of the following reasons:

  • Every assignment should be checked by the compiler for type compatibility.
  • Every variable and expression has some type.
  • Each and every data type is clearly defined.

Considering Java a pure Object Oriented programming language is not true, the fact is java is not pure Object Oriented programming language due to following reasons:

  • Language is still dependent on primitive data types.
  • Multiple inheritance is not supported.
  • Operator overloading is not supported.

Data types can be categorized in 2 forms as a Primitive and Non-Primitive data type.

Primitive Data Types in Java

Primitive data types are the most basic data types. They don’t have any special capabilities apart from the basic functionality. The categorization further divides into 3 types on the basis of values they can store. These are:

  1. Integral data types
  2. Non-integral data types
  3. Non-numeric data types

Classification of Primitive Data Types

There are 8 primitive data-types:

  1. byte: Its properties are:
    1. It is the signed data type.
    2. Its size is 1 byte.
    3. It ranges from -128 to 127.
    4. Possible values are integer values.
    5. Example: byte a = 3;

  2. short: Its properties are:

    1. It is the signed data type.
    2. Its size is 2 byte.
    3. It ranges from -32768 to 32767.
    4. Possible values are integer values.
    5. Example:short a = 1243;

  3. int: Its properties are:

    1. It is the signed data type.
    2. Its size is 4 byte.
    3. It ranges from -2147483648 to 2147483647.
    4. Possible values are integer values.
    5. Example: int a = 3;

  4. long: Its properties are:

    1. It is the signed data type.
    2. Its size is 8 byte.
    3. It ranges from -9223372036854775808 to 9223372036854775807.
    4. Possible values are integer values.
    5. Example: long a = 3;

  5. float: Its properties are:

    1. It is the signed data type.
    2. Its size is 4 byte.
    3. It ranges from -3.4e+38 to 3.4e+38.
    4. Possible values are real or decimal values.
    5. Example: float a = 3;

  6. double: Its properties are:

    1. It is the signed data type.
    2. Its size is 8 byte.
    3. It ranges from -1.7e+308 to 1.7e+308.
    4. Possible values are real or decimal values.
    5. Example: double a = 3;

  7. char: Its properties are:

    1. It is the unsigned data type.
    2. Its size is 2 byte.
    3. It ranges from 0 to 65535.
    4. Possible values are non-numeric values.
    5. Example: char a = x;

  8. boolean: Its properties are:

    1. It is the unsigned data type.
    2. Its size is 1 byte or 1 bit.
    3. It has the range of either true or false.
    4. Possible values are true and false.
    5. Example: boolean x = true;

Incompatible Data Types [ERROR]

The compiler will throw an error as incompatible types if assign any variable a value that is out of its range. If you want to store a value in a variable which is out of its range then you will need to do type conversion of the variable. For example:
float a = 2147483648;
This code will generate an error telling that value is too large to be represented. The correct version of this code will be
float a = (float) 2147483648;
or
float a = 2147463648L

This is all about the data types in Java.

Leave a Comment

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