Java for Android
 Development
Object-Oriented Programming Concepts
Comments
/* multiline
   comment */

// 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


if (a > 0) {
    // ...
} else if (a == 0) {
    // ...
} else {
    // ...
}
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 = 10
while (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() {
    @Override
    public 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
●   любая предметная область
●   наследование
●   абстрактный класс
●   интерфейс

Android Development Course in HSE lecture #2

  • 1.
    Java for Android Development
  • 2.
  • 3.
    Comments /* multiline comment */ // singleline comment /** Description * * @param input * @return output */
  • 4.
    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)
  • 5.
  • 6.
    If if (a >0) { // ... } else if (a == 0) { // ... } else { // ... }
  • 7.
    boolean a; if (a== true) ... if (a == false) ... if (a) ... if (!a) ...
  • 8.
    switch switch (code) { case 200: // ... break; case 404: // ... break; default: // ... break; }
  • 9.
    Loops ● while ● do-while ● for ● for-each
  • 10.
    while int n =10 while (n > 0) { n--; }
  • 11.
    do n = 10; do{ n--; } while (n > 0);
  • 12.
    for for (int i= 0; i < 100; i++) { //... }
  • 13.
    for-each for (Object o: objects) { System.out.print(o.toString()); }
  • 14.
  • 15.
  • 16.
    String ● Immutable ● StringBuilder ● Никогда не проверяйте равенство строк с помощью == !!!
  • 18.
    Class ● class MyClass {...} ● instance (object) ● fields ● methods ● inner and nested classes ● this ● final ● static
  • 20.
    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;
  • 21.
  • 22.
    Inheritance ● class MyClass extends BaseClass {...} ● super ● @Override
  • 24.
    Abstract Classes andMethods ● abstract class MyClass {...} ● abstract void myMethod {...}
  • 27.
    Interfaces ● interface ● classMyClass implements MyInterface
  • 29.
    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;
  • 30.
    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).
  • 31.
    hashCode() ● Метод hashCode() надо переопределять в каждом классе, переопределяющем метод equals(). ● Контракт метода hashCode(): ■ Непротиворечивость (consistency), «осмысленно» возвращает то же число; ■ Равные по equals() объекты дают равные значения hashCode();
  • 32.
    Nested Classes class OuterClass{ ... static class StaticNestedClass { ... } } MyOuterClass.MyStaticNestedClass myObject = new MyOuterClass.MyStaticNestedClass();
  • 33.
    Inner classes class OuterClass{ ... static class StaticNestedClass { ... } class InnerClass { ... } } OuterClass.InnerClass innerObject = outerObject.new InnerClass();
  • 34.
    Anonymous classes button.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { //... } });
  • 35.
    Enums public enum Day{ SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY }
  • 36.
    Exceptions ● try ● catch ● finally ● throw
  • 37.
    Collections ● List ● Set ● Map ● Queue ● Stack ● ...
  • 38.
    Extra topics ● Reflection ●Threading ● Generics
  • 39.
    Literature ● Cay HorstmannCore Java ● Bruce Eckel Thinking in Java
  • 40.
    Practice ● любая предметная область ● наследование ● абстрактный класс ● интерфейс