Introduction To Java (Prequel For Android)
Introduction To Java (Prequel For Android)
Why Android?
Worldwide, 72% of smartphones are
Android-based.
In the US, 50% of smartphones are Android-based.
In the US, the average salary of an Android developer is
93K. In San Francisco, the average salary of an
Android developer is 120K.
Mobile development (smartphones and tablet) is an
important focus for most product companies.
Developing mobile apps is fun!
Scope
Programming Fundamentals (Variables, Loops, Arrays, etc)
Object Oriented Programming (Classes, Inheritance)
Next course: Introduction to Android
Class Structure
Lesson Module (e.g., Variables or Loops)
Brief conceptual overview
Short hands-on exercises
Course project (building 2D snake game)
We cover each lesson module in turn
building on previous concepts.
Course Project
Over the course of the lessons, we will be
building a 2D Snake Game in Java.
Setup Checklist
Let's make sure we have everything we need to
get started.
Java and Eclipse IDE
eGit Plugin for Eclipse
Two Eclipse Projects (from Git):
Basic Exercises
git@github.com:thecodepath/intro_java_exercises.git
Introduction
Understanding Programs
What is Programming?
Computers are machines that can run programs.
Programs describe simple steps for a computer to
perform.
Programs start from the top and follow the instructions
on each line until a program is completed.
Types of Programs
Development Platforms
Java &
Android
Objective-C &
Cocoa
Ruby &
Rails
Hello World
Understanding our first program
Hello World!
Let's create a simple Java program.
Select File New to create new things.
We are going to create a New Project
...and create a New Class.
Hello World!
Let's create a simple Java program.
Hello World!
Let's run the program in Eclipse.
Click the Green Run Button.
Check the Console for "Hello World"
Run Program
Edit Code
View Console
Output
Resume Running
Next Line
Start Debugging
Current Line
Code View
Console View
Projects Overview
We will be working within two eclipse projects for
these lessons:
intro_java_exercises are basic
java exercises which we will
complete after each concept is
introduced.
snake_exercises is the initial
project which we will develop into
a 2D snake game during the
lessons.
1. Output
How a program displays information
Output - Println
When writing programs, displaying data onscreen is called producing output.
In Eclipse, the console is at the bottom and contains
anything printed out in a program.
To print out in the console, use the following command:
System.out.println("Hello World");
We will be using this command to print out text as a part
of our exercises.
Output - Main
public class MyFirstApp {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
Output - Commands
As shown before the following command outputs plain
text to the console:
System.out.println("Hello World");
But what do the parts of this command mean?
There are thousands of commands in Java, one of
which is println. Commands are organized into
categories. Categories can have sub-categories.
System is a category and out is a subcategory of
commands. In Java, these are known as packages.
Output - Execution
Run Program
args[]) {
step 1");
step 2");
step 3");
main
This is step 1
This is step 2
This is step 3
End
Output - Comments
When writing programs, you can need a way to
document your code and leave a summary explaining
what certain parts do.
To create a comment which is a summary of code that
is ignored by the computer, simply put '//' at the
beginning of a line:
// This is a comment, computer ignores these
Output Examples
System.out.println(5);
// 5
System.out.println("Hello World!");
// "Hello World!"
System.out.print("Hello ");
System.out.print("World!");
// "Hello World!"
// This comment is just for developers to read
// and is ignored by the computer when running the program
/* This is a block comment that can span multiple lines
and will all be ignored when running the program until
the terminating characters are found */
Output Exercises
Open up intro_java_exercises in Eclipse
Exercise 1 - PrintNameAndHometown
Exercise 2 - PrintFunFact
Exercise 3 - PrintQuote
Exercise 4 - PrintBio
Types
Types are different kinds of data that a program needs to
run. Variable types can include things such as names,
prices, ages, songs or anything in your application.
There are a few basic (or primitive) types:
Numbers (int and double)
55, 23.5, 1.45, 10000
Strings
"Hello", "Bruce Wayne"
Boolean
true, false
Complex Types
In addition to basic types, there are also complex types.
These are many more types that represent more specific
types of data.
Examples include:
Colors
Color.RED, Color.GREEN
Date
Date d = new Date();
Currency
Currency dollars = new Currency("US");
Variables, cont'd.
Variables consist of a type, a name and a
value.
int someName;
We can then store any value of the type specified into the variable.
someName = 5;
Variables, cont'd.
Variables can be declared, assigned and
reassigned.
A variable is like a box or a placeholder that can
contain any value that matches it's type.
int x;
x = 5;
x = 7;
Declare
Assign
Reassign
int x;
x = 5;
x = 7;
Expressions
Variables and types can be composed into
expressions. A good example is performing arithmetic
with numbers. For example:
int result = (2 + 3) x 6; // 30
Variables Example
int x;
// declaring
x = 3;
// assigning
x = x + 3; // increment by 3
System.out.println(x);
// 6
int age = 19; // declaring and assigning
boolean canDrink = age > 21;
System.out.println(canDrink);
// false
String name = "Bruce";
name = name + " " + "Wayne";
System.out.println(name);
// "Bruce Wayne"
// Constants
final double PI = 3.1415;
Arrays
Arrays are data types that can contain
multiple values within a list that are accessed
sequentially.
Variables and data types discussed so far only allow us
to contain a single value at a given time.
Often in programs, we need to be able to build a list of
items that are all related. For example, a sequence of
numbers for the lottery or a set of names that need to
be invited to an event.
Arrays, cont'd
When an array is defined you must specify
the type and the number of items:
int[] numbers = new int[10]; // create array of 10 ints
// create array of 3 items
String[] names = { "Bob", "Suzie", "Emma" };
Arrays, cont'd
Arrays are just lists of items stored
sequentially in memory.
0
Bob
Suzie
Emma
John
Tim
Dan
Kelly
Joan
Arrays Example
// Arrays are accessed and modified using square brackets
int nums[]; // declaring
nums = new int[2]; // initializing
nums[0] = 5;
nums[1] = 6;
System.out.println(nums[1]);
// 6
String fullName;
String[] names = new String[2];
names[0] = "Bruce";
names[1] = "Wayne";
fullName = names[0] + " " + names[1];
System.out.println(fullName);
// "Bruce Wayne"
Variables Exercise
Open up intro_java_exercises in Eclipse
Exercise 1 - Integers
Exercise 2 - AgeComplimentor
Exercise 3 - Siri
Exercise 4 - BadDeclaration
Exercise 5 - BasicMath
Exercise 4 - BasicArrays
3. Methods
How a program organizes steps
Defining Methods
Methods (also known as functions) are small units of
code that can be named and then run within a program.
A method is a simple way to group code statements
together.
In essence, methods are a name (i.e printInfo)
assigned to label the lines of code within braces:
public void printInfo() {
System.out.println("My name is Joe");
System.out.println("My favorite color is Blue");
}
// Calling a method from main
printInfo();
Method Execution
public static void main(String args[]) {
// Calling a method
println("Step 1");
printInfo();
println("Step 3");
}
public void printInfo() {
println("My name is Joe");
println("Color is Blue");
}
Run Program
main
Step 1
printInfo()
My name is Joe
printInfo();
Color is blue
Step 3
End
Defining vs Calling
Let's understand the difference between defining a
method and calling a method.
Defining is giving a group of code statements a name
but does not make the code run.
Calling a method is when you run the code for a method
by referencing the name.
// defining, does not run unless called in main
public void someMethodName() {
int num = 3;
System.out.println("The number is " + num);
}
public static void main(String args[]) {
// running the code in a method
someMethodName()
}
Method Parameters
Methods can accept values called parameters
which can be accessed when they run.
When calling a method, we can pass along variable
values for the method to use.
The parameters are values that the method needs in
order to run successfully.
public void sayHello(String someName) {
System.out.println("Hi " + someName);
}
sayHello("Bill"); // Hi Bill
Return Values
Methods can return back a value after
completing a run.
When calling a method, we can return values back to
store for later use.
The return value can be stored in a variable and used
later in the program.
public int add(int n1, int n2) {
return n1 + n2;
}
int sum = add(5, 6); // sum is assigned to return value
System.out.println(sum); // 11
Methods
return method
type
name
parameters
Method code
contained within
open and close
braces
Run Program
main
int x = 5;
5, 3
return a + b;
Sum is 8
End
Printing vs Returning
In programming, there is an important difference
between printing a string and returning a string.
Consider these two methods:
public String getHello(String name) {
return "Hello " + name + "!";
}
String helloString = getHello("Billy");
System.out.println(helloString) + "!";
// Hello Billy!!
public void printHello(String name) {
System.out.println("Hello " + name + "!");
}
printHello("Ted");
// Hello Ted!
Methods examples
public void printFullName(String firstName, String lastName) {
System.out.println("Nice to meet you " + firstName + " " + lastName);
}
printFullName("Bob", "Smith");
// "Nice to meet you Bob Smith"
public String askAboutJob(String name, String job) {
return name + ", I see here you work as a " + job + "?";
}
String sentence = askAboutJob("Billy", "Doctor");
System.out.println(sentence);
// Billy, I see here you work as a Doctor?
sentence = askAboutJob("Joe", "Plumber");
System.out.println(sentence);
// Joe, I see here you work as a Plumber?
Methods exercise
Open up intro_java_exercises in Eclipse
Exercise 1 - BasicMethod
Exercise 2 - SayMyName
Exercise 3 - PrintRandomNumber
4. Loops
How a program repeats steps
Loops
Loops are used in cases where a task must be
repeated many times.
In programming, the same task must often be repeated
many times or done to many different items.
Loops help reduce the amount of code written. Often
loops will be used in conjunction with methods.
Enumeration (i.e accessing arrays)
Animations (i.e moving drawn objects)
Displaying data (i.e printing tabular data)
More Loops
The simplest type of loop is a for-loop which executes
a group of code statements a specified number of times.
As discussed earlier, loops help us to reduce duplication
by repeating the same set of code statements over and
over.
In addition to the for-loop, there is also the while-loop
and the do-while-loop.
These for-loops help us to execute a group of code
statements repeatedly until a particular condition is met.
For Loops
The syntax of a for loop:
Start i at 0
While < 10
Increment by 1
For Loops
for (int i = 0; i < 5; i=i+1) {
System.out.println(i * 2);
}
Tracking the Loop Code Path
# Runs
Value of i
Output
1st
2nd
3rd
4th
5th
int i = 0;
if (i < 5) {
System.out.println(i * 2);
i = i + 1;
// Run code again with new i
}
While Loops
While loops execute code repeatedly until a
specified condition is met.
While loop is simpler than a for-loop in many respects
because the loop simply repeats over and over until a
condition is no longer true.
Used when we want to repeat code for as long as an
arbitrary condition is true. A while loop does not require
us to know the bounds ahead of time.
While Loops
While loops execute code repeatedly until a
specified condition is met.
int count = 1;
Repeat while count is less than 100
Loops exercise
Open up intro_java_exercises in Eclipse
Exercise 1 - BasicLoop
Exercise 2 - CountingBackwards
Exercise 3 - PrintGrid
Exercise 4 - CountByFive
5. Conditional Execution
How a program decides between steps
Conditional Execution
If statements are about checking conditions and then
running different code depending on the result.
int testscore = 76;
if (testscore > 50) {
System.out.println("You passed");
}
Conditional Execution
There are often cases where you need to check multiple
conditions
This is supported with an if-else as well as an else
which executes if every other condition is false.
int testscore = 76;
if (testscore > 90) {
System.out.println("You got an A!")
} else if (testscore > 50) {
System.out.println("You passed");
} else {
System.out.println("You failed!");
}
Conditional Execution
If statements are about checking conditions and then
running different code depending on the result.
First
Condition
Else
Condition
Conditional Execution
If statements are about checking conditions and then
running different code depending on the result.
Yes
Win
No
Hit
Conditionals example
public void suggestGift(int age) {
if (age < 3) {
System.out.println("Toy Truck");
} else if (age < 15) {
System.out.println("Board Game");
} else if (age < 25) {
System.out.println("DVD");
} else {
System.out.println("Gift Card or Money");
}
}
public void canDrink(int age) {
if (age > 21) {
System.out.println("Yes!");
} else {
System.out.println("Not Yet!");
}
}
Conditionals exercise
Open up intro_java_exercises in Eclipse
Exercise 1 - FizzBizz
Exercise 2 - RomanNumerals
Exercise 3 - BlackJack
Exercise 4 - GCF
Classes
In programming, programs are often
organized using methods, arrays and loops.
In Java programming there is a higher level
of organization of code into "classes".
A class is like a noun (a person) and a
method is like a verb (walks around).
A class can contain many actions
(methods) as well as different types of data.
Classes
Classes describe complex types and are
composed of two aspects:
Fields - adjective (descriptions)
Methods - verb (actions)
A class defines a type of an object and is the
basis of object-oriented programming.
Classes, examples
The following lists some examples of classes,
with some of their fields and methods.
String
Methods: length(), compareTo(string)
User
Fields: name, birthday, tagline
Methods: followUser(user)
Book
Fields: title, ISBN, numPages
Methods: exportPdf(filename)
Classes, cont'd.
The following code block defines a simple class
for User:
public class User {
// fields
String name;
String pictureUrl;
String email;
// methods
public void printName() {
System.out.println("My name is: " + name);
}
}
Classes, cont'd.
Classes are a definition explaining to the
program about the attributes and actions of
a particular noun.
User has a
name
email
pictureUrl
Book has a
Fields
and can
printName
buyBook
title
author
numPages
and can
Methods
printTitle
setOwner
Instantiation
Instances, aka objects, are created via
instantiation using the "new" keyword.
// Declaration
User u;
// Instantiation (allocates memory)
u = new User();
u.name = "John Smith";
u.printName();
// My name is John Smith
Objects
A class can have many instances, which is to say that
there can be any number of "versions" of a class. Each
example is an object:
public class User {
// fields and methods defining a user
}
User joe = new User();
joe.name = "Joe";
joe.printName();
User billy = new User();
billy.name = "Billy";
billy.printName();
Classes Exercise 1
Open up intro_java_exercises in Eclipse
Exercise 1 - Follow instructions in
Ex1_Book
Constructors
Constructor methods are called when objects are created
to initialize the object.
public class User {
// Fields
String name;
// Constructor
public User(String name) {
this.name = name;
}
}
// new user's name is initially set to "Billy"
User billy = new User("Billy");
// we can still manually reassign a user's name later...
billy.name = "William";
Classes Exercise 2
Open up intro_java_exercises in Eclipse
Follow the instructions in Ex2_Constructors
and Ex3_User
Access Modifiers
Access modifiers control whether other classes can use a
particular field or invoke a particular method.
If a class, method or field is marked public, then that is
accessible by all classes everywhere in an app.
In contrast, if marked private, then the member is
accessible only within its own class.
Access modifiers are important also to document which
fields and methods are intended to act as a public
interface when using this class.
Access Modifiers
Access modifiers control whether other classes can use a
particular field or invoke a particular method.
public class Car {
public String model;
private int numCylinders;
public void setSpeed(int speed) { ... }
}
car = new Car();
car.numCylinders = 6; // FAILS
car.model = "Toyota"; // WORKS
car.setSpeed(55); // WORKS
Classes Exercise 3
Open up intro_java_exercises in Eclipse
Follow the instructions in Ex4_Access
Classes Exercise 4
Open up intro_java_exercises in Eclipse
Follow the instructions in
Ex5_StaticMethods
Classes example
public class iPhone {
private String version;
String driveSize;
static int screenWidth = 320;
public iPhone(String version, String driveSize) {
this.version = version;
this.driveSize = driveSize;
}
public String getVersion() {
return version;
}
}
7. Inheritance
How related classes share behavior
Inheritance
Class inheritance shares common fields and
methods across related classes.
Classes can help us to organize code, data and
methods into logical units which can manage their own
behaviors within a program.
Inheritance means that two related class definitions will
share the same blueprint variables and methods.
Inheritance, cont'd.
Consider the case of an application with Customer and
Seller classes. Customer purchase items sold by the
Seller.
These two classes share many properties (name, email,
etc) but have different roles in the system.
public class User {
// attributes and methods defining a user
}
public class Customer extends User {
// attributes and methods defining a customer
}
public class Seller extends User {
// attributes and methods defining a seller
}
Inheritance example
public class IOSDevice {
protected String version = "4G";
protected String driveSize = "16GB";
protected String screenSize;
public String getVersion() {
return version;
}
}
public class IPhone extends iOSDevice {
public IPhone() {
this.screenSize = "320 x 480";
}
}
public class IPad extends iOSDevice {
public IPad() {
this.screenSize = "768 x 1024";
}
}
Inheritance Exercise 1
Open up intro_java_exercises in Eclipse
Follow Instructions in Ex1_Animals
Follow Instructions in Ex2_Constructors
Overriding Methods
Overriding methods tweaks or replaces the
behavior of a method within the parent class.
When inheriting a parent class, certain behavior will
need to be modified in the subclass.
Overriding allows us to easily redefine the methods
inherited from a parent class.
This is especially useful for when a subclass has
similar but not identical behavior from the parent.
Overriding Methods
Overriding methods augments or replaces the
behavior of a method within the parent class.
public class iOSDevice {
public void startDevice() {
startBluetooth();
startWiFi();
}
}
public class iPhone extends iOSDevice {
public void startDevice() {
super.startDevice();
connectCellTower();
}
}
Inheritance Exercise 2
Open up intro_java_exercises in Eclipse
Follow Instructions in Ex3_Overriding
Overloading Methods
Overloading allows multiple versions of a
method with different parameters.
Java supports having multiple methods with the same
name but different parameters.
This is useful when we want to have a method behave
differently depending on the type of the arguments
passed.
Inheritance Exercise 3
Open up intro_java_exercises in Eclipse
Follow Instructions in Ex4_Overloading.txt
8. Collections
A better way to store lists
Collections
A collection is a dynamic list of data stored
within your application.
Many different types such as:
Collections
A collection is a dynamic list of data stored
within your application.
Collections contain certain shared methods (through
an interface) such as add, remove, and size which can
be called on any collections available within Java.
The two most important collections for us to understand
are ArrayList (dynamic array) and HashMap (key-value
store)
ArrayLists
ArrayList is a dynamically-sized Array of items
that can have items added or removed.
Basic arrays are useful but when we think of lists in the
real world, lists are often dynamic with new items being
added and then old items being removed.
ArrayLists, cont'd.
An ArrayList is very much like an array but with more
methods available and with no fixed size:
ArrayList<String> list = new ArrayList<String>();
ArrayList example
ArrayList<String> stringList = new ArrayList<String>();
stringList.add("Item");
for (int i = 0; i < stringList.size(); i++)
String item = stringList.get(i);
System.out.println("Item " + i + " : " + item);
}
stringList.remove(0);
System.out.println("ArrayList is " +
stringList.size() + " items long!");
ArrayList exercise
Open up intro_java_exercises in Eclipse
Follow Instructions in Ex1_BasicArrayList
Generics
Generics enable types (classes and interfaces)
to be parameters when defining classes,
interfaces and methods.
Generics, cont'd.
Generics allow us to parameterize the type to
allow that type to be defined at runtime.
Generics can be used to allow generic types in any
class or method but are most frequently seen as a part
of collections.
In our usage of collections (i.e ArrayList) and throughout
using Java, the concept of generics will appear from
time to time.
Defining Generics
Generics are defined using special uppercase
generic type placeholders.
public class Calculator<T> {
// T stands for "Type"
// t is the name of the variable
private T t;
public T
return
}
public T
return
}
}
// T becomes a placeholder for Integer
Calculator<Integer> calc = new Calculator<Integer>();
System.out.println(calc.add(5, 6)); // 11
Generics exercise
Open up intro_java_exercises in Eclipse
Follow Instructions in Ex2_Generics
HashMap
The most common collections in Java are ArrayList
and HashMap. We have already covered arrays, so
let's learn about hashes.
A HashMap is a collection of values which are not kept
in order but are instead kept by "associating" each value
to a 'key'.
For example, imagine you want to determine a rating for
a movie. You could attach the rating (value) to the
movie title (key).
HashMap<String, Integer> ratingMap = new HashMap<String, Integer>();
ratingMap.put("Dark Knight", 5);
ratingMap.put("Pirahna 3D", 1);
HashMap, cont'd.
HashMaps can serve a surprisingly large number of
uses within programs. In particular, anytime you need to
associate pairs of things together for each access by
the 'key', you want a hash.
The most important thing is to determine the correct
'key' and 'value'. The key should be the data you want
to be able to access by and the value should be the
resulting attached data. A good example is a phone
directory (key is the name, value is the number).
HashMaps example
// Store ratings for movies
HashMap<String, Integer> ratingMap = new HashMap<String, Integer>();
ratingMap.put("Dark Knight", 5);
ratingMap.put("Spiderman 2", 3);
ratingMap.put("Pirahna 3D", 1);
System.out.println("Dark Knight is rated a " + ratingMap.get("Dark Knight"));
// or numbers for people
HashMap<String, String> personMap = new HashMap<String, String>();
ratingMap.put("Bob", "555-555-4532");
ratingMap.put("Bill", "555-123-4743");
ratingMap.put("Drake", "555-345-6753");
System.out.println("Bob can be reached at " + ratingMap.get("Bob"));
HashMaps exercise
Open up intro_java_exercises in Eclipse
Follow Instructions in
Ex3_NamesAndAddresses
Follow Instructions in Ex4_RemoveOdd
9. Advanced Classes
Diving deeper into designing classes
Interfaces
When defining objects and classes, there is often
situations where multiple classes and objects of
unrelated type have certain shared expectations for
certain methods.
In cases where classes have the same expected
methods but do not actually share much behavior, you
should consider using interfaces.
At the simplest level, an interface is a series of
methods with empty bodies that can be 'implemented'
within a class definition. Once implemented, that class
has to then define every method that is contained
within the interface.
Interfaces, cont'd.
Interfaces provides a contract which guarantees
certain methods will be defined within any class that
implements it.
This is helpful when you have disparate (unrelated)
items or complex classes that should all have certain
methods to expose the same 'interface'.
public interface Friendly {
public void sayHello();
}
public class Person implements Friendly {
public void sayHello() {
System.out.println("Hi!");
}
}
Interfaces, cont'd.
A class can actually implement multiple interfaces and
follow all of their method requirements:
public interface Friendly {
public void sayHello();
}
public interface Athletic {
public void jog(int miles);
}
public class Person implements Friendly, Athletic {
public void sayHello() {
System.out.println("Hi!");
}
public void jog(int miles) {
System.out.println("I just jogged " + miles + " miles");
}
}
Interfaces example
public interface MobileDevice {
public String getScreenSize();
}
public class iOSDevice implements MobileDevice {
private String version = "4G";
private String driveSize = "16GB";
private String screenSize;
public String getScreenSize() {
return screenSize;
}
}
public class AndroidDevice implements MobileDevice {
private String version = "4G";
private String driveSize = "16GB";
private String fullScreenSize;
public String getScreenSize() {
return fullScreenSize;
}
}
Interfaces exercise
Open up intro_java_exercises in Eclipse
Follow Instructions in Ex1_Alarm
Abstract Classes
An abstract class is a class that cannot be
directly instantiated (created) but can be
inherited from by other classes.
Abstract vs Interface
What is the difference between an abstract
class and an interface?
Inner Classes
An inner class is a class that is defined within
another class.
An inner class is a part of the enclosing class and has
access to other fields or methods of the parent class.
Inner classes are used as a way to organize and group
classes together if they are only ever used together.
Small "helper" classes that are used only to make a
parent class work can be hidden from public access.
Inner Classes
An inner class only exists within the parent
class instance.
Inner classes have direct access to the parent object's
methods and fields.
Inner classes cannot exist independently outside the
outer class instance.
Static inner classes are called nested classes and
behave differently (no access to instance variables or
methods).
Anonymous Classes
An anonymous class is a class created on the
fly without a specified name.
Anonymous Classes
Anonymous classes are inline classes defined as part of a
larger block of code.
In this example, we are defining the behavior of a button
when clicked
Button aButton = getMyButton();
aButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// User clicked my button, do something here!
System.out.println("clicked!");
}
});
10. Addendum
Additional concepts for Android development
XML Documents
An XML Document is a file that contains a
description of some data.
XML Example
Anonymous classes can be created based
on any existing class or interface
<container>
<menu name="top-navigation">
<item title="Home"/>
<item title="Books" />
<item title="Account" />
</menu>
<content backgroundcolor="red">
<paragraph>First paragraph</paragraph>
<paragraph>Second paragraph</paragraph>
</content>
</container>
XML Documents
An XML Document contains a series of nested
nodes that describe something.
2 Children of Container
Container
Menu
Home
Books
3 Children of Menu
Account
Content
First Paragraph
Second Paragraph
2 Children of Content
Exception Handling
An exception is an object that represents an
unexpected event within our application.
Exception Handling
Exceptions are used to handle when problems
arise in an application.
Exceptions Example
Anonymous classes can be created based
on any existing class or interface
PrintWriter out = null;
try {
System.out.println("Entering" + " try statement");
out = new PrintWriter(new FileWriter("OutFile.txt"));
out.println("Hello World");
} catch (IOException e) {
System.err.println("Caught Error: " + e.getMessage());
} finally {
System.out.println("Closing PrintWriter");
out.close();
}
Wrapping Up
Summary and next steps
Concept Review
Variables
Loops
Conditionals
Arrays
Classes
Inheritance
Interfaces
Collections
Generics
Keep Learning
Form a small group or schedule a recurring
meetup.
Learn to read blog tutorials and
stackoverflow.com
Build more 2D games and apps.
Take next course, Introduction to Android.
Next Steps
We have covered many important parts of the Java
programming language.
All of these concepts we learned are critical parts of almost
any type of software development and are directly
applicable to Android mobile applications.
To learn Android development, we must build on all these
concepts and learn several new ones as well.
Introduction to Android
Introduction to Android is a 12 week course
covering the basics of developing Android
apps.
Android fundamentals
User Interfaces and Views
User Interactions
Creating Menus and Actions
Application Flows
Networking and Building Client Apps
Communicating with other Apps
Comments? Questions?
We love feedback