SLIDE 2 What did we talk about last time? Software development Data representation
SLIDE 3
SLIDE 4
SLIDE 5
SLIDE 6 What if you want to write a Java program that can…
- Edit music files
- Stream video
- Organize your photo album
Each of these tasks manipulates a lot of data Audio, video, and images are complicated kinds of data Java must build these kinds of data out of much simpler kinds
SLIDE 7 We are going to focus on five basic types of data in Java These are:
For whole numbers
For rational numbers
For true or false values
For single characters
For words
String is a little different from the rest, but we'll get into
that later
SLIDE 8
SLIDE 9 As I just said, the int type is used to store integers (positive
and negative whole numbers and zero)
Examples:
Inside the computer, an int takes up 4 bytes of space, which
is 32 bits (1s and 0s)
SLIDE 10
With 32 bits, an int can hold integers from about -2 billion up
to 2 billion
Positive numbers are represented like we've seen in binary Negative numbers need an extra trick that we aren't going to
go into
The actual maximum value is: 2147483647
SLIDE 11
Let's say you add 100 to the maximum int value 2147483647 You do not get 2147483747 Instead, it becomes a very negative number: -2147483549 This phenomenon is called overflow The opposite thing happens if you have a very negative
number and you subtract a number that makes it too negative
This phenomenon is called underflow
SLIDE 12 Each type has a number of literals associated with it A literal is a concrete value within a given type Literals for the int type are numbers exactly like what you
would expect:
SLIDE 13
It is also possible to create variables for each data type Think of a variable as a "box" that you can put values into The name of a variable is an identifier We can declare a variable of type int with identifier i using
the following line of code: int i;
SLIDE 14
This line of code creates a box named i that is designed only
to hold ints
For one thing, it's 32 bits in size
int i; i
SLIDE 15
Java variables are not like variables in math which have a fixed
(but unknown) value
Instead, a Java variable can be changed by a line of code We use the assignment operator (=) to set the value of a
variable as follows: i = 5;
SLIDE 16
This line of code stores 5 into i Think of the = operator as an arrow pointing left Let's see this in Eclipse
i i = 5;
5 5
SLIDE 17
SLIDE 18
You will use the int type very often Sometimes, however, you need to represent numbers with a
fractional part
The double type is well suited to this purpose Declaration of a double variable is just like an int variable:
double x;
SLIDE 19
This line of code creates a box named x that is designed only
to hold doubles
It has a different size from an int
double x; x
SLIDE 20
This line of code stores 3.14159 into x Remember that the = operator is like an arrow pointing left Let's see this in Eclipse
x = 3.14159; x
3.14159 3.14159
SLIDE 21
SLIDE 22
Numbers are great But, sometimes you only need to keep track of whether or not
something is true or false
This is what the boolean type is for You will understand it better when we cover conditionals in a
couple of weeks
Declaration of a boolean variable is like so:
boolean value;
SLIDE 23 This line of code creates a box named value that is designed
It cannot be used to store numbers
boolean value; value
SLIDE 24 This line of code stores false into value Remember that the = operator is like an arrow pointing left
value = false; value
false
false
SLIDE 25
SLIDE 26
Sometimes you need to deal with characters This is what the char type is for The char type only allows you to store a single character like
'$' or 'q'
Declaration of a char variable is like so:
char c;
SLIDE 27
This line of code creates a box named c that is designed only
to hold chars
It is used to store characters from most of the different scripts
in the world char c; c
SLIDE 28
This line of code stores the letter 'a' into into c We must use the single quotes so that Java knows we are
talking about the character 'a' and not a variable named a c = 'a';
'a'
c
'a'
SLIDE 29
SLIDE 30
The String type is different from the other types in several
ways
The important thing for you to focus on now is that it can hold
a large number of chars, not just a single value
A String literal is what we used in the Hello, World program
String word;
SLIDE 31 This line of code creates a box named word that is designed
It is used to store text of any length from most of the different
scripts in the world String word; word
SLIDE 32
This line of code stores the String "Mad flavor" into
word
We must use the double quotes so that Java knows we are
talking about the text "Mad flavor" word = "Mad flavor"; word
"Mad flavor" "Mad flavor"
SLIDE 33
SLIDE 34 Type Kind of values Sample Literals int Integers
900031
double Floating-point Numbers
3.14
6.02e23
boolean Boolean values
true false
char Single characters
'A' 'Z' '&'
String Sequences of characters
"If you dis Dr. Dre" "10 Sesquipedalians"
SLIDE 35
SLIDE 36
To output stuff, we just use System.out.println() What about input? Input is a little trickier We need to create a new object of type Scanner
System.out.println("Flip mode is the squad!"); System.out.println(35);
SLIDE 37 There are three parts to using Scanner for input
1.
Include the appropriate import statement so that your program knows what a Scanner object is
- 2. Create a specific Scanner object with a name you choose
3.
Use the object you create to read in data
SLIDE 38
Lots of people have written all kinds of useful Java code By importing that code, we can use it to help solve our
problems
To import code, you type import and then the name of the
package or class
To import Scanner, type the following at the top of your
program (before the class!)
import java.util.Scanner;
SLIDE 39
Once you have imported the Scanner class, you have to create a
Scanner object
To do so, declare a reference of type Scanner, and use the new
keyword to create a new Scanner with System.in as a parameter like so:
You can call it whatever you want, I chose to call it in Doesn't make any sense? For now, that's okay.
Scanner in = new Scanner(System.in);
SLIDE 40
Now that you've got a Scanner object, you can use it to read
some data
It has a method that will read in the next piece of data that user
types in, but you have to know if that data is going to be an int, a double, or a String
Let's say the user is going to input her age (an int) and you want
to store it in an int variable called years
We'll use the nextInt() method to do so:
int years; years = in.nextInt();
SLIDE 41
Scanner has a lot of methods (ways to accomplish some tasks) For now, we're only interested in three These allow us to read the next int, the next double, and
the next String, respectively:
Scanner in = new Scanner(System.in); int number = in.nextInt(); double radius = in.nextDouble(); String word = in.next();
SLIDE 42
import java.util.Scanner; public class Age { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.println("What is your age?"); int years; years = in.nextInt(); years = years * 2; System.out.print("Your age doubled is "); System.out.println(years); } }
SLIDE 43
SLIDE 44 In Java, each data type has a set of basic operations you are
allowed to perform
It's not possible to define new operations or change how the
Some programming languages allow this, but not Java
SLIDE 45 Basic operations you can use on numerical values:
Add
Subtract
Multiply
Divide (some tricky things here)
Modulus (we'll cover this more later)
There are ways to raise numbers to powers or find square
roots, but they are not built-in operators
SLIDE 46
You can use these operators on literals (actual values),
variables, or both
int a; int b; a = 5 + 6; // a contains 11 b = a * 3; // b contains 33 double c = 2.5; c = c – a; // c contains -8.5
SLIDE 47
SLIDE 48 Lab 1 is tomorrow
- (For students with last names in the first half of the class roster)
We'll go deeper into math operations in the next lecture
SLIDE 49 Keep reading Chapter 3 of the textbook Start working on Project 1
- You've got most of the tools you need to finish it