Week 2 Class 2
Android: Java / Kotlin Basics
Homework Discussion
Reviewing PR
Goal of Today
Types of Classes in Kotlin
1. data class -> OOPs
2. object class
3. Library vs Framework?
4. Android App UI -> Image set with a button
What is a class?
1. Blueprint or a Template to create objects 2. Classes encapsulate data for the object
What is a class?
Solution of Messy If statements / Switch
Object Oriened Programming:
1. Inheritance
2. Polymorphism
3. Abstraction
Refer: Week 1 repo
github.com/iHarnoor/Week1Android
Converting Java code to a library
Step | Java | Kotlin |
---|---|---|
1. Compile Source Code | javac YourJavaFile.java | kotlinc YourKotlinFile.kt -include-runtime -d YourOutputJar.jar |
2. Create Manifest (Optional) | Create MANIFEST.MF | Create MANIFEST.MF |
3. Package into JAR | jar cvf YourLibrary.jar -C /path/to/compiled/classes . | jar cvf YourLibrary.jar -C /path/to/compiled/classes . |
4. Include Manifest (Optional) | jar cvfm YourLibrary.jar MANIFEST.MF -C /path/to/compiled/classes . | jar cvfm YourLibrary.jar MANIFEST.MF -C /path/to/compiled/classes . |
5. Verify the JAR | jar tf YourLibrary.jar | jar tf YourLibrary.jar |
Library vs Framework?
Android Revision (week2)
Constraint Layout
ImageView
Button Click
Compile-Time vs Run-Time Error
Which is Better?
Compile-Time vs Run-Time Error
println("Hello, world!"
a = 10 b = 0 result = a / b # Division by zero error (ZeroDivisionError)
lateinit var vs null object
Initialization | Property is uninitialized until explicitly initialized using lateinit. | Property is initialized to null by default. |
Nullable Property | No need for a ? after the property type, as it's not nullable. | Property is implicitly nullable (requires ? after the property type). |
Null Safety | Risky if not properly initialized; accessing an uninitialized lateinit property will result in a runtime exception. | Safe by default, as properties are initialized to null. Null checks or safe calls (?.) are required when accessing. |
Initialization Responsibility | Developer must ensure that the property is initialized before use. | No obligation to explicitly initialize the property; it can remain null. |
Use Cases | - Used when you can guarantee that the property will be initialized before being accessed. - To avoid nullable types and null checks, especially for properties that are expected to always have a value. | - Used when there's a possibility that the property might not be initialized immediately or can be left as null intentionally. - When you need to handle null values explicitly. |
without Lamda
public class Main {
public static void main(String[] args) {
Adder adder = new Adder();
int result = adder.add(5, 7);
System.out.println("Result of adding 5 and 7 is: " + result);
}
}
class Adder {
public int add(int a, int b) {
return a + b;
}
}
fun main() {
val add: (Int, Int) -> Int = { a, b -> a + b }
val result = add(5, 7)
println("Result of adding 5 and 7 is: $result")
}
with Lamda in Kotlin
ImageView and set Image in Android
1. Which is faster to share Image: Base64 or Downloading?
2. Why use Glide?
Using Library: Glide
1. Never reinvent wheel
2. Use Documentations
3. Gradle import
https://github.com/bumptech/glide
Add Permission
Add internet permisson
I Root cause (1 of 1)
java.lang.SecurityException: Permission denied (missing INTERNET permission?)
at java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:150)
at java.net.Inet6AddressImpl.lookupAllHostAddr(Inet6AddressImpl.java:103)
at java.net.InetAddress.getAllByName(InetAddress.java:1152)
at com.android.okhttp.Dns$1.lookup(Dns.java:41)
at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:178)
at com.android.okhttp.internal.http.RouteSelector.nextProxy(Ro
Trick of the Day
Declarative vs Imperative
SQL vs Java
SELECT * FROM customers WHERE state = 'CA';
public String getCustomers() {
return "Customer1"
}
Week 2 Class 2: Kotlin Basics + Creating our First App (RestAPI + GLide
By Harnoor Singh
Week 2 Class 2: Kotlin Basics + Creating our First App (RestAPI + GLide
- 281