android development course in hse lecture #2
DESCRIPTION
Java for Android DevelopmentTRANSCRIPT
Java for Android Development
Object-Oriented Programming Concepts
Comments
/* multilinecomment */
// singleline comment
/** Description * * @param input * @return output */
Primitive Data Types
● byte (8-bit signed integer between -128 and 127)
● short (16-bit signed integer between -32,768 and 32,767)
● int (32-bit signed integer between -2,147,483,648 and 2,147,483,647)
● long (64-bit signed integer between -9,223,372,036,854,775,808 and 9,223,372,036,854,775,807)
● float (32-bit floating point number)
● double (64-bit floating point number)
● boolean (true and false)
● char (single 16-bit Unicode character)
Operators
if (a > 0) {// ...
} else if (a == 0) {// ...
} else {// ...
}
If
boolean a;if (a == true) ...if (a == false) ...if (a) ...if (!a) ...
switch
switch (code) {case 200:
// ...break;
case 404:// ...break;
default:// ...break;
}
Loops
● while● do-while● for● for-each
while
int n = 10while (n > 0) {
n--;}
do
n = 10;do {
n--;} while (n > 0);
for
for (int i = 0; i < 100; i++) {//...
}
for-each
for (Object o : objects) {System.out.print(o.toString());
}
Branching statements
● break● continue● return
Arrays
String
● Immutable● StringBuilder
● Никогда не проверяйте равенство строк с помощью == !!!
Class
● class MyClass {...}
● instance (object)
● fields
● methods
● inner and nested classes
● this
● final
● static
import
● import java.util.List;● import org.apache.http.HttpEntity;● import org.json.JSONObject;● import android.content.SharedPreferences;● import com.inflow.model.Feed;● import java.io.IOException;
Access Modifiers
Inheritance
● class MyClass extends BaseClass {...}
● super
● @Override
Abstract Classes and Methods
● abstract class MyClass {...}● abstract void myMethod {...}
Interfaces
● interface● class MyClass implements MyInterface
java.lang.Object
○ public String toString();
○ public boolean equals(Object obj);
○ public native int hashCode();
○ protected native Object clone() throws CloneNotSupportedException;
○ protected void finalize() throws Throwable;
○ public final native Class<?> getClass();
○ public final native void notify();
○ public final native void notifyAll();
○ public final native void wait(long timeout) throws InterruptedException;
○ public final void wait(long timeout, int nanos) throws InterruptedException;
○ public final void wait() throws InterruptedException;
equals()
● Рефлексивность: x.equals(x) -> true при x не null;
● Симметричность: x.equals(y) <- -> y.equals(x) при x,y не null;
● Транзитивность: x.equals(y), y.equals(z) -> x.equals(z) при x,y,z не null;
● Непротиворечивость (consistency) «одинаковых» вызовов;
● x.equals(null) – всегда false (при x не null).
hashCode()
● Метод hashCode() надо переопределять в каждом классе,
переопределяющем метод equals().
● Контракт метода hashCode():
■ Непротиворечивость (consistency), «осмысленно» возвращает то же число;
■ Равные по equals() объекты дают равные значения hashCode();
Nested Classes
class OuterClass { ... static class StaticNestedClass { ... }}
MyOuterClass.MyStaticNestedClass myObject = new MyOuterClass.MyStaticNestedClass();
Inner classes
class OuterClass { ... static class StaticNestedClass { ... } class InnerClass { ... }}
OuterClass.InnerClass innerObject = outerObject.new InnerClass();
Anonymous classes
button.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {
//...}
});
Enums
public enum Day { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY }
Exceptions
● try● catch● finally● throw
Collections
● List● Set● Map● Queue● Stack● ...
Extra topics
● Reflection● Threading● Generics
Literature
● Cay Horstmann Core Java● Bruce Eckel Thinking in Java
Practice
● любая предметная область● наследование● абстрактный класс● интерфейс