Variable and Expressions

Variables

What is a variable anyway? You can consider a variable as a holder for a certain value. There are two kinds of variables: Simple variable, which is to be discussed soon, and complex variable. The difference between the two is that simple variables can only store one scalar value, while the complex ones can store more. If you don't understand what scalar means, don't worry about it. You will understand that anyway later.

How to declare a variable in Java? The variable is declared in the following format: <type> <variable_name>;

Let's look at the following example(Variables1.java):

class Variables1 {
   public static void main(String[] args)
   {
       int age;         // <-- These are the variables:
       String name;     // variable age of type integer
                        // and variable name of type string

       age = 20;
       name = "John Doe";

       System.out.println("Hi, my name is "+name);
       System.out.println("I am "+age+" years old.");
   }
}
You can examine there are two variables: age and name. Of course you can declare more than two variables. If you want to declare two or more variables of the same type, you can combine the declaration as follows:
int var1, var2, var3;
Well, that declaration says that we declare three variables: var1, var2, and var3. Be careful and keep in mind that Java is case sensitive, so pay attention to capital letters as well. Otherwise your program may not work.

To use variable, i.e. to make the variable store a value, simply put the equal sign after the variable name like the line age = 20; above. Of course, you will be able to use variables ONLY AFTER you declare them. However, it is not necessarily to have the variable declaration on the top of your program like the example above. You can declare them anywhere as long as before you using them.

There are some basic data types you should know. Note that this is not the basic in the technical sense, however, I just point out several data types that is necessary for you to know first.

  • int     is the one that store integer values. Its limit is between about -2 billion to about +2 billion. Pretty much for simple applications.
  • long    is the one you need if int is not sufficient for you.
  • float   is the one that store fractional values like 0.5 and so on. It's practical limit is huge... don't ask me how big it is... :-)
  • String  is the one that store sentences and words. So, the best use for this data type is like to input names, addresses, and so on.
  • char    is the one that store only one character, either it is a letter, a space, or a single punctuation mark, and so on.
Keep in mind that in Java, we must acknowledge the scopes of the program in declaring variables. What is the scope? Well, don't bother on the technical details... simply consider that the scope is anything between the curly braces { ... }. The outer curly braces is called the outer scope, and the inner curly braces is called inner scope. Of course later you would have a lot more complex braces. If you declare variables in the outer scopes, they are visible (i.e. usable) by the program lines inside the inner scopes. However, if you declare variables in the inner scope, you cannot expect the outer scopes to see it. For the example see the illustration below:
{
   :  // Scope A
   :  // Suppose variable i is declared here
   :
      {
         :
         : // Scope B
         : // Suppose variable j is declared here
            {
               : // Scope C
               : // Suppose variable k is declared here
            }
         :
         : // Scope B continued
         :  // Suppose variable l is declared here
            {
               : // Scope D
               : // Suppose variable m is declared here
            }
         : // Scope B continued
      }
   :
   : // Scope A continued
   : // Suppose variable n is declared here
      {
          : // Scope F
      }
}
If you create the variable on the scope A, it will visible on all the scope below it. Like in the case of variable i above, it is visible through all scopes. In the case of variable j, it is only visible for scope C, D, and the rest of scope B, but not scope A and F. In the case of variable k, only scope C can use it, not even scope D. The same case is variable m, which can only be used by scope D. For the case of variable l in scope B, it can only be used by the rest of scope B and scope D. Scope C cannot use it because the variable is declared below scope C. Same case is variable n, which can only be used by the rest of scope A and scope F, but not scope B since the variable is declared below B.

This scope concept is very important to keep in mind. I'd rather to explain it to you pretty early so that you won't find much difficulties later, especially in dealing with methods.

Expressions

Of course you can do more in using vaiables. For example: You can write some math formula here like the excerpt below:
long E;
int m, c;

m = 5;
c = 3000;
E = m * c * c;
Hey, that's the famous E = mc2 formula! You bet! :-) Of course you can do more complex equation with Java. These kind of equations will later be called expressions.

Keep in mind that Java doesn't have the ^ operator like BASIC. The power sign for Java will be discussed later.

Oh yes, in case you wonder this line:

       System.out.println("Hi, my name is "+name);
       System.out.println("I am "+age+" years old.");
Well, to print out variables onto the screen, simply put the plus sign after the text quotes you want to display, like the first lane in the above example. In the second line, suppose you want to continue with some more texts, you may add the plus sign, and again put some texts. Don't forget to wrap up the text with double quotes as well. If you just want to print out the variables, then you can do like this:
System.out.println(myvariable);
Hmm... pretty simple, eh?

Summary

To sum up, after following this lesson, you should understand the meaning of variables and how to use them, and how to apply expressions in Java. You should understand program scopes and how it affect variables. Of course, here you also learn how to print out your nice variables.

Related:

Java Books
Java Certification, Programming, JavaBean and Object Oriented Reference Books

Return to : Java Programming Hints and Tips

All the site contents are Copyright © www.erpgreat.com and the content authors. All rights reserved.
All product names are trademarks of their respective companies.
The site www.erpgreat.com is not affiliated with or endorsed by any company listed at this site.
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk.
 The content on this site may not be reproduced or redistributed without the express written permission of
www.erpgreat.com or the content authors.