40
Java for Android Development

Android Development Course in HSE lecture #2

Embed Size (px)

DESCRIPTION

Java for Android Development

Citation preview

Page 1: Android Development Course in HSE lecture #2

Java for Android Development

Page 3: Android Development Course in HSE lecture #2

Comments

/* multilinecomment */

// singleline comment

/** Description * * @param input * @return output */

Page 4: Android Development Course in HSE lecture #2

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)

Page 7: Android Development Course in HSE lecture #2

boolean a;if (a == true) ...if (a == false) ...if (a) ...if (!a) ...

Page 8: Android Development Course in HSE lecture #2

switch

switch (code) {case 200:

// ...break;

case 404:// ...break;

default:// ...break;

}

Page 13: Android Development Course in HSE lecture #2

for-each

for (Object o : objects) {System.out.print(o.toString());

}

Page 16: Android Development Course in HSE lecture #2

String

● Immutable● StringBuilder

● Никогда не проверяйте равенство строк с помощью == !!!

Page 17: Android Development Course in HSE lecture #2
Page 18: Android Development Course in HSE lecture #2

Class

● class MyClass {...}

● instance (object)

● fields

● methods

● inner and nested classes

● this

● final

● static

Page 19: Android Development Course in HSE lecture #2
Page 20: Android Development Course in HSE lecture #2

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;

Page 22: Android Development Course in HSE lecture #2

Inheritance

● class MyClass extends BaseClass {...}

● super

● @Override

Page 23: Android Development Course in HSE lecture #2
Page 24: Android Development Course in HSE lecture #2

Abstract Classes and Methods

● abstract class MyClass {...}● abstract void myMethod {...}

Page 25: Android Development Course in HSE lecture #2
Page 26: Android Development Course in HSE lecture #2
Page 28: Android Development Course in HSE lecture #2
Page 29: Android Development Course in HSE lecture #2

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;

Page 30: Android Development Course in HSE lecture #2

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).

Page 31: Android Development Course in HSE lecture #2

hashCode()

● Метод hashCode() надо переопределять в каждом классе,

переопределяющем метод equals().

● Контракт метода hashCode():

■ Непротиворечивость (consistency), «осмысленно» возвращает то же число;

■ Равные по equals() объекты дают равные значения hashCode();

Page 32: Android Development Course in HSE lecture #2

Nested Classes

class OuterClass { ... static class StaticNestedClass { ... }}

MyOuterClass.MyStaticNestedClass myObject = new MyOuterClass.MyStaticNestedClass();

Page 33: Android Development Course in HSE lecture #2

Inner classes

class OuterClass { ... static class StaticNestedClass { ... } class InnerClass { ... }}

OuterClass.InnerClass innerObject = outerObject.new InnerClass();

Page 34: Android Development Course in HSE lecture #2

Anonymous classes

button.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {

//...}

});

Page 35: Android Development Course in HSE lecture #2

Enums

public enum Day { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY }

Page 40: Android Development Course in HSE lecture #2

Practice

● любая предметная область● наследование● абстрактный класс● интерфейс