We know how to compile and run a basic Java application. We also know the basic structure of code for Java. If you don’t know what I am talking about, please refer to the previous articles in the Java 101 series (lessons 1, 2 and 3). In this article, we will take a look at variables and how to use them.
What is a variable? A variable is a container which holds information. Containers come in all shapes and sizes. We know that they have a limitation to what they can hold (or what can be stored inside them). You can’t put a shoe in a container that is meant for storing pens (it would be too small). Variables have these limitations too.
Types of variables :
- int (stores integers. Ex: -1, -2, 9, 4)
- float (stores floating point numbers. Ex: 2.3, 2.0, -9.75)
- double (stores floating point numbers also but has a larger size)
- String (stores text. Ex: “Hello world”, “Java 101”)
- boolean (stores true or false)
A variable will only hold a value (information) that is of its type. An int will only hold an integer. You can’t put a string in it.
You have to make a chair before you can sit on it. Makes sense right? How can you sit on a chair that doesn’t exist. Same is the case with variables. You have to declare them before you can store something in them. Variables are declared like this :
int a;
int someNumber;
String greeting = “Good morning”;
You write the variable type first then write the variable name. In the above examples, variable types are written in purple and variable names are in black. A variable’s name is what you will use to refer to it (when you want to put something in it, see what’s in it, or take something out of it). There are some rules to how you can name a variable. They are (quoting from java.sun.com):
- Variable names are case-sensitive. A variable’s name can be any legal identifier — an unlimited-length sequence of Unicode letters and digits, beginning with a letter, the dollar sign “
$
“, or the underscore character “_
“. The convention, however, is to always begin your variable names with a letter, not “$
” or “_
“. Additionally, the dollar sign character, by convention, is never used at all. You may find some situations where auto-generated names will contain the dollar sign, but your variable names should always avoid using it. A similar convention exists for the underscore character; while it’s technically legal to begin your variable’s name with “_
“, this practice is discouraged. White space is not permitted. - Subsequent characters may be letters, digits, dollar signs, or underscore characters. Conventions (and common sense) apply to this rule as well. When choosing a name for your variables, use full words instead of cryptic abbreviations. Doing so will make your code easier to read and understand. In many cases it will also make your code self-documenting; fields named
cadence
,speed
, andgear
, for example, are much more intuitive than abbreviated versions, such ass
,c
, andg
. Also keep in mind that the name you choose must not be a keyword or reserved word. - If the name you choose consists of only one word, spell that word in all lowercase letters. If it consists of more than one word, capitalize the first letter of each subsequent word. The names
gearRatio
andcurrentGear
are prime examples of this convention. If your variable stores a constant value, such asstatic final int NUM_GEARS = 6
, the convention changes slightly, capitalizing every letter and separating subsequent words with the underscore character. By convention, the underscore character is never used elsewhere.
You will notice that there is a ; (semi-colon) at the end of every line in the above example. It is required in Java. You have to place a semi-colon at the end of every statement. It’s presence tells Java where a statement ends (yes, you can have a statement on 2 lines but there has to be a semi-colon on the second line).
After you declare a variable, you can store values (numbers, text, etc) in it. This process is called assigning a value to a variable. You can assign a value to a variable when you declare, or after you declare it. It is done like this :
int numberOfCars; // Declare numberOfCars as an int
numberOfCars = 5; // assign the value 5 to numberOfCars
double weight = 152.50; // Declare weight as a double and assign 152.50 to it.
Now that we have an idea of what a variable is, let’s write a program that will utilize them.
import java.io.*; public class VariableTest { public static void main(String[] args) { /* Declare variables */ double numberOne = 14.0; double numberTwo = 13.5; double addResult; double diffResults; double prodResult; double diviResult; /* Add numberOne and numberTwo and put the sum in addResult */ addResult = numberOne + numberTwo ; /* Subtract numberOne and numberTwo and put the difference in subResult */ diffResult = numberOne - numberTwo; /* Multiply numberOne and numberTwo and put the product in prodResult */ prodResult = numberOne * numberTwo; /* Divide numberOne and numberTwo and put the quotient in diviResult */ diviResult = numberOne / numberTwo; /* Print out all the results */ System.out.println("numberOne = "+numberOne); System.out.println("numberTwo = "+numberTwo); System.out.println("Their sum is : "+addResult); System.out.println("Their difference is : "+diffResult); System.out.println("Their product is : "+prodResult); System.out.println("Their quotient is : "+diviResult); } }
Leave a Reply