// Main, void, semicolon // Declarations, assignments // For and While // If/else // Int, String, [] * Anatomy of a class * Main method In Java, everything goes in a class. You'll type your source code file (with a .java extension), then compile it into a new class file (with a .class extension). When you run your program, you're really running a class. The main() method is where your program starts running. %java // Project: TestingApp // Package: testingapp // File: TestingApp.java package testingapp; public class TestingApp { public static void main(String[] args) { System.out.println("Hello World!"); } } // Output: Hello World! * Void: The return type (void) means there's no return value. * Semicolon: Every statement must end in semicolon. * Statements Declaratlons, assignments, method calls, etc: %java public static void main(String[] args) { int x = 3; String name = "Catalin"; x = x*3; System.out.println("x is " + x); // this is a comment // Output: x is 9 } * Loops For and While %java public static void main(String[] args) { int x = 3; while (x < 10) { x++; System.out.println("x: " + x); } for(int i=0; i<10; i++) { System.out.println("i: " + i); } } * Branching If/else tests %java public static void main(String[] args) { int x = 3; if (x == 0) { System.out.println("x is 0"); } else { System.out.println("x isn't 0"); } } * Int, String, List (array) %java // Get random entry from a set public static void main(String[] args) { int x = 1; String a = "a"; String[] listOne = {"a1", "b2", "c3"}; int oneLength = listOne.length; int rand1 = (int) (Math.random() * oneLength); System.out.println("Random word is " + listOne[rand1]); }