Data Types in Java

As an important step in your journey to be an expert of Java, in this blog, we will learn about data types which are there in java. This is a complete guide which will inform you about the reason why data types are required, the syntax to work with them, the size of memory that they occupy, classifications of the data types, type casting and the range of values that they can contain.

What do you mean by data types?

The data which has to be handled and stored while programming is of various kinds, hence, to ensure storage of data in proper memory locations and assignment of adequate space to the variables, it is essential to specify the type of data that is being assigned to the variables.

Data types identify the kind of data that is being assigned to a variable.

  • On the fundamental level, java compilers handle data in the form of tokens. To manage these tokens, data types are required.
  • In Java, data types are classified into two categories: Primitive data types and Non Primitive data types. Primitive data types are the built-in data types available in Java. Non-Primitive data types are user defined data types.
  • According to the specified data types, the compiler works on allocating the memory to the data, hence, it is important to specify the data types of variables so that they can easily be accommodated into a stream of tokens.
  • Type conversion is possible in Java. It’s of two kinds: Implicit and explicit.

Classification of Java Data Types

Below is the classification of data types in java:

 

a. Primitive Data Types:-

These are the 8 basic data types in Java that are independent. These are built-in data types as they are predefined. The 8 primitive data types are:-

i. Byte
We use this data type for bit-wise operations and it can accommodate 8 bits of data.

Syntax:
byte <variable name>;

Example of Byte
byte var=0;

Range:
-128 to 127(whole numbers)

Default value: 0

ii. Short
We use this data type to specify a shorter range of integers. It can accommodate 16 bits or 2 bytes of data.

Syntax:
short <variable name>;

Example of Short
short var=2;

Range:
-32,768 to 32,767(whole numbers)

Default value: 0

iii. Integer
We use this data type to specify integers but for a bigger range than short data type. It can accommodate 4 bytes of data.

Syntax:
int <variable name>;

Example of Int
int var=45;

Range:
-2147,483,648 to 2147,483,647(whole numbers)

Default value: 0

iv. Long Integer
We use this data type for integer values but it’s range is bigger than the integer data type. It can accommodate 8 bytes of data.

Syntax:
long <variable name>;

Example of Long
long var=22;

Range:
-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807(whole numbers)

Default value: 0

Now, we have the floating point numbers:-

v. Float
This data type specifies floating point values. It can accommodate 4 bytes of data.

Syntax:
float <variable name>;

Example of Float
float var;

Range:
Decimal numbers upto 7 decimal places.

Note:- The value assigned to a float variable should be followed by the letter ‘f’.
Example of float;
float var=24.01f;

Default value= 0.0

vi. Double
This data type is used to specify decimal numbers or fractional numbers but its range is bigger than the float data type. It can accommodate 8 bytes of data.

Syntax:
double <variable name>;

Example of double
double var=24.10;

Range:
Decimal numbers upto 15 decimal places.

Default value= 0.0

vii. Character
This data type specifies the data which includes a single character. It can accommodate 2 bytes of data. In Java, Characters aren’t stored corresponding to ASCII codes. In Java, characters are used with reference to unicode and 2 bytes of data is needed to store them.

Syntax:
char <variable name>;

Example of Char
char var;

Range:
Single character or ASCII values.

Note:-
The character value assigned to a variable of character data type should contain single quotes.

Example of char:
char var=’a’;

Default Value:” /u000″

viii. Boolean
We use this data type to store true or false values. It can accommodate only a single bit of data.

Syntax:
boolean <variable name>;;

Example
boolean var;

Range:
true or false value.

Note:- The value assigned to the boolean variable should contain the keyword “true” or “false” without quotes.

Example of Boolean:
boolean var=true;

Default value: false

Why are data types which store the same kind of data but have smaller storage capacity included in Java?

The data types which have smaller storage capacity but store the same kind of data are required because they help to minimize the wastage of memory. When a user knows that he won’t need more memory than a specific amount, he can use data types that reserve lesser memory and in this way, they can ensure that the memory space is utilized efficiently.

b. Non- Primitive data types in Java

These data types are dependent upon the primitive data types in some or other way. Hence, they are also known as derived data types. These data types store data in a way that allows them to store a reference value to the location of that specific data in the memory.

Classes, Arrays and Objects are some examples of Java User-Defined Datatypes.

i. Class in Java

A class is a collection of properties that all the objects belonging to it share. It has a basic format to which every object should adhere to. We declare a class using the keyword class followed by the class name.

We use modifiers sometimes to control the kind of access to a class. They are also known as access specifiers.
Instances tell what all a class has to implement. They don’t have a facility to tell how it processes.

If some class is a child class of another class, that class extends it’s parent class. We do it by using the keyword “extends”. The body of a class is enclosed within brackets and it contains all the functions and constructors that are being used in that class.

Example of Java Class:

public class FirstCode
{
  int var=22;
}

ii. Array in java

Arrays are a non-primitive data type that can store a number of data items of the same data type. We refer to the data items in an array, using index numbers that start from 0. We declare an array just like any other variable, it should be followed by [].

Arrays are allocated dynamically in Java, so they are objects and we find their size using the function:- length.
Object is the direct superclass of an Array. Every Array in Java implements two interfaces. They are java.io.Serializable and Cloneable.

Example of Java Arrays;

Int[] Arr={2,3,4,5};

iii. Objects in java

Objects represent real-world entities. They have a specific set of attributes and some behaviors of their own. The state of an object specifies its properties of it.

We mark the behavior of an object by the methods which it uses to interact with other objects. The name of each object is its unique identity.

Example of Java Objects:

public class FirstCode
{
  public void main(String[] args)
   { FirstCode A=new FirstCode;//this is an object
        }

iv. Interface in Java

We use an interface to identify what all a class is implementing. It only contains the function signature of every method in a class, it does not have anything in the body of those methods.

We consider a class abstract if all the functions that are declared in its interfaces do not have a body inside the class.

Example of Java Interface:

Interface XYZ
{
   public void calcadd();
   public void calcmult();
                     }
 class calc implements XYZ
 {
  public void calc add()
   {
    //body of the function here
   }
  Public void calcmult()
  {
    //body of the function here
      }
       }

v. Strings in Java

String is the data type that stores a sequence of characters in it. There are a lot of String functions that are available in Java, we use these functions to manipulate the Strings according to the user’s need.

Syntax:
String <variable name>=”<characters>”;

Example of Java String:

String var=”FirstCode”;

Type Conversion in Java

There are instances where you need to convert a certain type of data into another type. When you have an expression that contains variables with different data types, you need to convert it to the same data type, so that the machine can evaluate it. For requirements like these, we have the facility to convert one data type to another.

We address this as type conversion and it’s of two types: Implicit Type Conversion and Explicit Type Conversion.

a. Implicit Type Conversion in Java:

This is the kind of type conversion where the machine automatically converts data to a higher type, according to the hierarchy of data types. This doesn’t require the user to provide any instructions. Implicit Type Conversion is also known as Coercion.

Example for Java Implicit Type Conversion:

int var1=2;
Double var2=2.0;
Double sum;
sum=var1+var2; //FirstCode

In the given case, the result that we get will be of the data type double, it will be converted automatically.

b. Explicit Type Conversion in Java:

This is the kind of type conversion in which the user does the conversion. In implicit type conversion we convert the data type from the lower to higher type. In Explicit Type conversion, the user decides to convert the data type and he can convert it from a higher type to lower type or lower to higher, whichever he wants. This is Type Casting.

Example for Java Explicit Type Conversion:

double var1;
double var2;
int product;
product=(int)(var1*var2);//FirstCode

In the above example, the data type will be converted into int due to Type Casting.

Hierarchy of Data Types in Java

Given below, is an image that shows the increasing order of the data types as we go downwards. If, in an expression, there are variables of different data types the result type then automatically converts to a higher type.

Scientific Numbers in Java

Scientific Numbers are the floating-point numbers when it contains the symbol “e” to indicate some power of 10.

Syntax:
float <variable name>= <numeric value>e<the power>;

Example of Java Scientific Numbers:

float sfnum= 23e3f; OR
double sfnum= 23e3;

Conclusion

Data types are one of the fundamental parts of learning Java. Without data types handling data will become a lot more difficult and tedious. Hence, learning about data types is essential to become an exceptionally good Java developer.

Leave a Reply

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