07 java variables

22
Mobile App:IT Eclipse Basics and Hello World IHS GIRLS TECH CLUB: Java Variables

Upload: zeeshan-shaikh

Post on 19-Jul-2015

134 views

Category:

Engineering


3 download

TRANSCRIPT

Page 1: 07   java variables

Mobile App:ITEclipse Basics and Hello World

IHS GIRLS TECH CLUB: Java

Variables

Page 2: 07   java variables

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.

Page 3: 07   java variables

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.

Page 4: 07   java variables

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

Page 5: 07   java variables

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.

Page 6: 07   java variables

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".

Page 7: 07   java variables

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.

Page 8: 07   java variables

COMMENT EXAMPLES

- There are two ways to use comments. Single line comments, or multi-line comments.

Page 9: 07   java variables

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.

Page 10: 07   java variables

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.

Page 11: 07   java variables

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.

Page 12: 07   java variables

ADDING VARIABLES

- Java makes it easy to add variables of the same type together.

Page 13: 07   java variables

VARIABLE MATH

- Java can also do math. Some basic examples are listed below.

Page 14: 07   java variables

PRINTING OUT TIME

Page 15: 07   java variables

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.

Page 16: 07   java variables

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 stringswhen the line of text is printed.

- Try the example below and examine the results.

Page 17: 07   java variables

MORE VARIABLE TYPE TROUBLES

- Try these examples and examine the results.

Page 18: 07   java variables

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.

Page 19: 07   java variables

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)

Page 20: 07   java variables

THE PROGRAM CODE (EXAMPLE 1)

Page 21: 07   java variables

THE PROGRAM CODE (EXAMPLE 2)

Page 22: 07   java variables

THE PROGRAM OUTPUT

- The results of the program should be similar to the text shown below: