Posts
60
Comments
36
Trackbacks
18
Friday, June 19, 2009
Using Java Variables

When programming in Java, it is essential to understand the basics of storing and using information that is stored in variables.  In this post, we dig a little more into detail about using Java variables.  If you are just happening onto this post, please read the pre-cursor to this as we learned how and why to create Java variables.  You can find that post here: Java Variables.

Java allows you to store variables at different times during programming.  The first place you might store information into a variable is at the time you declare it.  Declaration happens when you are assigning a type to a name (i.e. int number;).  When you are assigning a value to a variable, you use the '=' equals sign with the assigned value coming after the name of the variable.  You can do this when initializing your variable, or you can do it anytime after you have declared your variable (assigned a type to a name, int number;).  Below are examples of how you can accomplish this.

Assigning value to a variable at initialization:

public class Variables
{
    // This method is where we will play
    public static void main(String[] args)
    {
        // TODO: Variables assigned at
        // initialization.
        int number = 2;
        double decimalNumber = 4.55;
    }
}

Assign a value to the variable after initialization:

public class Variables
{
    // This method is where we will play
    public static void main(String[] args)
    {
        // TODO: Variables assigned at
        // initialization.
        int number;
        number = 2;
        
        double decimalNumber;
        decimalNumber = 4.55;
    }
}

Note: You can not use a variable before it has been initialized to a type.  This will result in a compiler error.

When using variables in java, you can assign variables different values along the way.  In this post we have described and demonstrated assigning variables at initialization time as well as after initialization.

Technorati Tags: ,,,
posted @ Friday, June 19, 2009 11:28 PM | Feedback (0)
Java Variables

Java variables are essential when programming.  At the core of variables, is the ability to store information in a java program.  Variables can store many different types of information.  Some of the most basic types are integers (int), doubles or floating point (double), characters (char) and String (String).  After reading this post, you should have a very solid understanding of how to use variables in Java.

All java variables need at least two things to be created.  The type, which tells Java what kind of information you are storing and the name of the variable.  The name is how you would access this information later in the program.

To start off, lets create a basic java file that we will use to get a better understanding of variables.  If you haven't done so yet, please visit my posts on getting java set up to operate: Using Java with The Command Prompt and Our First Program.

Fire up notepad or Notepad++.  At this point, my screen shots will be coming from the latter, Notepad++.

Create a new text file in your editor and save it as Variables.java.  I place mine in the C:\Java directory.  The first thing we will do with our file is create the basic layout.

public class Variables
{
    // This method is where we will play
    public static void main(String[] args)
    {
        // TODO: Play with variables.
        
    }
}

Next we will start by creating an integer variable, used to hold an integer number for out program.  Let's name our integer number.

public class Variables
{
    // This method is where we will play
    public static void main(String[] args)
    {
        // TODO: Play with variables.
        int number;
    }
}

Here is a picture and explanation of what the different words mean.  Notice that our type represents the type of information being stored in this case, we are storing a whole number.  The name of the variable is how we will access our variable in the future.  We can change, print, read and compare our integer using the "number" reference (or name) later in our program.

6-19-2009 9-46-54 PM

Now let's create a number that stores a value that has a decimal.  This could be known as a floating point number or fraction.  At the end of our previous line, lets enter a new line and create a double. 

public class Variables
{
    // This method is where we will play
    public static void main(String args)
    {
        // TODO: Play with variables.
        int number;
        double decimalNumber;
    }
}

Notice that at this point we have two variables defined.  Now we must learn how to use these variables.

posted @ Friday, June 19, 2009 10:01 PM | Feedback (2)
News
My Developer Notebook! This also happens to be my opinion place. Thanks for coming by.