maxtek IoT

Data type

The objective of a Java program is to manipulate data. The programmer should specify to the compiler which type of data he wants to manipulate.

Different programs manipulate different data. A program designed to calculate a student’s grade will add, multiply, and divide numbers. Some of the numbers might represent mid semester grade, final exam grade and assignment grade . Similarly, a program designed to alphabetize a class list will manipulate names. Similarly, you would not manipulate alphabetic characters with a program designed to perform arithmetic calculations. Furthermore, you would not multiply or subtract names.With regard to this uniqueness, Java categorizes data into different types, and only certain operations can be performed on a particular type of data.

So a data type can be defined as a set of values together with a set of operations on those values.

Primitive data type

The primitive data types are the fundamental data types in Java. There are of three categories:
  • Integral, which is a data type that deals with integers, or numbers without a decimal part and characters. To declare an integral data type, we use the reserved word int which stands for an integral number or a whole number before the variable name.
  • Floating-point, which is a data type that deals with decimal numbers. To declare an floating point data type, we use the reserved word float before the variable name.
    float grade = 0;
  • Double, which is a data type that deals with decimal numbers. To declare an double data type, we use the reserved word double before the variable name.
    double score = 78;
  • Boolean, which is a data type that deals with logical values. To declare a boolean data type, we use the reserved word boolean before the variable name.
    boolean isTrue= false;
  • Character, which is a data type that deals with letter such as alphabet or a symbol To declare a boolean data type, we use the reserved word char before the variable name and the data is always placed between a single quote.
    char symbol = 'c';

    Caution


    Java provides two data types to represent decimal numbers: float and double. As in the case of integral data types, the data types float and double differ in the set of values. float: The data type float is used in Java to represent any real number between 3.4E+38 and 3.4E+38. The memory allocated for the float data type is 4 bytes.
    double: The data type double is used in Java to represent any real number between 1.7E+308 and 1.7E+308. The memory allocated for the double data type is 8 bytes.
    Other than the set of values, there is one more difference between the data types float and double. The maximum number of significant digits that is, the number of decimal places in float values is 6 or 7. The maximum number of significant digits in values belonging to the double type is typically 15.
    The maximum number of significant digits is called the precision. Sometimes float values are called single precision, and values of type double are called double precision.