Published on

Java Quick Guide

Authors

Overview

If you have a general understanding of programming, then all you need to start programming in Java is a simple quick guide.

Creating Files

Java files end in .java. All java files must have a class:

public class Main {

}

The class name must be the name of the file (with the .java omitted).

Variables

In Java, variables must be preceded by a type. Types include:

  • boolean: a variable that is always either true or false.
  • int: a number that is not a decimal (Ex. 1, 0, -1)
  • String: text (Ex. "hello")
  • char: a single character (Ex. 'A')
  • float: a number that can be a decimal (Ex. -1.0, 7.2)
  • double: a number that can be a decimal (stores twice as many digits as float)

Before the variable type, you may indicate if the variable is private or public. Private variables can not be accessed outside of the class, whereas public variables can. Variables can also be indicated to be static. static variables are shared among all objects at the class level.

Functions

Java Functions have a type, can be declared as private or public, and can be declared as static (all as metioned above).

public class Main {
    static int a = 5;
    static int b = 10;

    int add() {
        return a + b;
    }
}

The return statement is required for functions of a type. The two ints in the example above are static so that they can be accessed by the add() function. Functions that don't return a type are implemented with type void. Here's an example:

public class Main {
    static int a = 5;
    static int b = 10;
    static int c;

    void add() {
        c = a + b;
    }
}

When a Java program is run, the main function is run. Here's an example:

public class Main {
    static int a = 5;
    static int b = 10;

    static int add() {
        return a + b;
    }

    public static void main(String[] args) {
        System.out.println(add());
    }
}

When this program is run, the int 15 will be printed out (System.out.println() is the function for printing). Note: the syntax public static void main(String[] args) {} is requred for the main function.

Functions can also take in parameters. Example:

public class Main {
    static int add(int a, int b) {
        retunr a + b;
    }

    public static void main(String[] args) {
        System.out.println(add(5, 10));
    }
}

Objects

You can create an object by using a constructor. Example:

public class Object {
    public Object() {}
}
public class Main {
    Object a = new Object();
}

Note: Main and Object are in two different files. Here's an example of how to use an Object:

package folder;

public class Calculator {
    static int a;
    static int b;

    public Calculator(int ap, int bp) {
        a = ap;
        b = bp;
    }

    public static int add() {
        return a + b;
    }

    public static int multiply() {
        return a * b;
    }

    public static int subtract() {
        return a - b;
    }

    public static double divide() {
        return a / b;
    }
}
import Calculator.java;

public class Main {
    static Calculator calculator = new Calculator(5, 10);

    public static void main(String[] args) {
        System.out.println(calculator.add());
        System.out.println(calculator.multiply());
        System.out.println(calculator.subtract());
        System.out.println(calculator.divide());
    }
}

This would return:

15
50
-5
0.5

Note: the above code uses imports. Read below section to learn more.

Importing Code

To use an object from a different file, you must import it.

import folder/Calculator.java;

public class Main {}

In this example, Main.java is in the folder "folder." If Main.java and Calculator.java were in the same folder, you could import a package:

package folder;

public class Main{}

If Statements

In Java, if statements check a condition. They can also include if else statements and/or else statements. Example:

if (x > 10) {
    System.out.println("x is greater than ten.");
} else if (x < 10) {
    System.out.println("x is less than 10.")
} else {
    System.out.println("x is equal to 10.")
}

Conditions in if statements are booleans. They can use a boolean variable or an equality (Ex. x > 6, x == 6, x != 6). Switch statements are alternatives to if statements for when you are checking for many different values of a variable. Example:

switch(day) {
    case 1:
        System.out.println("It's Monday!");
        break;
    case 2:
        System.out.println("It's Tuesday!");
        break;
    case 3:
        System.out.println("It's Wednesday!");
        break;
    case 4:
        System.out.println("It's Thursay!");
        break;
    case 5:
        System.out.println("It's Friday!");
        break;
    case default:
        System.out.println("It's the weekend!");
        break;
}

Note: default is a catch-all case for anything that is not one of the above cases. The break key word tells the code to jump to the end of the switch statement after the case returns true.

Arrays

Arrays are another variable type. They are essentaily a list of variables. Example:

public class Main {
    static char[] alphabet = {'a', 'b', 'c', 'd', 'e'};

    public static void main(String[] args) {
        System.out.println(alphabet[0]);
    }
}

This would return a. Each value of an array has an index, starting at 0 then going up by one. In the above example, we print the value at index 0 of the array. You can change the value of an index of the array, but once it has been initialized, you can't add or remove indexes.

Loops

In Java, there are for loops, while loops, and for each loops. Here's an example of a while loop:

while (x < 3) {
    x++;
}

In the above example, the while statement takes in a boolean condition and repeats the code inside it until the consition is true. Note: The ++ is an increment operator, which sets x equal to x + 1. Here's an example of a for loop:

for (int i = 0; i < 4; i++) {
    System.out.println(i);
}

In the above example, the first statement is the iterator variable. The second condition is the boolean that, when it returns true, stops the loop. The third statement is optional. It increments the iterator so that the loop can end. If your loop does not end, your computer might explode. Here is an example of a for each loop:

for (char letter : alpahbet) {
    System.out.println(letter);
}

In for each loops, you instantiate a variable whose type corresponds to the type of the array. Then, you put a colon and then an array to loop through. This example would return:

a
b
c
d
etc.

Support

Using the template? Support this effort by giving a star on GitHub, sharing your own blog and giving a shoutout on Twitter or be a project sponsor.

Licence

MIT © Timothy Lin