Sunday, May 15, 2016

Data Type

Java defines eight simple data types: byte, short, int, long, char, float, double, and boolean. They are called as primitive data types. The types then grouped into several groups: integer type, floating-point type, character type, and boolean (logical) type.

  • Integer. This group consists of byte, short, int, and long type; integer is round, for example: -5, 0, 4, 100, etc.
  • Floating-point. This group consists of float and double type; used to represent the real numbers (containing fractions), for example, -12.34, 0.65, 3.1416, 10.654, and so forth.
  • Character. The group consists only one data type: char, which represent data in the form of alphanumeric characters and symbols, such as 'a', 'A', '1', '4', '$', '#', and so forth.
  • Boolean. This group also consists only one data type: boolean, used to represent a logical value (true/false)

As explained previously, the simple types above is the basic form of all other complex types, such as array and object type; which will be studied in next post.

Simple data type can only store a single value, not the complex object. Although Java fully supports object-oriented programming model, but this does not apply to simple data types. These types did not differ with the simple data type that contained in most non-OOP (procedural) programming language.

Other programming languages, such as C and C++ allow integer size that varies depending on the execution environment to be used, whereas Java has a different concept. In Java, all data types are in the fixed range for each environment/platform or different machine architecture. For example, int type size is always 32 bit. The reason is that programs can run on all platforms without having to change the code contained in it.


1.  Integer Type


In the previous section, you see that the integer types divided into four types: byte, short, int, and long. All these types are signed, which can represent a positive or negative value. Unlike most other programming languages such as C, C++, Pascal, and others, Java does not support unsigned value, which can only store positive values.

You can see the size and range of integer types in Java in the table below.

  Data Type   Size (bit)   Range
  byte   8   -128 to 127
  short   16   -32,768 to 32,767
  int   32   -2,147,483,648 to 2,147,483,647
  long   64   -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807


1.1  byte Type

This type is the smallest type of integer type. byte type usually used when we are working with a data stream of a file or network: for reading/writing process. In addition, this type is used when we are working with binary data that is not compatible with other types defined in Java.

To declare a variable with byte data type, we need to use byte keyword. Here is an example of the declaration of three variables with byte type.

byte a;
byte b, c;


1.2  short Type

This type is the type of 16-bit value that is in the range -32,768 to 32,767. In general, this type is applied to 16-bit computers, which is increasingly rare existence. Here is an example of declaring a variable with short type.

short s;


1.3  int Type

This type is the most widely used type to represent an integer value because it is considered the most efficient in comparison with other integer types. int type is widely used for the index in the loop structure as well as in the construction of an array. Moreover, in theory, every expression involving integer types (byte, short, int, and long) will be promoted to int type before the calculation process is done. For example, consider the following program code:

Class CalculationExample {
   public static void main(String[] args) {
      byte a = 1;
      short b = 2;
      int c = 3, d;
      d = a + b + c;
     System.out.println("Result = " + d);
   }
}

The following result will be displayed on the screen:

Result = 6

In this case, internally, variable a (byte) and b (short) will be promoted to int type before the calculation.


1.4  long Type

This type is used for certain cases that the value is outside of int type range. Here is an example of using long data type to declare a variable.

long numberOfSeconds;
long distance;


2.  Floating-Point (Real) Type


Floating-point type used to represent values that contain fractions or decimal digits after the decimal point, such as: 3.1416, 5.25, and so on. Such numbers are often called the real numbers. In Java, a floating-point type can be divided into two types: float and double. Examples of simple cases that require this data type is calculate the area and circumference of a circle and calculations that involve trigonometric (e.g. sine and cosine).

The size and range of values of the two types are shown by the table below:

  Data Type   Size (bit)   Range
  float   32    3.4e-038 to 3.4e+038
  double   64    1.7e-308 to 1.7e+308


2.1  float Type

This type is used to indicate values that contain single-precision or accuracy that use 32-bit storage space. Single precision usually faster for certain processors and takes up half times storage space than the double precision (double precision). The problems that will arise is when we use the float type for values that are too small or too large, because the resulting value will be inaccurate.

You can see examples of variable declaration using float type in the code below:

float x, y;
float temperature;



2.2  double Type

This type contains double precision level and uses 64-bit storage space to store the value. In modern processors that has been optimized to perform mathematical calculations at high speed, the double type actually will be faster than the float type. All mathematical functions in Java, such as sin(), cos(), and sqrt() will return a double type value. If you want to perform calculations that involve real numbers and need high accuracy level results in your program, then double type would be the best choice.

Here is a sample code that demonstrates the use of double type in the program:

class Circumference {
   public static void main(String[] args) {
      double pi = 3.1416;
      double r = 2.12;
      double circumference;
      circumference = 2 * pi * r;
      System.out.println("Circumference = " + circumference);
   }
}

The following result will be displayed on the screen:

Circumference = 13.320384


3.  Character


Same with other programming language, in Java, data type used to store character value is char. Here is a sample code that demonstrates the use of char type in the program:

class CharacterDemo1 {
   public static void main (String[] args) {
      char ch1 = 'B';
      System.out.println("ch1 = " + ch1);
   }
}

The following result will be displayed on the screen:

ch1 = B


4.  Boolean Type


Boolean type is the type used to store the logic values, which is only two possibilities: true or false. This type is using boolean keyword. Here is an example of using boolean data type to declare a variable:

boolean a, b;
a = true;
b = false;

boolean type is the type returned by all relational operators, which is always required when we write the expression/condition in the decision making and loop structure, such as: if, for, and while.

3 comments:

  1. Nice wonderful article
    Here also easy article to understand and learn some another language
    Java DataType

    ReplyDelete
  2. Thank you for sharing valuable information with us.

    Java web application is a type of free programming language used to develop applications and software that are becoming highly in demand on the internet. The software engineer can provide the web application or software in the form of windows, Linux, and MAC OS. There are various types of systems that can not work without java and it is getting necessary day by day even the world’s no.1 application depends on java. Java has become a most popular feature in android smartphone applications that's why Aptech learning provides java training in Dwarka, Janakpuri, and in Gurgaon also. Java training helps to get you complete knowledge about web frameworks, Java testing tools, and design patterns in java.

    Let's get learn about the PHP training course
    PHP is a type of programming language that is most commonly used in dynamic functions such as modifying, collecting, and deleting all types of databases It also helps to encrypt all types of data in the form of HTML and XML file. Aptech learning provides you with a complete PHP training course at a reasonable cost that will help you to grow more in the era of the software industry.

    For more details please visit our website : Aptech Janakpuri

    ReplyDelete