 # Data types and variables in Java Posted by

# Data types and variables in Java

Writing a software program without knowing about data types and variables is almost impossible. This is true for Java, C#, python or for that matter, any other language. In this post, we are going to discuss data types and variables in Java. This article is part of Java tutorials for Selenium, which is a complete tutorial for beginners who are looking to learn java basics for Selenium.

## Introduction to Data Types

Lets take an example of cricket. Imagine India won the toss against Pakistan and elected to bat first. Sachin and Sehwag have opened the innings. At the bowlers end, we have Shoaib Aktar and Sachin is on the strike. Shoaib bowls first ball as a bouncer to surprise Sachin. But Sachin was ready for it and he hit for a SIX.

Now if you watch your TV screen it will show you live score board and Sachin’s score will be shown as 6 (1). It indicates that Sachin has made 6 runs in 1 ball. At the same time, Shoaib’s bowling figure will show 0.1 balls 6 runs.

Now if you observe closely here, 6 is a number without any decimal values. This is referred to as Integer in Java. A number with decimal points like 10.54 or 5.03 are referred to as floating values.

We have short, int and long data types to represent non-decimal data types in java. Float and double are used to refer to decimal values in Java. We also have data types to represent text values – char etc.

So, why do we have multiple data types to represent non-decimal number of decimal numbers? Can’t we use one single data type for storing all sizes of non-decimal numbers?

The answer is ‘no’. To understand that, let us consider the following facts:

Short data type has a size of 16-bit. It means that it can store a value from -32,768 to 32,767. Int data types can store values from -2,147,483,648 to 2,147,483,647. If we use int data type to store numbers like 10, 15 then we will be wasting a lot of memory space unnecessarily.

## Classification of Data types in Java

We have two varieties of data types in Java:

• Primitive Data Types
• Non-Primitive Data Types / Object Data Types

### Primitive Data Types in Java

In total there are 8 primitive data types supported by java. We are providing the list of these with their classifications:

Integer Data Types:

• byte (1byte)
• short(2 bytes)
• int(4 bytes)
• long(8 bytes)

Float Data Types:

• float (4 bytes)
• double(8 bytes)

Textual Data Types:

• char (2 bytes)

Logical:

• Boolean(true/false)

Let’s understand the usage of data types in Java with the help of an example.

## Variables in Java

We use variables in java for storing values. As values may be text, number etc, we also define the data types of these variables.

We will declare a variable for storing non-decimal numbers as follows:

int value_non_decimal;  //declaration of a variable

value_non_decimal is a variable and has a data type of int. To define a variable for storing decimal values, the syntax would be as follows:

float value_decimal;

It is necessary to initialize a variable by assigning a valid value to it.

value_non_decimal = 100; //Initializing a variable by assigning it a value

We can combine the declaration and initialization in a single statement:

int value_non_decimal = 100;

Example: We will write a program to add two integers and two decimal numbers. You can notice the error, where we have defined float variables. Float data types need to be defined as follows:

float c = 123.45f;
float d = 321.65f;

A decimal number does not converted to a float data type automatically. However, Java compiler does not complain when we use:

double a = 123.45;
double b = 321.65;

This is because a decimal number gets converted to a double automatically.

Output of this program is as follows:

Sum of Integers: 36000
Sum of Decimal Numbers: 445.09999999999997

As you can see, there are 12 digits after the decimal when we print a double variable as it is a 64-bit precision value.

What happens if we use float data type to add up numbers?

package Javatutorials;

public class VariablesEx {

public static void main(String[] args) {

float c = 123.45f;
float d = 321.65f;

float sum_float;

sum_float = c+ d;

System.out.println(“Sum of Decimal Numbers in Float: “+ sum_float);

}

}

Output of this program will be as follows:

Sum of Decimal Numbers in Float: 445.09998

Now you can see only 5 digits after the decimal.

You can try out other data types to understand the working of each of the data types

### Non Primitive Data Types

If we see our surroundings we can see people, fan, light, television, mobile phones etc…These entities have multiple properties and cannot be captured using a single value.

For example, a person has a name, height, weight and many other features, which we can use to uniquely identify a person. Name alone is not sufficient to uniquely identify a person.

So what will we do if we have to use a person in our Java program, we are looking at a person with following structure:

Person {

Name,

Weight,

Height,

Mother’s name,

Employment_Status

}

We cannot use a primitive data type for these structures, instead we use non-primitive data types. Non-primitive data types are created by programmers and are of 3 types:

• Classes
• Arrays
• Interfaces

They are also referred to as ‘reference variables’ because variables of Non-Primitive data types don’t store the value directly. They use a memory address to store the value and just store the address of the memory.

### Example of a class

Let’s look at an example of class “SoftwareEngineer

public class SoftwareEngineer{
String Name;
String Skills;
int age;
boolean marriage_status;

void Code() {
}

void tellMeYourName() {
}

void TellMeYourAge() {
}
}

How do we create a variable of non-primitive data type?

SoftwareEngineer SE1 = new SoftwareEngineer();

## Conclusion

In this tutorial, we have discussed the primitive and non-primitive data types. We also saw the concept of variables and how can we declare the variables for each data type?

Techcanvass offers IT certification courses for professionals. We are an IIBA endorsed education provider (EEP), iSQI ATP (for Certified Agile Business Analyst Training) as well as Agile Testing alliance partner for CP-SAT certification training in Selenium.

Our Selenium 3.0 Training programs along with DevOps courses are designed for Manual testing professionals with basics of Java and Python covered.

### Selenium 3.0 Training with Java ### Selenium 3.0 Training with Python ### Selenium Certification Training (CP-SAT) ### DevOps Certification Training 