What are variables? – II

Further notes on variables and Java Types

Hi! Welcome to the next lesson of our beginner’s course.

This is an extension of the previous lesson and now we are going to learn some important rules and tips about the variables.

Oh sorry guys I couldn’t show you that how do we actually write variables in java programs. So here’s an example.

Go through it and notice how the variables are written.

Note that the statements like //only second variable is assigned a value during the declaration” are not a part of the code that is run by the computer.

They are called comments. Comments are only for human readers. They are there just to give an idea for the reader about the code.

The comments start from // symbol. You can also use /* and */ for an extended comment with several lines.

Back to the real code! Actually, there is no any output for this code. You can try running it by pressing the Run button in the toolbar.

But nothing is given out… right? 🙂

Ok. Here are the basic rules of writing variables. You MUST obey them.

Rule #1

You MUST begin the variable name with a letter or an underscore.

Eg: issacNewton, _test

Rule #2

You cannot add spaces in a variable name. See, issacNewton is NOT issac newton.

Rule #3

The variables names are case sensitive. That means, variables issacNewton and IssacNewton and ISSACNEWTON are different. Not the same variable!

That means, it strictly consider the capital and simple difference in a variable. To be the same variables, two entities must have the same capital and simple letters.

So suppose when you want to change the value of issacNewton to 400 as above code and you accidentally write IssacNewton=400. What happens? (Note the change in letter “i”) Try it and see for yourself… An error occurs. Right?

As java variables are case sensitive, IssacNewton will be recognised by the computer as another variable, not the same issacNewton. But we didn’t give it a type.

That is, we didn’t declare IssacNewton whether it is a int, double or any other type.

Remember, a variable has to declare its type before assigning a value like 400.

Those are the 3 important rules that you MUST follow.


Other than them, there are some tips for you to apply to be a good programmer.

  • Always start your variable with a simple letter as I did in the previous examples. That’s the usual convention among programmers. If you wanna join them, follow it 🙂 Why?

When you become a serious programmer soon and write your codes, and you give your code to a fellow guy like you.

Then he can easily identify a variable you write, if you follow this convention. If you mess up your variables with capitals and simples, then it will be a bit annoying 😛

  • There are two techniques to separate two words in a variable.
  1. Use an underscore to separate two words

eg: issac_newton

       2. Start the next word as a capital letter

eg: issacNewton

Anyway, I prefer the 2nd. Dunno why…Maybe it’s quite easier capitalize a letter instead of typing an extra underscore 🙂

Use these techniques. Never write a variable like issacnewton… It’s not nice at all. Isn’t it? You can’t read it easily like the previous two.

  • Try to use some meaningful name for the variable. A name which you can easily understand and relevant to the data that you are going to store in that variable.

For example, interest_rate variable that we have used in the example might be useful in some software used to calculate the rate of interest of a bank deposit.

It does make sense to use that long name instead of using some letter like x as the variable name. So we can easily remember the variable name.

In a huge program, there would be 100000s of lines of code. If we see interest_rate among those lines, we may say “Oh it’s the interest rate…” coz we are familiar with it.

Alright. That’s all for this lesson. Still some theory parts are going on 🙁 Don’t worry!

We’ll get some hands-on experience in the next lesson. It’s about some cool tricks that you can do by combining variables with Strings…Let’s see.