The most common way to execute a Java program is to run it through a sequence of two programs. The first is the Java compiler, or javac. The second is the Java interpreter, or java.

java1

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

So variables in Java are strictly defined with a specific type. You can’t change the type of variable after defining it.

System.out.println print to screen (like stdout), with \n at the end of line.
System.out.print same as above but without \n.

Conditional statement similar as javascript.

Data types: 8 primitive types in Java:

  1. byte
  2. short
  3. int
  4. long
  5. float
  6. double: stores approximations of real numbers
  7. boolean
  8. char

reference type

  1. references to Objects

When you declare a variable of a certain type in Java:

  • Your computer sets aside exactly enough bits to hold a thing of that type. Example: Declaring an int sets aside a “box” of 32 bits.
  • Java creates an internal table that maps each variable name to a location
  • Java does NOT write anything into the reserved boxes. - For safety, Java will not let access a variable that is uninitialized java2

The Golden Rule of Equals (GRoE)
Given variables y and x:

  • y = x copies all the bits from x into y.

java3

java4

java5

java6

java7

Use the private keyword to prevent code in other classes from using members (or constructors) of a class.

Function

public static int max(int x, int y) {
    if (x > y) {
        return x;
    }
    return y;
}

Same as in C, we declare function with name, type of output and parameters.

ARRAY

int[] numbers = new int[3];
numbers[0] = 4;
numbers[1] = 7;
numbers[2] = 10;
// Or this way
int[] numbers = new int[]{4, 7, 10};

As above, we create new instance of class int named numbers which is array of 3 int.
You can get the length of an array by using .length.

FOR LOOP syntax is similar as javascript.

break and continue similar as javascript. Continue skip the rest of the current iteration and jump to the next increment condition. Break terminates the innermost loop.

Enhanced For Loop

String[] arr = ['x', 'y'];

for (String element: arr){
	System.out.println(element);
}

Basically, it’s similar to for in in python.

OOP

// This class can be run directly cause it has main() method. Similar idea as C
public class Otoke {
	public static void main() {

	}
}

// This class can't be run directly cause there is no main method
class Dog {
	// weight is a Instance variable
	public int weight;
	// Static method or variable like below can only be access by Class, not instance
	public static String TYPE = 'animal';

	// Constructor: (similar to a method, but not a method). Determines how to instantiate the class.
	public Dog(int initWeight){
		weight = initWeight;
	}

	// Non-static method, a.k.a. Instance Method
	// If the method needs to use "instance variables”, the method must be non-static.
	public void doubleWeigth() {
		System.out.println(weight * 2);
	}
}

public class Main {
	public static int main() {
		Dog smallDog;  // Declaration of a Dog variable.
		new Dog(20);  // Instantiation of the Dog class as a Dog Object / Dog instance
		smallDog = new Dog(5);  // Instantiation and Assignment.
		Dog hugeDog = new Dog(150);  // Declaration, Instantiation and Assignment
	}
}

Static methods are invoked using the class name, e.g. Dog.makeNoise(); Instance methods are invoked using an instance name, e.g. maya.makeNoise(); Static methods can’t access “my” instance variables, because there is no “me”.

public static void main(String[] args)  // args is a array of command line arguments
// access item in array by args[index]
// args[0] mean first argument after name file.

Nested Class

public class SLList {
	public class IntNode { // Define a class inside another
		public int item;
	}
	
	private IntNode stuff; // Use that to declare a new variable
}

Nested Classes are useful when a class doesn’t stand on its own and is obviously subordinate to another class.
Make the nested class private if other classes should never use the nested class.

If the nested class never uses any instance variables or methods of the outer class, declare it static.
Static classes cannot access outer class’s instance variables or methods.
Results in a minor savings of memory.

java8

Caching idea: putting aside data to speed up retrieval
So everytime we add new node to SSList, we +1 to size variable (which is an instance variable). Of course, when remove 1 node, -1 to size.

java9

SLList - Singly Linked List

Suppose we have a naked linked list (simply a node)

public class IntNode {
	public int item;  // Item is the integer part of each node
	public IntNode next;  // next is a "pointer"/reference to the next node IntNode 

	public IntNode(int i, IntNode n){  // Initiation the node
		item = i;
		next = n;
	}
}

java10

For better hanling with list, we can wrap the node above inside a Singly Linked List.

public class SLList {
	private static class IntNode {
		private int item;
		private IntNode next;

		private IntNode(int i, IntNode n){
			item = i;
			next = n;
		}
	}
}