Variables In Java

Abdul Azeez
Aug 26, 2017 · 2 min read

Well well, before we proceed, let us begin by understanding the concept of variables in programming. A variable is a label for data in memory. So what exactly do I mean? The essence of programming is to manipulate a given input to get an output. So when the computer receives the input, how does it label it? How does it save it? That is the essence of a variable. A variable is a named memory location for keeping data. So in Java, to save a certain input, it follows a basic way.

First is to define the type of variable you want to save. Well, there are different types of data you can save on the computer, but let us begin with the basic types in Java otherwise known as the primitive data types. The primitive data types handle Numbers, Characters, and Boolean (True or False).

Numbers are also divided into different categories. We have whole numbers and real numbers or you could just imagine numbers with decimals. Whole Number Data Types in Java are byte, short, int and long. These data types hold different range of numbers. The byte is the smallest and the long the largest. Real Number Data Types are basically double and float. They also hold the different range of real numbers with float being the least and the double the largest.

Beyond numbers, we can also save characters. We use char to save characters. Single characters are what is saved using the char. Also, there is the boolean which handles true and false. They handle logical operations.

To create a variable in Java, you first need to declare its type. This is called variable declaration. By declaring a variable type, you are telling Java the type of data you want to save. This concept where a language tells what type of data it holds is called a Strongly Typed Language. Languages that do not hold this paradigm are called loosely typed languages. So Java is a strongly typed language.

variable_type variable_name;

A semicolon must end every statement in Java. After declaring the variable type, the next thing is to define the value that the variable would hold. So for example,

int age;

age = 5;

The first statement creates a variable called age that is to hold a number. The next line initializes age with a value of 5. The second line is called variable initialization. After declaring and initializing a variable, you can now simply refer to the variable name to get the value you saved.

Variables in programming is a bit more extensive than this little explanation but I hope this little explanation throws more light on variable declaration and initialization in Java.

Thank You

)

Abdul Azeez

Written by

A Java Developer By Day, Python Developer By Night. Becoming Better Day by Day is my Ambition

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade