Week 2 - Monday
What did we talk about last time? Software development Data representation Binary numbers
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 of data
We are going to focus on five basic types of data in Java These are: int For whole numbers double For rational numbers boolean For true or false values char For single characters String For words String is a little different from the rest, but we'll get into that later
As I just said, the int type is used to store integers (positive and negative whole numbers and zero) Examples: 54 -893992 0 Inside the computer, an int takes up 4 bytes of space, which is 32 bits (1s and 0s)
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
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
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: 115 -9837461 2
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;
int i; This line of code creates a box named i that is designed only to hold int s For one thing, it's 32 bits in size i
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;
i = 5; This line of code stores 5 into i Think of the = operator as an arrow pointing left i 5 0 5 Let's see this in Eclipse
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;
double x; This line of code creates a box named x that is designed only to hold double s It has a different size from an int x
x = 3.14159; This line of code stores 3.14159 into x Remember that the = operator is like an arrow pointing left x 3.14159 3.14159 Let's see this in Eclipse
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;
boolean value; This line of code creates a box named value that is designed only to hold boolean s It cannot be used to store numbers value
value = false; This line of code stores false into value Remember that the = operator is like an arrow pointing left value false false
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;
char c; This line of code creates a box named c that is designed only to hold char s It is used to store characters from most of the different scripts in the world c
c = 'a'; 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'
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 char s, not just a single value A String literal is what we used in the Hello, World program String word;
String word; This line of code creates a box named word that is designed only to hold String s It is used to store text of any length from most of the different scripts in the world word
word = "Mad flavor"; 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" "Mad flavor"
Type Sample Literals Kind of values -5 int 0 Integers 900031 3.14 Floating-point double -0.6 Numbers 6.02e23 true boolean Boolean values false 'A' char 'Z' Single characters '&' Sequences of "If you dis Dr. Dre" String "10 Sesquipedalians" characters
To output stuff, we just use System.out.println() System.out.println("Flip mode is the squad!"); System.out.println(35); What about input? Input is a little trickier We need to create a new object of type Scanner
There are three parts to using Scanner for input Include the appropriate import statement so that your program 1. knows what a Scanner object is 2. Create a specific Scanner object with a name you choose Use the object you create to read in data 3.
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;
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: Scanner in = new Scanner(System.in); You can call it whatever you want, I chose to call it in Doesn't make any sense? For now, that's okay.
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();
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();
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); } }
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 operations behave Some programming languages allow this, but not Java
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
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
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
Keep reading Chapter 3 of the textbook Start working on Project 1 You've got most of the tools you need to finish it
Recommend
More recommend