Various kinds of data types in Java and their utility

Various kinds of data types in Java and its utility

Java is an object-oriented, high-performance, simple language. It is distributed, portable, multi-threaded, and interpreted—mainly intended for the development of object-oriented, network-based software for Internet applications.

In 1990, Sun Microsystems began a project called Green to develop software for consumer electronics. Developer, Gosling began writing software in C++ for embedding into such items as toasters, VCR’s, and Personal Digital Assistants (PDA’s). The embedded software makes many appliances more intelligent. Gosling’s solution to the problems of C++ was a new language called Oak. In 1995, Oak was renamed Java. Since then Java has been rising in popularity. Java guarantees identical program behavior on different platforms.

In Java, Data type defines the values that a variable can take, for example, if a variable has an int data type, it can only take integer values. These are the most data types available in Java language.

There are two data types −

Primitive Data Types

In Java language, primitive data types are the building blocks of data manipulation. There are eight primitive data types supported.

These are -

 

Boolean

The Boolean data type is used to store only two possible values - true and false. This data type is used for simple flags that track true/false conditions. The Boolean data type specifies one bit of information, but its "size" can't be defined precisely.

Example: Boolean one = false

 

Char

The char data type is a single 16-bit Unicode character. Its value-range lies between '\u0000' (or 0) to '\uffff' (or 65,535 inclusive).The char data type is used to store characters.

Example: char letter A = 'A'

 

Byte

The byte data type is an example of the primitive data type. It's an 8-bit signed two's complement integer. Its value-range lies between -128 to 127. Its minimum value is -128 and a maximum value is 127. Its default value is 0. The byte data type is used to save memory in large arrays where the memory savings is most required. It saves space because a byte is 4 times smaller than an integer. It can also be used in place of "int" data type.

Example: byte a = 10, byte b = -20

 

Short

The short data type is a 16-bit signed two's complement integer. Its value-range lies between -32,768 to 32,767. Its minimum value is -32,768 and a maximum value is 32,767. Its default value is 0. The short data type can also be used to save memory just like a byte data type. A short data type is 2 times smaller than an integer.

Example: short s = 10000, short r = -5000

 

Int

The int data type is a 32-bit signed two's complement integer. Its value-range lies between - 2,147,483,648 (-2^31) to 2,147,483,647 (2^31 -1). Its minimum value is - 2,147,483,648and maximum value is 2,147,483,647. Its default value is 0. The int data type is generally used as a default data type for integral values unless if there is no problem about memory.

Example: int a = 100000, int b = -200000

 

Long

The long data type is a 64-bit two's complement integer. Its value-range lies between -9,223,372,036,854,775,808(-2^63) to 9,223,372,036,854,775,807(2^63 -1). Its minimum value is - 9,223,372,036,854,775,808 and maximum value is 9,223,372,036,854,775,807. Its default value is 0. The long data type is used when you need a range of values more than those provided by int.

Example: long a = 100000L, long b = -200000L

 

Float

The float data type is a single-precision 32-bit IEEE 754 floating point. Its value range is unlimited. It is recommended to use a float (instead of double) if you need to save memory in large arrays of floating point numbers. The float data type should never be used for precise values, such as currency. Its default value is 0.0F.

Example: float f1 = 234.5f

 

Double

The double data type is a double-precision 64-bit IEEE 754 floating point. Its value range is unlimited. The double data type is generally used for decimal values just like a float. The double data type also should never be used for precise values, such as currency. Its default value is 0.0d.

Example: double d1 = 12.3

Reference/Object Data Types

Reference variables are created using defined constructors of the classes. They are used to access objects. These variables are declared to be of a specific type that cannot be changed. Class objects and various type of array variables come under reference data type. The default value of any reference variable is null. A reference variable can be used to refer any object of the declared type or any compatible type. Reference data types are made by the logical grouping of primitive data types. Because they contain the address of a value rather than the value itself. Arrays, objects, interfaces, class etc. are examples of reference data types.

Array - An array is a one of the data structure in Java, that can store a fixed-size sequential collection of elements of the same data type.

Objects - In real-world an entity that has state and its behavior is known as an object.

Interfaces - Interfaces are implicitly abstract. Every method within an interface is also implicitly abstract. All methods of an interface are implicitly public.

Classes - A class is a template or blueprint that is used to create objects. Class representation of objects and the sets of operations that can be applied to such objects. A class consists of Data members and methods. The primary purpose of a class is to hold data/information. This is achieved with attributes which are also known as data members. The member functions determine the behavior of the class, i.e. provide a definition for supporting various operations on data held in the form of an object. Private, Protected, Public is called visibility labels. The members that are declared private can be accessed only from within the class. Public members can be accessed from outside the class also.

 

Photograph by Casimiro PT

Recommended for you