Like any other statically typed programming language such as C and CPP, data types and variables in java play’s the same role of reserving the memory space in ram to hold the data when a program is in execution.

Data types are the unit of storage for digital information. The data type decides how much memory is going to be allocated or reserved for a variable.

What is a variable in java?

variable container example

variable container example

Well, the variable is a container in the programming language which stores the values in it of its type. Here type means the datatype.

Just like the containers in our day to day use, variables stores something, we call it digital information/data.

The value of the variable can be changed as per our need. The size of the variables depends on the data type of the variable. You cannot create a variable in Java without data as it is type safe language.

Variable can store values such as integer, fractional, characters, text, boolean etc. It is dependent on data type. So we’ll head towards data type.

What is the data type?

According to WikipediaIn computer science and computer programming, a data type or simply type is a classification of data which tells the compiler or interpreter how the programmer intends to use the data. 

In simple words, let’s take the example of buckets. Buckets are of different type and capacity. Some buckets hold one liter of liquid and some hold double or triple of that.

Data type will declare the memory space a variable requires.

Types of data types

There are two types of data types.

  1. Primitive data types  – These are predefined by Java itself.
  2. Non-primitive data types – These are user-defined data types.

Primitive data types

Primitive data types are the data types which are defined by the Java itself. This means the space which will be allocated by the variable will be decided by the operating system. This will also decide which type of data variable will store like text, decimal number, whole number,  single character etc.

These are also known as keywords, you cannot use any of them as the name of your variable.

Note – Always make sure the data type is written in lower case.

Syntax : datatype variable_name;

Following are the eight primitive data types in Java.

  1. boolean
  2. char
  3. byte
  4. short
  5. int
  6. long
  7. float
  8. double

boolean

Boolean is the data type which tells the compiler that the variable will hold only true or false, i.e 1 or 0.

Boolean values are the values which are returned by the conditional operators and these values are also used by the looping and branching statement.

Use this data type to store yes or no kind of information. It allocates the memory of 1 bit. The value can be true or false.

  • Size – 1 bit
  • Value – true or false

Following is the simple program which demonstrates the boolean variable.

In above program variable state is of type boolean. It is declared and initialized with the value of false. As we discussed above it is used inside the conditional statement. Due to the value inside the variable false block of if statement is executed.

The default value of the static boolean variable is false.

If variable is non static and inside local scope, variable has to be initialized.

char

Char stands for character. This data type is used to store the single Unicode character value in it.

It holds the memory of 2 bytes or 16 bits.

The value of this type of variable ranges from ‘\u0000’ (or 0) to ‘\uffff'(or 65,535).

While initializing the value must be within single inverted quotes, as shown in the example below.

Example: char myData=’n’;

In above example, variable with name myData is assigned with value ‘n’.

Every character value is mapped with its predefined ASCII value.

Following example shows the how char variable is used.

In the above program, we are casting/converting the value of character variable into an integer.

This will show us the ASCII value of any character. If you want to learn about casting in Java, read this article.

byte

The byte data type is used to store the 8 bit signed integer value.

It holds the memory of 1 byte in runtime.

The value of this data type ranges from -128 to 127.

If the value exceeds above the 127 then it will loop back to -128.

Suppose any byte variable contains the current value equal to 127 and if you increment it by 1, then the value of the variable will be -128.

Following program shows the example of how to use byte data type.

You can see in the output of the above program, after incrementing the value in the variable, our program printing value -128, due to the range of this data type.

The byte data type is useful in case when you want to reduce the memory usage.

short

The short data type is used to store the 16-bit integer values. The value of this data type ranges between -32,768 to 32,767. Just like byte, this data type is used to consume less memory.

It takes 2 bytes of memory when the program is under execution. It can be useful to minimize the memory usage of a large array.

Following program shows the example of the short variable in java.

Notice the output shown in the comments. In the above program, we decremented the value by one, but due to the range of the data type, it is looping back within the range.

int

Here int stands for integer datatype. The int data type reserves the memory of 4 bytes. 

The value of the integer type variable ranges within -2,147,483,648 to 2,147,483,647.

It is 32 bit signed integer data type.

It can be used as follows

int variable_name;

Following example shows the use of int.

long

The long data type is 64-bit signed integer data type.

The main difference between the int and long is the size of the variable. This data type allocates the memory of 64 bits that is 8 bytes.

The value of the variable ranges from -263 to 263-1.

From JavaSE 8, you can use it to store the unsigned value which ranges within 0 to 264-1.

Following program shows the example of the long variable in java.

 

float

The float is a single precision 32bit datatype. This data type is used to store the values with the fractional parts like 32.42, 534.22.

This kind of numbers has fractional part after the decimal point. All of the above integer variables will omit the fractional part.

The value of this data type ranges from 3.4e−038 to 3.4e+038.

Following program shows the example of the float variable in java.

F/f is the suffix used to denote that value is a float value as shown in the above program.

double

The double data type stores the floating point value, same as above. But the size of the variable will be double of the float.

It allocates the memory of 64 bit means 8 bytes. 

The value of this variable ranges from 1.7e−308 to 1.7e+038.

Following program shows the example of the double variable in java.

Floating point data types are mostly used for scientific calculations where the accuracy matters.

Non-primitive data types

Non-primitive data types are also known as user-defined data-types. The variables created using such data type are also known as reference variables or objects.

The size of the nonprimitive data type varies and depends upon the structure of the class, interface, and number of the instance variable in it.

Arrays come under the non primitive data type. As the memory allocation is dependent on the programmer as well.

As shown in the above program, we have created a class User which is having two instance variable.

Whenever you create a variable of type User, it will allocate separate memory for name and age inside it.

Every object of type User will have a separate copy of name and age variable.

Also read: Decision making and looping statements in Java

Keep learning :).


Leave a Reply

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