2. WHAT ARE VARIABLES?
- Variables are core components in any program.
- A variable is a way to store information so you can reuse it
later.
- Without variables things like Microsoft Word, Facebook,
Amazon, and any other program or website, would not exist.
3. TWO PARTS OF A VARIABLE
- A variable contains two parts, a value, and a symbolic name.
- A value contains the information your variable is actually
storing
- A symbolic name or variable name is how you are able to set
and retrieve the value of the variable.
4. VARIABLES HAVE TYPES
- There are different types of variables.
- These types of variables are used to store different types of
data.
- Here are three common variable types.
- A string is used to store text information. An example would
be your name.
- An integer is used to store whole numbers. Examples include
1, 87, -24
- A double is used to store decimal numbers. Examples include
3.23, 8.7, -94.2
5. VARIABLE TYPES IN JAVA
- Java needs to know what type of information you plan on
storing in a variable.
- If you try to store the wrong type of information in a variable, a
Java program may crash, display errors, or work improperly.
6. VARIABLE EXAMPLES
- In the first example, a string variable is being created. The
variable name is str and the value is being set as "test".
7. COMMENTS
- You will notice that there are three lines of code in the
previous example starting with //
- These lines of code are called comments and will be ignored by
Java. This is useful for documenting or adding extra help text
to detail what specific sections of code do.
- Comments make it easier for you and other developers to
understand the Java program.
8. COMMENT EXAMPLES
- There are two ways to use comments. Single line comments, or
multi-line comments.
9. FOLLOW ALONG WITH ECLIPSE
- Now it is time to follow along with the code samples in the upcoming
slides.
- The first step is to open Eclipse and create a new project called
"Examples".
- After creating the project, Create a new Java class called Examples.
Make sure to check the box to add the "public static void main"
method.
- Inside the "main" method, you can type in the upcoming code and
run it after you finish typing in the code. The code should run
without any errors.
- Each time you type in the code, you should replace the code you
were previously working on.
- Make sure to keep all your code inside the main method.
10. PRINTING VARIABLES
- You already know how to print things using a line of code that
looks like:
- You can also print out variables. Here is an example of how to
do the same thing using variables. Notice how the variable
name is used inside println, instead of the text.
11. STRING VARIABLE EXAMPLES
- Now that example is not very useful, so we will look at how to
print your name using a variable.
- Using variables makes it simple to change the name that is
printed out.
- You will also notice we have introduced a new concept. Inside
the println method, we are adding two strings together. This
is called string concatenation.
15. CODE EXPLANATION
- In the previous example we introduce the print method.
- The print method can be called multiple times to build a line of
text to be output by your program.
- Like in the example, the series of print statements should
always end with a println method line.
16. VARIABLE TYPE TROUBLES
- In the previous example, you will notice that on the last line of
code we add together strings and integers. In this case it
works by automatically converting all the integers to strings
when the line of text is printed.
- Try the example below and examine the results.
17. MORE VARIABLE TYPE TROUBLES
- Try these examples and examine the results.
18. EXPLANATIONS AND CAUTIONS
- You will notice in the string and integer examples, things did not
always display as you might expect.
- What about the first division example? You would expect it to display
1.5 or perhaps round up to 2 but instead it outputs 1. This is
because you are dividing two integers. Because they are integers
they can not contain a decimal value, anything after the decimal
point is lost. This is why three divided by two with integers is
actually 1.
- As soon as you add one decimal point, Java knows that you want to
convert everything to a double. Because a double can store decimal
values, the math works correctly.
- Try out a few other examples and see what happens. As you can see,
variable types in Java are very important.
19. TRY THIS!
- Clear out all the code in your main method.
- Write a program that uses variables to store your name and
your age.
- Print out your name and age on one line.
- Print out how many months old you were at your last birthday
(hint: age * 12)