Mastery
Java » Basics

Structuring a Program

1 Open Eclipse

2 File » New » Java Project

3 Enter project name » Finish

4 Right click the folder src » New » Class

5 Enter package name and class name. For a quick void function, check the box that reads public static void main(String[]args) » Finish

Syntax

Print statements:

System.out.print("This prints on the same line.")
System.out.println("This prints on the next line.")

And, or, not:

if ( (HaveMangoes == false && !money.equals("$10")) || (Hungry == true) ) {
    BuyMangoes();
    // AND is &&
    // OR is ||
    // NOT is !
}

Some operators:

int x = 5;
x++; // ++ : x = x + 1
y = ++x // ++var : same as x=x+1; y=x; increment var, then use the new value of var
y = x++; // var++ : same as oldX=x; x=x+1; y=oldX;

Creating and assigning variables:

int apples = 3; // Whole number
float bananas = 3.3f; // Number with a decimal. 'f' at the end prevents future errors
char mangoes = 3; // Single letter/number/symbol
String fruit = "Mangoes";
boolean easy = true; // True or false

int tigers;
tigers = 10;

While loops:

int value = 0;
while (value < 10) {
    System.out.println("Hello " + value);
    value = value + 1;
}

If statements:

if (mango == 0) {
    System.out.println("I don't have any mangoes...");
    // Since this is a single line, { } are unnecessary
}
else if (mango == 2) {
    System.out.println("Mangoes are yummy.");
}
else
    System.out.println("All the mangoes!");