Java Tutorial for Beginners

This is a very fast tour of some basic Java. You should type everything in to a Java repl, and try changing values to see what happens.You should know how to program in other languages - I'm assuming knowledge of a language like Python or JavaScript.

Basics

Java is OOP (Object-Oriented Programming) so everything must be in a class. The Java compiler will automatically run any code in the main method.

class Main {
public static void main(String[] args) {
// This is a comment
// This will be run
}
}

In Java, blocks are defined with curly braces {} - indentation is not necessary, but improves readability. All lines in Java must end with either ;, or } (where appropriate).

Control Flow

Comparison operators are used to compare things.

class Main {
public static void main(String[] args) {
System.out.println(1==1); // equal to
System.out.println(2>1); // greater than
System.out.println(2<1); // less than
System.out.println(1>=1); // greater than or equal to
System.out.println(2<=1); // less than or equal to
System.out.println(2!=1); // not equal to
}
}

Logical operators are ways of choosing based on trues and falses.

class Main {
public static void main(String[] args) {
// AND - true if both are true
System.out.println(true && true);
// OR - true if either are true
System.out.println(true || false);
// NOT - swaps true with false and vice versa
System.out.println(!true);
// XOR - true if either are true but not both
System.out.println(true^false);
System.out.println(true^true);
}
}

If statements do things depending on whether certain conditions are evaluated as true or false.

class Main {
public static void main(String[] args) {
// outputs "10 is greater than 5!"
if (10>5) {
System.out.println("10 is greater than 5!");
} else {
System.out.println("10 is not greater than 5");
}
int a = 10;
// outputs "a is greater than 8"
if (a<2) {
System.out.println("a is less than 2");
} else if (a<8) {
System.out.println("a is less than 8");
} else {
System.out.println("a is greater than 8");
}
}
}

A class is like a blueprint, with which you can make objects (instances).

class Main {
public static void main(String[] args) {
// create a new 'instance' of the dog class, called George
Dog George = new Dog("George");
George.bark();
George.live();
George.eat(); // fails because eat() is private
}
}
class Dog {
// this is an attribute that all
// of our Dog objects will have
String name;
// this is called the constructor
// it is called when the dog is created
public Dog(String name) {
// this. means that name applies the this dog
// it means that it's name can be used elsewhere
// in the class
this.name = name;
System.out.println("A dog called "+this.name+" was just created!");
}
// public means that it can be accessed by other classes
public void bark() {
System.out.println(this.name+" just said 'Woof!'");
}
public void live() {
this.eat();
}
// private so cannot be accessed by other classes
private void eat() {
System.out.println(this.name+" is eating!");
}
}

static methods can be referenced from a static setting.

class Main {
public static void main(String[] args) {
// create a new instance of the Shop class
Shop Amazon = new Shop();
Amazon.buy();
// static, so we can just do this
Shop.buy();
Amazon.showDetails();
// fails because it is not an instance
// and the method is not static
Shop.showDetails();
}
}
class Shop {
// static so don't need to instantiate in order to
// use the method
public static void buy() {
System.out.println("Buying!");
}
public void showDetails() {
System.out.println("You have to create a instance for this to work");
}
}