Welcome to Lesson 3!
Learning Objectives By the end of today's lesson, you should know... - What kinds of data the computer can store in its memory
- The four most common data types in Java and what kind of data they represent:
- int - whole number or integer data
- boolean - true or false values
- double - numbers with a decimal point
- char - character data
- How computers store data in memory.
- What is a variable.
- How to name variables.
- How to declare variables.
- How to assign values to variables.
1. Lesson 2 Practice Exam Questions- Write a complete program to print out the message Greetings! to the console window.
- Assume you are writing it in a file named Greeting.java
- Also, make sure that you have a correct comment up top with your name and section information
- How much of the code can you remember
- Add an inline (single line) comment to your program that states System.out is Java's name for the screen
- Place this comment above or next to your command to print the greeting
2. Introducing Data TypesWhat is a data type?- What is data?
- Information produced or stored by the computer.
- What is a type?
- The kind of data.
- For example, numbers vs letters.
- Data types are important because the compiler treats different types of data differently:
- What kind of operations can be performed. Ex: We can't add and subtract letters.
- How much storage to allot. Numbers and letters are stored differently in memory.
- There are four basic categories into which each data type falls:
Category | Explanation | Example | Integer
| A whole number. No decimal point.
| 106 | Floating Point Number
| A number with a decimal point.
| 3.14 | Character | Individual letters, digits and symbols
| 'A', '%', '9'
| Boolean | True/False, 0/1, On/Off
| true, false
|
Java Has a Variety of Data Types:Type | Bytes | Use |
---|
boolean | 1 | A true or false value.
| char | 2
| All Unicode characters, which may require more than 1 byte of storage
| short | 2 | Short integers from -32,768 to 32,767. | int | 4 | Integers from -2,147,483,647 to 2,147,483,647. | long | 8
| Integers from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 (quintillion). |
|
|
| float | 4 | Single-precision, floating-point numbers from about E-45 to E38 with 6 or 7 significant digits. | double | 8 | Double-precision, floating-point numbers from E-308 to E308 with from 14 to 15 significant digits. |
- Note that the number of bytes used for storage depends on the system and compiler
Group Activity: Name That Data Type!
Which of the 4 most common data types are the values below. Select int, double, boolean or char.
- 'A' is a(n) _____________________
- -213.555 is a(n) ________________
- 14 is a(n) _____________________
- 14.0 is a(n) ____________________
- false is a ______________________
- '$' is a ________________________
- -2147483647 is a ______________
- true is a _______________________
Most Common Data Types:
Type Memory Used Example Size Range
int 4 bytes 10 -2,147,483,648 to 2,147,483,647
double 8 bytes 10.5 10e-38 to 10e38
char 2 bytes 'c' not applicable
boolean 1 byte true not applicable
Storing Data In the Computer's Memory

Recall How Computer's RAM Memory Works:

- Each memory location has eight bits, which are known as a byte
- The memory address is the number that identifies a memory location
- Some data is too large for a single byte
- For instance, most numbers are too large for one byte
- In these cases, the address refers to the first byte
- The next few consecutive bytes store the additional bits for larger data
3. Variables- When
we want to instruct a computer to store or retrieve data from its
memory, we don't want to have to refer to the memory location in 1s and
0s.
- Wouldn't it be nice if we could use a name or label for a location in memory?
- Most people can remember a name better than a number.
- We would rather remember a name than a memory location in 1s and 0s.
- Fortunately, there is a way to give labels to the contents of memory addresses.
- Variables are labels used to reference locations in computer memory.
We don't want to have to remember memory addresses like this:
Instead we would rather remember memory addresses like this:- In the above diagram, num1 is a variable that refers to the contents of memory address 1652, num2 is a variable that refers to the contents of memory address 2548, and total is a variable that refers to the contents of memory address 45.
- When
we use variables, we let the computer remember the address and size of
the memory, we just remember the name of the variable.
Variables by Analogy
Variable Declarations- To create a new variable, you need to tell the compiler what kind of data to expect.
- Different data types require different numbers of bytes to store
- e.g. integers are stored in 4 bytes while doubles are stored in 8 bytes.
- Different operations can be performed on different types of data
- e.g. You can't add characters together, but you can add integers together.
- You also need to give your variable a name.
- Each variable declaration (statement) needs to be followed by a semi colon ;
- Let's look at some examples
double pi;
int numberStudentsClass;
boolean is_answer_correct;
char grade;
boolean tooCool4U;
int my_height;
Rules for Naming a Variable- A variable name is a sequence of letters, numbers, or underscore characters
- The first character must be either a letter or an underscore character (
_ )- Cannot be a number
- An _ is allowed but its use is discouraged.
- Also, the variable name cannot be one of the Java reserved words (keywords), such as int or double
- For a list of reserved words, see: Java Keywords
- Spaces are not allowed in variable names
- A space is NOT a letter, number or underscore character
- Also, variable names are cAsE sEnSiTiVe
id , ID , iD and Id are all valid but different names
Activity 3.1: Variable Declarations (10 pts)- A variable to store how many quarters you have been at De Anza
- A variable to store your age
- A variable to store whether or not programming is fun
- A variable to store your middle initial
- A variable to the value of pi
- Invent your own!
- Note that I should only see variable declarations inside your code - and the variables should not be assigned any values (that will take place in the next activity)
- When you are finished, run your code and notice what happens?
- Why is there no output to the screen?
- Hint: Are there any System.out.print() statements in the code
- Upload Vars.java to Canvas
4. Assigning Values to Variables- Once we have specified the name and data type for a variable, we want to give it a value.
- The value should match the data type.
- We give a variable a value by using the = sign.
- The variable name goes on the left side of the = sign.
- The variable value goes on the right side of the = sign.
- A semi colon ; always goes at the end of the statement.
variableName = expression;
- where expression is the value we assign to the variable.
- Below is an example of how to declare and assign a variable.
int numHorses;
numHorses = 12;
- We can also combine the two statements above into one statement.
int numHorses = 12;
- Either method is valid though the second option is preferred.
Assigning Literals and Complex Expressions to Variables
Initializing Variables
- Initial values may or may not be assigned when variables are declared:
// These are not initialized when declared
// and have unknown values
int sum, number1, number2;
// These are initialized when declared
int sum = 0;
int number1 = 5, number2 = 10;
- Good programming practice: initialize variables when declared
- Variables can also change their values as the program progresses
- Why are they called variables? Their content varies.
int myAge = 28;
//later in program...
myAge = 29;
Displaying Variables Using System.out.print()- To
display the contents of a variable using System.out.print(), the
variable should first be declared and have been assigned a value.
System.out.println(myAge); //NO! int myAge = 12; - To display the variable, place the name of the variable inside of the parenthesis of the System.out.print() statement.
int myAge = 12; System.out.println(myAge); - It
is also possible to print a message along with the variable contents by
using the + operator inside of the parenthesis of the
System.out.print() statement
int myAge = 12; System.out.println("I am " + myAge + " years old."); Activity 3.2: Assigning Values to Variables (10 pts)
- Let's
practice by giving values to the variables that we declared in our
last activity.
- Open Vars.java and practice assigning values to each of your variables
- After you have assigned a value to each variable, try displaying the variable to the console by placing it inside of a System.out.println() statement.
- For example:
int quarters = 3; System.out.println("I have been at De Anza for " + quarters + " quarters."); - When you have displayed each variable to the console, upload the updated Vars.java to Canvas under Activity 3.2
Activity 3.3: My Weekly Salary (10 pts)- Open up Eclipse.
- Inside of your Activity3 project, add a new class class named Salary.
- Add a block comment at the start of your program to include your name and section information. For example
/** * @author Jennifer Parrish * CIS 36A
*/
- Declare a variable called hours and assign it the value 40.
- Declare a variable called wage and assign it a value of your choice.
- Declare a variable called salary and assign it the value of hours * wage;
- Run the program and verify that there are no errors.
- Is this program satisfactory?
- Now, alter the program to print out the values of the salary you calculated.
- For example, you could print out the salary like this:
My weekly salary is $600.
- Note the $ and the . above. How do you get those to display?
- Submit your program to Canvas.
5. Wrap Up: - Answer the following question:
What Gets Printed to the Screen? int oranges = 5; int apples = 8;
int numFruit = apples + oranges; System.out.println("Total Fruit: " + numFruit); oranges = oranges + 1; apples = oranges; System.out.println( "Apples: " + apples); System.out.println("Oranges: " + oranges);
Upcoming Assignments:
- Assignment 2 due Tuesday at 11:59pm on Canvas
- Activities 3.1-3.3 due Tuesday at 11:59pm on Canvas
- Assignment 3 due Friday at 11:59pm on Canvas
- Quiz 2 due Friday at 11:59pm on Canvas
|