askIT

Variables storage

Lagre Download example files

A variable is a storage for numbers or words. A dresser is a good example. It has four drawers. We can give the four drawers different names, and use each drawer for storage. The top drawer is for t-shirts, next to sweaters, next to the pants and the bottom of underwear.

We provide the drawers names as shown below. All names always start with lowercase letters.

variabler.jpg

We can now save some of the variables. This is done by first typing String, then the variable name, then the content of the variable.

The code for programming the drawer:

package dresser;
public class Dresser {
        public static void main(String[] args) {
               
                // My Variables
                String topDraver = "T-shirts";
                String middleDraver1 = "Sweaters";
                String middleDraver2 = "Pants";
                String bottomDraver = "Underwear";
        }
       
}

Printing variables

Use System.out.print or System.out.println to print variables. The first code will print a text, while the second will print text and create a line break.

The example under will use both print functions.


package dresser;
public class Dresser {
        public static void main(String[] args) {
               
                // My Variables
                String topDraver = "T-shirts";
                String middleDraver1 = "Sweaters";
                String middleDraver2 = "Pants";
                String bottomDraver = "Underwear";
               
                // Print content of drawers
                System.out.println("In the top drawer there are " + topDraver);
                System.out.print("In the middle drawer there are " + middleDraver1);
                System.out.println(" and " + middleDraver2);
                System.out.println("In the bottom drawer there are " + bottomDraver);
        }
}

Output:
In the top drawer there are T-shirts
In the middle drawer there are Sweaters and Pants
In the bottom drawer there are Underwear

Change the content of variables

It is possible to change the contents of variables. To switch the contents of the top drawer from t-shirts to towels first write the variable name then the new content.

topDraver = "Towels";

The example below will first create four variables and print the content of them. After printing them we will change the content of one variable with a new value.

Note that we do not need to write String before the variable name when we will change its contents. We do this when we create variables.


package dresser;
public class Dresser {
        public static void main(String[] args) {
               
                // My Variables
                String topDraver = "T-shirts";
                String middleDraver1 = "Sweaters";
                String middleDraver2 = "Pants";
                String bottomDraver = "Underwear";
               
                // Print content of drawers
                System.out.println("In the top drawer there are " + topDraver);
                System.out.print("In the middle drawer there are " + middleDraver1);
                System.out.println(" and " + middleDraver2);
                System.out.println("In the bottom drawer there are " + bottomDraver);
               
                // Change content of top drawer
                topDraver = "Towels";
                System.out.print("The top drawer now has ");
                System.out.println(topDraver);
        }
}

Output:
In the top drawer there are T-shirts
In the middle drawer there are Sweaters and Pants
In the bottom drawer there are Underwear
The top drawer now has Towels

Exercises

1. Create a program with three variables with the colors red, blue and white. Use variables to write the colors of the flags Cuba, France, Iceland, Norway, Haiti and Austria.

2. Create variables for 10 different animals. The content of the variables should be the sound the animal creates.

3. Write a program that includes variables for name, age and residency for 3 friends. Each variable can only contain either name, age or place of residence. You have to either create 9 variables or find some other clever way to solve this assignment.

Add comment

avatar_blank_60.png

Comment:

Name:

Email:

Comments

No comments yet. You can be the first one to comment. Just write your comment in the form and click on the submit button.