2. What you we learn
Should learn the basics of the Java language
All Java language needed for Android development
Object-oriented software basics
3. What is Java?
Java is a language and a platform originated by
Sun Microsystems.
Java’s syntax is partly patterned after the C and
C++ languages
Help shorten the learning curve.
4. Classes
Object-oriented applications represent entities as
objects.
Each object encapsulates an entity’s attributes and
behaviors.
Objects do not pop out of thin air; they must be
instantiated (created) from something.
Languages such as C++ and Java refer to this
“something” as a class.
5. Declaring a Class
Declares a class
named
CheckingAccount.
By convention, a
class’s name begins
with an uppercase
letter.
class CheckingAccount
{
// fields
// methods
// other declarations
}
6. Fields
After declaring a class, you can declare variables
in the class’s body.
Entity attributes
Class attributes
7. Declaring Fields
Declares two fields
name owner and
balance.
By convention, a field’s
name begins with a
lowercase letter
class CheckingAccount
{
String owner;
int balance;
int theCounter;
}
TYPE NAME
8. Java Primitive Types
Primitive Type Reserved Size Min Max
Boolean boolean -- -- --
Character char 16 bit
Byte Integer byte 8-bit -128 127
Short Integer short 16-bit -2^15 2^15 -1
Integer int 32-bit -2^31 2^31 -1
Long Integer long 64-bit -2^63 2^63 - 1
Floating-point float 32-bit IEEE 754 IEEE 754
Double Precision floating-point double 64-bit IEEE 754 IEEE 754
9. Arrays
a multi-value variable
each element holds
one value.
ex. of array based field
class WeatherData
{
String country;
String[ ] cities;
double[ ] [ ] temp;
}
ARRAY TYPE
10. Static Fields
counter is a class field.
a field associates with
a class instead of the
class’s object.
one object associate
with all created class of
the same type
class CheckingAccount
{
String owner;
int balance;
static int counter;
}
KEYWORD
11. Initializing Fields
Is it common to initialize an instance field to a
value.
fields are initialized to default values when the
object is created.
String = “”;
int = 0;
objects = null;
13. Expressions &
Operators
Operator Symbol
Addition +
Array Index [ ]
Assignment =
Bitwise &
Bitwise complement ~
Bitwise exclusive OR ^
Bitwise inclusive OR |
Cast ( type )
Compound assignment
+=, -=, *=, /=, %=, &=, |=, ^=,
<<=, >>=, >>>=
14. Expressions &
Operators Continued...
Operator Symbol
Conditional ?:
Conditional AND &&
Conditional OR ||
Division /
Equality “==”
Inequality !=
Left shift <<
Logical AND &
Logical complement !
Logical exclusive OR ^
15. Expressions &
Operators Continued...
Operator Symbol
Logical inclusive OR |
Member access .
Method call ( )
Multiplication *
Object creation new
Decrement --
Increment ++
Relational greater than >
Relational greater than equal
to
>=
16. Expressions &
Operators Continued...
Operator Symbol
Relational less than <
Relational less than equal to <=
Relational type checking instanceof
Remainder %
Signed right shift >>
String concatenation +
Subtraction -
Unary minus -
Unary plus +
Unsigned right shift >>>
17. Casting
Casting is used to
convert one type to
another.
Only certain types can
be casted to another
type.
char c = ‘A’;
// incorrect way
byte b = c;
// correct way
byte b = (byte) c;
CAST
18. Read-only Fields
final keyword
final used for fields that
are read-only.
this field must be
initialized in the
constructor or field’s
declaration.
constant can be
accomplished by using
static with final.
class Employee
{
final int AGE = 26;
// constant ex.
final static int RETIRE_AGE = 65;
}
19. Declaring Methods
Declare methods within a
class’s body.
Should return a type.
followed by an identifier
that names the method.
followed by a parameter
list.
followed by a body.
class CheckingAccount
{
String owner;
int balance;
static int counter;
void printBalance()
{
// function
}
}
21. Decisions & Loops
if statement
switch statement
for loops
while loops
do while loops
break and continue
22. If statements
void printBalance()
{
if (balance < 0)
System.out.println(balance);
else if (balance == 0)
System.out.println(“zero balance”);
else
System.out.println(balance * balance);
}
23. Switch Statement
switch (selector expression)
{
case value1: statement1 [break;]
case value2: statement2 [break;]
case valueN: statementN [break;]
[default: statement]
}
int balance = 5;
switch(balance)
{
case 1: balance = 42; break;
case 2: balance = 43; break;
default: balance = 99;
}
24. For Loops
for ([ initialize ]; [ test ]; [ update ])
statement;
public static void doSomething(int size)
{
for (int i = 0; i < size; i++)
{
// inside the for loop
}
}
25. While Loops
while (Boolean expression)
statement
int ch = 0;
while (ch != ‘C’ && ch != ‘c’)
{
System.out.println(“Press C or c to continue”);
ch = System.in.read();
}
28. Constructors
Constructors are named blocks of code, declared in
class bodies for constructing objects by initializing
their instance fields and other developer needs.
class CheckingAccount
{
String owner;
int balance;
// Constructor start
CheckingAccount(String acctOwner, int acctBalance)
{
owner = acctOwner;
balance = acctBalance;
}
}
29. Access Control
Public
A field, method, or constructor that is accessible from anywhere.
A source file can only contain one public class per source file.
Protected
A field, method, or constructor that is accessible from all classes in of subclasses.
Private
A field, method, or constructor that is can not be accessed beyond the class in which it is
declared.
Package-private ?
30. Example of Access
public class Employee
{
private String name;
public Employee(String name)
{
setName(name);
}
public void setName(string empName)
{
name = empName;
}
}
31. Creating Objects and
Arrays
Employee emp = new Employee(“John”, “Doe”);
OBJECT TYPE CREATE MEMORY SPACE AND
RETURN OBJECT’S REFERENCE
CONSTRUCTOR
32. Accessing Fields
// Create object
Account account = new Account(23423);
// Call the field
int id = account.customerId;
// id contains the number 23423
PUBLIC FIELD
33. Calling Methods
// Create object
Account account = new Account(23423);
// Call the method
int id = account.getCustomerId(); // id = 23423
// set the id to 87873
account.setCustomerId(87873);
id = account.getCustomerId(); // id = 87873
PUBLIC METHOD
PUBLIC METHOD
34. Chained Instance
Method Calls
Account myAccount = new Account(9723);
// multiple method calls
myAccount.deposit(1000).printBalance();
PUBLIC METHOD #1PUBLIC METHOD #2
35. Garbage Collections
Objects are created via reserved word new
how are they destroyed?
Garbage collector is code that runs in the
background and checks for unreferenced objects.
When it discovers an unreferenced object, the
garbage collector removes it from the heap
(memory)
37. Inheritance or
Extending
public abstract class Animal
{
public abstract void eat();
}
public class Bird extends Animal
{
@Override
public final void eat()
{
}
}
39. Runtime Type
Identification
public class A
{
// stuff
}
void main( )
{
int h = 0;
A obj = new A( );
if ( ( h instanceof A) || ( obj instanceof A ) )
{
// Do something
}
}
40. Interfaces
Interfaces are like contracts
An agreement between two class or parties that
says these methods and properties are to be in
both classes and will not change.
Good for two classes that have nothing in common
to have common methods and properties.
43. Anonymous Classes
abstract class Speaker
{
abstract void speak();
}
public class ACDemo
{
public ACDemo()
{
new Speaker()
{
void speak()
{
System.out.println(“Hello”);
}
}
.speak();
}
}
44. Local Classes
public class EnclosingClass
{
public void m (final int x)
{
final int y = x*2;
class LocalClass
{
int a = x;
int b = y;
}
}
}
45. Packages
Packages are the same as namespaces in C++
A package is a unique namespace that can contain
a combination of top-level class, types, and sub-
packages.
Package name must be unique
46. Importing
import is the same as #include in C++
Includes a package to use with the current file.
Uses the import keyword
49. ArrayList
// Create new ArrayList
ArrayList<String> listOfStuff = new ArrayList<String>();
// Add items to list
listOfStuff.add(“NewString”);
// Get item
String s = list.get(0);