arrow_up

Table of Contents

Java

Variables

  • String
  • Integer
  • Double
  • Character
  • Boolean
  • Operators

    Math Operators

  • Addition
  • Subtraction
  • Multiplication
  • Division
  • Modulo
  • Increment
  • Decrement
  • Assignment Operators

  • Assignment
  • Addition and Assignment
  • Subtraction and Assignment
  • Multiplication and Assignment
  • Division and Assignment
  • Conditional Operators

  • Equal to
  • Not equal to
  • Less than
  • Greater than
  • Less than or equal to
  • Greater than or equal to
  • Logical Operators

  • And
  • Or
  • Not
  • Basics

  • What you need to know
  • Comments
  • Output
  • Input
  • Conditional Statements
  • Looping Statements

  • While
  • For
  • Do...While
  • C#

    Variables

  • String
  • Integer
  • Double
  • Character
  • Bool
  • Operators

    Math Operators

  • Addition
  • Subtraction
  • Multiplication
  • Division
  • Modulo
  • Increment
  • Decrement
  • Assignment Operators

  • Assignment
  • Addition and Assignment
  • Subtraction and Assignment
  • Multiplication and Assignment
  • Division and Assignment
  • Conditional Operators

  • Equal to
  • Not equal to
  • Less than
  • Greater than
  • Less than or equal to
  • Greater than or equal to
  • Logical Operators

  • And
  • Or
  • Not
  • Basics

  • What you need to know
  • Comments
  • Output
  • Input
  • Conditional Statements
  • Looping Statements

  • While
  • For
  • Do...While
  • Android

    Java

    Basics

  • What you need to know
  • Activity
  • Intent
  • User Interface

  • Button
  • TextView
  • EditText
  • Intermediate

  • Getting text from a widget
  • Alerting a user through an EditText
  • Toast
  • AlertDialog
  • Advanced

  • Hiding a keyboard
  • OnKeyListener
  • Notification
  • Java

    Variables

    These store data for processing.

    A variable is given a data type and a name (or identifier).

    A data type tells the system what kind of data it should store and a name or an identifier is set by a programmer to uniquely reference a specific data.

    See sample code below:

    String name = "Bob";

    int age = 18;

    double grade = 85.25;

    char gradeLetter = 'B';

    boolean isOnline = false;

    String

    Strings store a sequence of characters and are enclosed in double quotes.

    See sample code below:

    String name = "Bob";

    Integer

    Integers store whole numbers.

    The max value you can store in an integer is 2147483647.

    See sample code below:

    int age = 18;

    Double

    Doubles store floating-point numbers (or numbers with decimal values).

    The max value you can store in a double is approximately 1.7976931348623157 x 10308.

    See sample code below:

    double grade = 85.25;

    Character

    Characters store single alphanumeric characters and are enclosed in single quotes.

    See sample code below:

    char gradeLetter = 'B';

    Boolean

    Booleans only store two (2) possible values: true or false.

    See sample code below:

    boolean isOnline = false;

    Operators

    Every programming language provides a set of operators to use in manipulating variables.

    Math Operators

    See sample code below:

    int a = 6;

    int b = 5;

    int sum = a + b;

    int difference = a - b;

    int product = a * b;

    int quotient = a / b;

    int remainder = a % b;

    Addition

    The plus sign (+) is used to add two numbers.

    See sample code below:

    int a = 6;

    int b = 5;

    int sum = a + b;

    Subtraction

    The dash sign (-) is used to subtract two numbers.

    See sample code below:

    int a = 6;

    int b = 5;

    int difference = a - b;

    Multiplication

    The asterisk sign (*) is used to multiply two numbers.

    See sample code below:

    int a = 6;

    int b = 5;

    int product = a * b;

    Division

    The forward slash sign (/) is used to divide two numbers.

    See sample code below:

    int a = 6;

    int b = 5;

    int quotient = a / b;

    Modulo

    The percent sign (%) is used to divide two numbers and returns the remainder.

    See sample code below:

    int a = 6;

    int b = 5;

    int remainder = a % b;

    Increment

    The double plus sign (++) is used to increase the value of a number by one (1).

    See sample code below:

    int a = 6;

    a++;

    Decrement

    The double dash sign (--) is used to decrease the value of a number by one (1).

    See sample code below:

    int a = 6;

    a--;

    Assignment Operators

    See sample code below:

    int a = 6;

    a += 5;

    a -= 5;

    a *= 5;

    a /= 5;

    Assignment

    The equal sign (=) is used to assign a value to a variable.

    See sample code below:

    int a = 6;

    Addition and Assignment

    The plus and equal sign (+=) is used to add a value to a variable with the same data type and assign it to itself.

    See sample code below:

    int a = 6;

    a += 5;

    Subtraction and Assignment

    The dash and equal sign (-=) is used to subtract a value to a variable with the same data type and assign it to itself.

    See sample code below:

    int a = 6;

    a -= 5;

    Multiplication and Assignment

    The asterisk and equal sign (*=) is used to multiply a value to a variable with the same data type and assign it to itself.

    See sample code below:

    int a = 6;

    a *= 5;

    Division and Assignment

    The forward slash and equal sign (/=) is used to divide a value to a variable with the same data type and assign it to itself.

    See sample code below:

    int a = 6;

    a /= 5;

    Conditional Operators

    These operators always returns a boolean value.

    See sample code below:

    boolean a = 6 == 5;

    boolean b = 6 != 5;

    boolean c = 6 < 5;

    boolean d = 6 > 5;

    boolean e = 6 <= 5;

    boolean f = 6 >= 5;

    Equal to

    The double equal sign (==) is used to compare two values together and check if the left variable is equal to the right variable.

    See sample code below:

    boolean a = 6 == 5;

    Not equal to

    The exclamation point and equal sign (!=) is used to compare two values together and check if the left variable is not equal to the right variable.

    See sample code below:

    boolean a = 6 != 5;

    Less than

    The left bracket symbol (<) is used to compare two values together and check if the left variable is less than the right variable.

    See sample code below:

    boolean a = 6 < 5;

    Greater than

    The right bracket symbol (>) is used to compare two values together and check if the left variable is greater than the right variable.

    See sample code below:

    boolean a = 6 > 5;

    Less than or equal to

    The left bracket symbol and equal sign (<=) is used to compare two values together and check if the left variable is less than or equal to the right variable.

    See sample code below:

    boolean a = 6 <= 5;

    Greater than or equal to

    The right bracket symbol and equal sign (>=) is used to compare two values together and check if the left variable is greater than or equal to the right variable.

    See sample code below:

    boolean a = 6 >= 5;

    Logical Operators

    These operators always returns a boolean value.

    See sample code below:

    boolean a = true && false;

    boolean b = true || false;

    boolean c = !true;

    And

    The double ampersand symbol (&&) is used to combine multiple conditions.

    If both values are true, then the result is true.

    If one value is false, then the result is false.

    See sample code below:

    boolean a = true && false;

    Or

    The double pipe symbol (||) is used to combine multiple conditions.

    If one value is true, then the result is true.

    See sample code below:

    boolean a = true || false;

    Not

    The exclamation point (!) is used to reverse a condition.

    If a condition is true, then the result is false.

    If a condition is false, then the result is true.

    See sample code below:

    boolean a = !true;

    Basics

    Learning a programming language usually starts with using the console.

    What you need to know

    Creating simple programs in Java starts with putting code inside its main loop function.

    See sample code below:

    class ConsoleApp1 {

        public static void main(String[] args){

            // your code here

        }

    }

    Comments

    Comments are specific texts in a programming language that is ignored in a program's execution.

    See sample code below:

    // single-line comment


    /*

     * multi-line comment

     */

    Output

    See sample code below:

    System.out.println("Sample text");

    Input

    Java uses the Scanner package to get input from the console.

    The Scanner package is imported by calling import java.util.Scanner; at the top of your class.

    See sample code below:

    import java.util.Scanner;

    class ConsoleApp1 {

        public static void main(String[] args){

            Scanner scanner = new Scanner(System.in);

            String a = scanner.nextLine();

        }

    }

    Conditional Statements

    Conditional statements make use of the keywords if, else, and else if.

    The keyword if is always followed by a condition enclosed in parentheses.

    If the condition inside the parentheses is true, it will then execute the code it contains.

    Else, it will then execute the code inside the else block.

    The else if keyword simply combines both the if and else statement.

    See sample code below:

    if (6 == 5){

        // your code here

    } else if (6 < 5){

        // your code here

    } else {

        // your code here

    }

    Looping Statements

    Looping statements make use of the keywords for, while, and do.

    While

    The keyword while is always followed by a condition enclosed in parentheses.

    See sample code below:

    while (6 != 5){

        // your code here

    }

    For

    The keyword for is also followed by a statement enclosed in parentheses but requires three (3) statements inside the parentheses: initialization, condition, and update.

    See sample code below:

    for (int a = 0; a < 6; a++){

        // your code here

    }

    Do...While

    The keyword do acts the same as a while statment and is always accompanied by the while statement.

    The only difference being: do...while statements executes the code inside it first before checking the condition; and the while statements checks for the condition first before executing.

    See sample code below:

    do {

        // your code here

    } while (6 != 5);

    C#

    Variables

    These store data for processing.

    A variable is given a data type and a name (or identifier).

    A data type tells the system what kind of data it should store and a name or an identifier is set by a programmer to uniquely reference a specific data.

    See sample code below:

    string name = "Bob";

    int age = 18;

    double grade = 85.25;

    char gradeLetter = 'B';

    bool isOnline = false;

    String

    Strings store a sequence of characters and are enclosed in double quotes.

    See sample code below:

    string name = "Bob";

    Integer

    Integers store whole numbers.

    The max value you can store in an integer is 2147483647.

    See sample code below:

    int age = 18;

    Double

    Doubles store floating-point numbers (or numbers with decimal values).

    The max value you can store in a double is approximately 1.7976931348623157 x 10308.

    See sample code below:

    double grade = 85.25;

    Character

    Characters store single alphanumeric characters and are enclosed in single quotes.

    See sample code below:

    char gradeLetter = 'B';

    Bool

    Bools only store two (2) possible values: true or false.

    See sample code below:

    bool isOnline = false;

    Operators

    Every programming language provides a set of operators to use in manipulating variables.

    Math Operators

    See sample code below:

    int a = 6;

    int b = 5;

    int sum = a + b;

    int difference = a - b;

    int product = a * b;

    int quotient = a / b;

    int remainder = a % b;

    Addition

    The plus sign (+) is used to add two numbers.

    See sample code below:

    int a = 6;

    int b = 5;

    int sum = a + b;

    Subtraction

    The dash sign (-) is used to subtract two numbers.

    See sample code below:

    int a = 6;

    int b = 5;

    int difference = a - b;

    Multiplication

    The asterisk sign (*) is used to multiply two numbers.

    See sample code below:

    int a = 6;

    int b = 5;

    int product = a * b;

    Division

    The forward slash sign (/) is used to divide two numbers.

    See sample code below:

    int a = 6;

    int b = 5;

    int quotient = a / b;

    Modulo

    The percent sign (%) is used to divide two numbers and returns the remainder.

    See sample code below:

    int a = 6;

    int b = 5;

    int remainder = a % b;

    Increment

    The double plus sign (++) is used to increase the value of a number by one (1).

    See sample code below:

    int a = 6;

    a++;

    Decrement

    The double dash sign (--) is used to decrease the value of a number by one (1).

    See sample code below:

    int a = 6;

    a--;

    Assignment Operators

    See sample code below:

    int a = 6;

    a += 5;

    a -= 5;

    a *= 5;

    a /= 5;

    Assignment

    The equal sign (=) is used to assign a value to a variable.

    See sample code below:

    int a = 6;

    Addition and Assignment

    The plus and equal sign (+=) is used to add a value to a variable with the same data type and assign it to itself.

    See sample code below:

    int a = 6;

    a += 5;

    Subtraction and Assignment

    The dash and equal sign (-=) is used to subtract a value to a variable with the same data type and assign it to itself.

    See sample code below:

    int a = 6;

    a -= 5;

    Multiplication and Assignment

    The asterisk and equal sign (*=) is used to multiply a value to a variable with the same data type and assign it to itself.

    See sample code below:

    int a = 6;

    a *= 5;

    Division and Assignment

    The forward slash and equal sign (/=) is used to divide a value to a variable with the same data type and assign it to itself.

    See sample code below:

    int a = 6;

    a /= 5;

    Conditional Operators

    These operators always returns a bool value.

    See sample code below:

    bool a = 6 == 5;

    bool b = 6 != 5;

    bool c = 6 < 5;

    bool d = 6 > 5;

    bool e = 6 <= 5;

    bool f = 6 >= 5;

    Equal to

    The double equal sign (==) is used to compare two values together and check if the left variable is equal to the right variable.

    See sample code below:

    bool a = 6 == 5;

    Not equal to

    The exclamation point and equal sign (!=) is used to compare two values together and check if the left variable is not equal to the right variable.

    See sample code below:

    bool a = 6 != 5;

    Less than

    The left bracket symbol (<) is used to compare two values together and check if the left variable is less than the right variable.

    See sample code below:

    bool a = 6 < 5;

    Greater than

    The right bracket symbol (>) is used to compare two values together and check if the left variable is greater than the right variable.

    See sample code below:

    bool a = 6 > 5;

    Less than or equal to

    The left bracket symbol and equal sign (<=) is used to compare two values together and check if the left variable is less than or equal to the right variable.

    See sample code below:

    bool a = 6 <= 5;

    Greater than or equal to

    The right bracket symbol and equal sign (>=) is used to compare two values together and check if the left variable is greater than or equal to the right variable.

    See sample code below:

    bool a = 6 >= 5;

    Logical Operators

    These operators always returns a bool value.

    See sample code below:

    bool a = true && false;

    bool b = true || false;

    bool c = !true;

    And

    The double ampersand symbol (&&) is used to combine multiple conditions.

    If both values are true, then the result is true.

    If one value is false, then the result is false.

    See sample code below:

    bool a = true && false;

    Or

    The double pipe symbol (||) is used to combine multiple conditions.

    If one value is true, then the result is true.

    See sample code below:

    bool a = true || false;

    Not

    The exclamation point (!) is used to reverse a condition.

    If a condition is true, then the result is false.

    If a condition is false, then the result is true.

    See sample code below:

    bool a = !true;

    Basics

    Learning a programming language usually starts with using the console.

    What you need to know

    Creating simple programs in C# starts with putting code inside its main loop function.

    See sample code below:

    using System;

    namespace ConsoleApp1

    {

        internal class Program

        {

            static void Main(string[] args)

            {

                // your code here

            }

        }

    }

    Comments

    Comments are specific texts in a programming language that is ignored in a program's execution.

    See sample code below:

    // single-line comment


    /*

     * multi-line comment

     */

    Output

    See sample code below:

    Console.WriteLine("Sample text");

    Input

    See sample code below:

    string a = Console.ReadLine();

    Conditional Statements

    Conditional statements make use of the keywords if, else, and else if.

    The keyword if is always followed by a condition enclosed in parentheses.

    If the condition inside the parentheses is true, it will then execute the code it contains.

    Else, it will then execute the code inside the else block.

    The else if keyword simply combines both the if and else statement.

    See sample code below:

    if (6 == 5)

    {

        // your code here

    }

    else if (6 < 5)

    {

        // your code here

    }

    else

    {

        // your code here

    }

    Looping Statements

    Looping statements make use of the keywords for, while, and do.

    While

    The keyword while is always followed by a condition enclosed in parentheses.

    See sample code below:

    while (6 != 5)

    {

        // your code here

    }

    For

    The keyword for is also followed by a statement enclosed in parentheses but requires three (3) statements inside the parentheses: initialization, condition, and update.

    See sample code below:

    for (int a = 0; a < 6; a++)

    {

        // your code here

    }

    Do...While

    The keyword do acts the same as a while statment and is always accompanied by the while statement.

    The only difference being: do...while statements executes the code inside it first before checking the condition; and the while statements checks for the condition first before executing.

    See sample code below:

    do

    {

        // your code here

    }

    while (6 != 5);

    Android (Java)

    Basics

    Programming in Android using Java starts with using the Android Studio IDE.

    What you need to know

    To start creating applications for Android, we first need to create a new project.

    There will be a button available at the top of the starting page of Android Studio named "New Project".

    Android Studio New Project Button

    Android Studio New Project Button

    Creating a new project in Android Studio may be confusing for some people because of the multiple options given to us at the start.

    Android Studio New Project Dialog

    Android Studio New Project Dialog

    To keep things simple, we can start with the "Empty Views Activity" option

    Android Studio Empty Views Option

    Android Studio Empty Views Option

    Do note that other versions of Android Studio may not include this option.

    For that scenario, we can simply pick the "No Activity" option.

    Android Studio No Activity Option

    Android Studio No Activity Option

    After clicking next, we can then edit a few details of the project such as:

    - The name

    Android Studio Project Name

    Android Studio Project Name

    - The package name

    Android Studio Project Package Name

    Android Studio Project Package Name

    - The save location of the project

    Android Studio Project Save Location

    Android Studio Project Save Location

    - The programming language the project will use

    Android Studio Project Programming Language

    Android Studio Project Programming Language

    - The minimum Android version the project (or the application) support

    Android Studio Project Android Version

    Android Studio Project Android Version

    - And the build configuration language of the project.

    Android Studio Project Build Configuration Language

    Android Studio Project Build Configuration Language

    For now, we are going to be using these settings below:

    Android Studio Project Settings

    Android Studio Project Settings

    Upon clicking the "Finish" button, you will see this window pop up.

    Android Studio IDE

    Android Studio IDE

    Do note that you will need to let the IDE finish the Gradle project sync indicated near the top,

    Android Studio Gradle Sync at the Top

    Android Studio Gradle Sync at the Top

    and at the bottom of the window.

    Android Studio Gradle Sync at the Bottom

    Android Studio Gradle Sync at the Bottom

    After the Gradle has finished syncing, this will be the final look of the IDE.

    Android Studio IDE Final Look

    Android Studio IDE Final Look

    Do note that if you chose the "No Activity" option, you will not have the default "activity_main.xml" and "MainActivity.java" files at the start.

    Android Studio No Activity IDE Final Look

    Android Studio No Activity IDE Final Look

    To add a new activity, right-click on the "app" folder on the left and choose the "New > Activity > Empty Views Activity" option.

    Android Studio No Activity New Activity

    Android Studio No Activity New Activity

    You can choose to either change or ignore the settings on the pop-up window.

    Android Studio No Activity New Activity Settings

    Android Studio No Activity New Activity Settings

    After clicking the "Finish" button, make sure to edit the "AndroidManifest.xml" located at the folders at the left at "app > manifests > AndroidManifest.xml" and modify the "<activity>" tag for the Activity you just created just like this example below.

    Android Studio No Activity Manifest

    Android Studio No Activity Manifest

    This will serve as the default activity that will open as soon as your application starts.

    Activity

    Activities in Android represents a single screen in an application.

    An example of this is the "MainActivity.java" that is generated at the start of a new project.

    Android Studio Activity

    Android Studio Activity

    Every activity is composed of two (2) things.

    The corresponding Java and XML files.

    Android Studio Activity Composition

    Android Studio Activity Composition

    The Java file contains the back-end functions of the activity.

    Android Studio Activity Java

    Android Studio Activity Java

    The XML file contains the front-end functions of the activity.

    Android Studio Activity XML

    Android Studio Activity XML

    Intent

    An Intent represents an action you want an activity to do.

    Intents can be used to start activities.

    See sample code below:

    Intent intent = new Intent(CurrentActivity.this, ActivityToStart.class);

    startActivity(intent);

    Intents can also be used to pass data to activities.

    See sample code below:

    Intent intent = new Intent(CurrentActivity.this, ActivityToStart.class);

    intent.putExtra("data_name", "data_value");

    startActivity(intent);

    The data sent to the other activity using "putExtra()" can be retrieved using the "getIntent()" method.

    See sample code below:

    String data = getIntent().getStringExtra("data_name");

    The method to be used in getting your data should match the type of the data you sent.

    User Interface

    The XML file of an activity is where the user interface lives.

    Button

    A button represents a clickable element.

    See sample code below:

    <Button

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="Sample text"/>

    The code above will look like the example below.

    Android Studio Button

    Android Studio Button

    TextView

    A TextView element shows text.

    See sample code below:

    <TextView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="Sample text"/>

    The code above will look like the example below.

    Android Studio TextView

    Android Studio TextView

    EditText

    An EditText element gives a user an area to type in.

    See sample code below:

    <EditText

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:hint="Sample text"/>

    A hint acts as a placeholder to give a user some context about an element.

    The code above will look like the example below.

    Android Studio EditText

    Android Studio EditText

    Intermediate

    This section will talk about a few functions available in Android.

    Getting text from a widget

    Getting any text or content from any widget, such as TextViews and EditTexts, is very simple.

    See sample code below:

    EditText editText = findViewById(R.id.editTextId);

    String text = editText.getText().toString();

    Alerting a user through an EditText

    An EditText contains a "setError()" method to alert a user about an input.

    See sample code below:

    EditText editText = findViewById(R.id.editTextId);

    editText.setOnKeyListener(new View.OnKeyListener() {

        @Override

        public boolean onKey(View v, int keyCode, KeyEvent event) {

            if (editText.getText().toString().contains("1")){

                editText.setError("Input contains the number 1!");

            }

            return false;

        }

    }

    See preview below:

    Android Studio EditText Set Error

    Toast

    A Toast gives a user a simple feedback about an action that is performed in the app.

    See sample code below:

    Toast.makeText(this, "Sample text", Toast.LENGTH_SHORT).show();

    See preview below:

    Android Studio Toast

    AlertDialog

    A dialog in general is a small window that asks the user to make a decision or enter additional information.

    See sample code below:

    AlertDialog.Builder builder = new AlertDialog.Builder(this);

    builder.setTitle("Sample title");

    builder.setMessage("Sample text");

    builder.setPositiveButton("Done", (dialog, which) -> dialog.dismiss());


    AlertDialog alertDialog = builder.create();

    alertDialog.show();

    See preview below:

    Android Studio AlertDialog

    Advanced

    This section will talk about more advanced functions available in Android.