Variables and Datatypes In Java

Congratulations on your first Java program. We will go a step forward to learn what are datatypes and variables in Java.

In a computer program, we basically play with values. We get some input values, we evaluate them, we calculate new values based on the input values and display the results.

These values are stored in a computer’s memory. When doing our calculations, we will want to refer to these different values. To refer to these values we have to remember the memory addresses where these values are stored, right?

It is difficult to remember so many memory addresses. So, for convenience, these memory addresses are given names and we can then refer to the values by these names.

These names which are given to memory locations are called variables in terms of computer programming.


We may have different types of values in our computer program. We may have some numbers. We may have some words too. When we do any calculations on these values (or variables), let’s say addition, we cannot add a word and a number. We cannot divide a word by a number.

Every value or variable (or say data) has its properties. To distinguish amongst different types of values or data based on their properties, we have Datatypes. Using datatypes, we tell the computer to treat a value either as a word or as a number.

Datatypes

In Java, datatypes are categorised as following:

  • Numerical datatypes
    • Whole numbers
    • Decimal numbers
  • Character datatypes
  • Conditional datatypes

Numeric Datatypes

To represent whole numbers we have these datatypes:

  • byte
  • short
  • int
  • long

Good thing!

You just need to read them twice, and not worry much about these at this point of time.

The above mentioned datatypes differ in terms of how big a number they can hold. For example, a byte can hold a number between -128 to 127. A short can hold a number from -32768 to 32767. An int is larger than short and a long is larger than an int.

byte < short < int < long

I REPEAT. You need not worry about the size of the datatypes right now.

Similarly, to represent decimal numbers (numbers with fraction part) we have two types of datatypes:

  • float
  • double

Again, they differ in terms of the size of the number they can hold.

float < double

Character Datatypes

To represent character type values, we have the following datatypes:

  • char

Conditional Datatypes

Sometimes, we want to store values that are either true or false. To store these types of values, we have:

  • boolean

Now in order to use variables in our program, we have to explicitly tell the computer that we are going to use these many variables and their respective datatype, so that when the computer does its calculations on those variables, it will know how to deal with them.

This explicitly telling the computer about the variables and their datatypes is called ‘Variable Declaration’. We will learn in the next article how to declare and use variables.

3 thoughts on “Variables and Datatypes In Java”

  1. I read your written content frequently and I just thought it finally was time to leave a comment and say keep up the great work.

  2. Way cool! Some extremely valid points! I appreciate you writing this post and also the rest of the site is also really good.

  3. Every weekend i used to visit this website, because i want enjoyment, as this this website conations really nice funny stuff too.

Leave a Reply

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