Java Android App Development Course, Introduction to Java Language

In Android app development, Java is one of the most important programming languages. While Google has been developing the Android platform, Java has established itself as the core language. Understanding the advantages and basic concepts of Java can greatly assist in Android app development. This course will start with the fundamental concepts of the Java language and progressively explain the basics to advanced topics for Android app development.

1. What is Java?

Java is a general-purpose programming language that was developed by Sun Microsystems (now Oracle) in 1995. It is an object-oriented programming (OOP) language that makes the code more structured and reusable. Java has the following key features:

  • Platform Independence: Once written, the code can run on any platform through the JVM (Java Virtual Machine).
  • Object-Oriented: It allows for structured management of data and methods using classes and objects.
  • Robust Memory Management: Java efficiently performs memory management with automatic garbage collection.
  • Diverse Libraries: Java provides a wide range of standard libraries and APIs for developers to use conveniently.
  • Multithreading Support: It facilitates easy management of multiple threads, making concurrent programming easier.

2. Basic Syntax of Java

The syntax of Java is simple and intuitive. Let’s take a look at the basic syntax of Java through example code.

2.1 Hello World Example


public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}

    

The above code shows the basic structure of Java. It defines a class, and the program starts through the main method. The System.out.println() method is responsible for printing messages to the console.

2.2 Variables and Data Types

Java has primitive data types and reference data types. Primitive data types include int, float, double, char, boolean, etc., while reference data types represent complex data structures like classes and arrays.


public class VariableExample {
    public static void main(String[] args) {
        int age = 25;
        double height = 175.5;
        char initial = 'A';
        boolean isStudent = true;

        System.out.println("Age: " + age);
        System.out.println("Height: " + height);
        System.out.println("Initial: " + initial);
        System.out.println("Is a student: " + isStudent);
    }
}

    

2.3 Operators

Java provides various operators to process data.

  • Arithmetic Operators: +, -, *, /, %
  • Comparison Operators: ==, !=, >, <, >=, <=
  • Logical Operators: &&, ||, !

public class OperatorExample {
    public static void main(String[] args) {
        int a = 10;
        int b = 20;
        boolean result;

        result = (a < b) && (b > 15);
        System.out.println("Result: " + result);
    }
}

    

2.4 Control Statements

Control statements are used to control the flow of the program. Java includes conditional statements (if, switch) and loops (for, while, do-while).


public class ControlFlowExample {
    public static void main(String[] args) {
        int num = 5;

        if (num > 0) {
            System.out.println(num + " is a positive number.");
        } else {
            System.out.println(num + " is a negative number.");
        }

        for (int i = 1; i <= 5; i++) {
            System.out.println("Iteration: " + i);
        }
    }
}

    

3. Object-Oriented Programming (OOP)

Object-oriented programming is a paradigm that solves problems from the perspective of objects. Java supports the four main characteristics of OOP: encapsulation, inheritance, polymorphism, and abstraction.

3.1 Classes and Objects

A class is a blueprint that defines an object. An object is an instance created based on a class.


class Dog {
    String name;
    int age;

    void bark() {
        System.out.println(name + " barks.");
    }
}

public class Main {
    public static void main(String[] args) {
        Dog dog = new Dog();
        dog.name = "Bongchi";
        dog.age = 3;
        dog.bark();
    }
}

    

3.2 Inheritance

Inheritance is a feature that allows a child class to inherit properties and methods from an existing class (parent class).


class Animal {
    void eat() {
        System.out.println("Eating.");
    }
}

class Cat extends Animal {
    void meow() {
        System.out.println("Meow!");
    }
}

public class Main {
    public static void main(String[] args) {
        Cat cat = new Cat();
        cat.eat();
        cat.meow();
    }
}

    

3.3 Polymorphism

Polymorphism is the ability to use different implementations through the same interface.


class Bird {
    void fly() {
        System.out.println("The bird is flying.");
    }
}

class Ostrich extends Bird {
    void fly() {
        System.out.println("The ostrich cannot fly.");
    }
}

public class Main {
    public static void main(String[] args) {
        Bird myBird = new Ostrich();
        myBird.fly();
    }
}

    

3.4 Abstract Classes and Interfaces

An abstract class is an incomplete class, while an interface is a collection of methods that a class must implement.


abstract class Shape {
    abstract void draw();
}

class Circle extends Shape {
    void draw() {
        System.out.println("Drawing a circle");
    }
}

public class Main {
    public static void main(String[] args) {
        Shape shape = new Circle();
        shape.draw();
    }
}

    

4. Android App Development with Java

Now, let's explore how to develop Android apps using Java. We will install Android Studio and create a simple app.

4.1 Setting Up the Android Development Environment

To develop Android apps, you need to install Android Studio. After installing Android Studio, create a new project.

4.2 Creating a Simple "Hello World" App

First, after creating a new project in Android Studio, take a look at the provided "Hello World" app.


package com.example.helloworld;

import android.os.Bundle;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextView textView = findViewById(R.id.text_view);
        textView.setText("Hello, Android!");
    }
}

    

The above code displays the text "Hello, Android!" in the main activity.

4.3 Setting Up Layouts

The UI of Android is primarily defined in XML. Below is an example of a basic layout file.




    


    

5. Conclusion

In this course, we covered various topics from the basic concepts of the Java language to object-oriented programming and Android app development. Java is a very important language for Android development, and a thorough understanding and practice are required. Continue studying and practicing for a deeper understanding of Java and Android. In the next course, we will cover various functionality implementations for Android apps.

6. References

  • Java Programming Language: https://www.oracle.com/java/technologies/javase/javase-jdk8-downloads.html
  • Android Developer Site: https://developer.android.com/
  • Principles of Object-Oriented Programming: https://www.tutorialspoint.com/java/java_object_oriented.htm