Java Crash Course
One full page of Java that will take you from zero to hero. Let’s begin. (please note, this does not contain anything about OOP – this will be added that later)
- Syntax
- Input, Calculations
- Selection
- Iteration/Loops
- Alogrithms
- Arrays
- File Mgmt
- Methods
Level 0 – Setup
Level 0 – (0??? Yes, 0, Computer Scientists like to start at 0 when we count so get used to it!)
Computer Science – A history lesson
A computer is a big ole’ pile of switches. Imagine an on/off switch connected to a bulb:

Now, imagine it’s connected to other switches and other bulbs:

FInally, imagine a whole room covered in these switches and bulbs. that’s kind of like what a computer is. If we hit one certain switch, or a combination of them, it will turn on a specific bulb. You can imagine this like when you press the letter A on your keyboard, all of the different switches go into action in order to “light up” the correct bulb (such as the A appearing on your monitor) so you can write words on your documents. The “logic” behind how these switches work is where a computer scientist comes into play.

Java is an Object Orientated Programming (OOP) language. Microprocessors were being developed to the point we could miniaturise them into a small personal computer. Its development dates back to 1991 when Sun Microsystems created the project Green. The language of C++ was born, which they wanted to call “Oak” but realised there was already a language named this. They decided to call it Java instead after their meeting at a coffee shop that day. Java was officially introduced in 1995. It evolved out of many generations of languages starting as early as the 60s. It was similar to “B”, which then developed into “C” (you’ve probably heard of that). C++ was developed from C, which added OOP capabilities as well as “classes”
Class – a set of code in a program that is independent of the main program.
Object Orientate Programming in general, but in particular, C++ makes it easy to build very complex graphical environments such as Windows and Mac.
Why Java?
- Java is a high-level programming language (closer to human language than machine code);
- It is event-driven (it can respond to user mouse clicks and key presses);
- Programs can come as two types: applications (run on their own) and applets (run in a web browser, making Java very web user-friendly);
- Executables in other languages have too much access to the system and can’t be trusted – the sandboxing in Java means it is difficult to write a virus or crash the whole system;
- it is effectively a platform in itself and so is platform-independent (programs will run on Windows, Macs, Linux, etc.)
- It is simpler than other languages, so easier to learn, so fewer bugs and bugs are easier to spot – this is up for debate. If we compare Java with Python, some might say Python is easier to learn.
- it is object-oriented which makes it easier to conceive and work with GUI (window- based) interfaces;
- it is multithreaded , meaning programs can run several tasks simultaneously
- The different files (classes) of a Java project are independent but dynamically linked
So, what is a program?
- A list of instructions telling the computer what to do.
- It must be precise and its semantics and syntax, (logic and grammar/spelling) must be correct if translation into machine code is to succeed.
Pseudocode
An artificial simplified programming ‘language’ is used to describe algorithms without using the rules of any particular programming language.
What do I need for Java?

JDK (Java Development Kit) –
download from Oracle here (Windows)
IDE (Integrated Development Environment) Many options:
- Eclipse (eclipse.org) – PC and Mac version
- BlueJ (bluej.org)
- Netbeans (netbeans.org) ALL FREE!
Level 1 Syntax
The 3 holy laws of Java
- Every line must end with a ; unless a { follows
- For every { you must have a }
- Classes start with capital letters, methods and variables are always lowercase
Reserved words
Certain keywords are reserved by Java. class, true are examples. See the full list here. Do not use these words for things like variable names!
Structure
Java is very particular about how you lay the code out. A Java program is called a “class” as we read earlier. These exist within a project which then has a “main()” method inside of it. See below:

Our first Example
Let’s run our first program. Have you got an IDE setup yet? if so, go ahead and copy the code below:
This first line is the class definition for the class “MyFirstTime”. Every program in Java consists of at least one class definition that is defined by the programmer (YOU). The public means the class is public to the application so any other class in the program can “access” and use variables from it.

Generally, the IDE that you use will type all of the extra code you see at the top so you needn’t worry about typing that out.
Now you have written your first-ever Java program it is time to build on those skills. Progress through each level ensuring you have a firm understanding of the levels before it before you move on.
Level 2 Input, Calculations and Strings
5 Variable Types

Combining Values and variables

int num1 = 5;
int num2 = 10;
System.out.println(num1+num2);
System.out.println(num1+” + ”+num2);The output of this code will be:
15
5 + 10Taking an Input
we can take inputs from various devices, including the mouse, keyboard, microphone and scanner. Let’s take a look at the keyboard input.
Steps
- Import java.util.* before the main() in your code
- Declare a Scanner object
- Declare a String variable to get input
- Use the Scanner to assign input from keyboard to variable
- Convert to other data type if necessary (int/char/double)
Code in action

When catching inputs, it is best to use a String data type. We can use:
String anything = kb.nextline();
Converting String to int
Integer.parseInt();Example:
String stnum = kb.nextLine();
int num = Integer.parseInt(stnum);
In Practice (Example)


Converting String to double
Double.parseDouble();Example:
String snumber = kb.nextLine();
double price = Double.parseDouble(snumber);
In Practice (Example)


Calculatations in Java

Good Practice when dealing with variables

Common issues that students get
int x = 1;
int y = 3;
x = 3;
int total = x + y; Answer = 6
int h = 4;
h++; Answer = 5
int k = 7;
k = k + 2; Answer = 9the charAt function
String device = “radio”;
char letter = device.charAt(2);0 1 2 3 4 – using the .charAt() we get the specific character of string from the value of the index

Other string methods

Taking String Inputs
One of the most common tasks in programming is to get input from the user. In Java, you can use the Scanner class to read input from various sources, such as the keyboard, a file, or a network connection. To use the Scanner class, you need to import it from the java.util package at the beginning of your program, like this:
import java.util.Scanner;
Then, you need to create an object of the Scanner class and pass it a source of input, such as System.in, which represents the standard input stream (usually the keyboard). For example:
Scanner sc = new Scanner(System.in);
Now, you can use the methods of the Scanner object to read different types of input from the user. For example, to read a string input, you can use the nextLine() method, which returns the entire line of input as a string. For example:
System.out.print("Enter your name: "); // prompt the user for input
String name = sc.nextLine(); // read the input as a string and store it in a variable
System.out.println("Hello, " + name); // print the output
The output of this code will look something like this:
Enter your name: Alice
Hello, Alice
Creating Variables
A variable is a name that refers to a value stored in memory. In Java, you need to declare the type and name of a variable before you can use it. The type of a variable determines what kind of values it can store and what operations can be performed on it. For example, to declare a variable of type int, which can store integer values, you can write:
int x;
This statement creates a variable named x that can store an integer value. However, the variable is not initialized, which means it does not have a value assigned to it. To assign a value to a variable, you can use the = operator, like this:
x = 10;
This statement assigns the value 10 to the variable x. You can also declare and initialize a variable in one statement, like this:
int y = 20;
This statement creates a variable named y of type int and assigns it the value 20. You can use variables to store the input from the user, like this:
System.out.print("Enter a number: "); // prompt the user for input
int num = sc.nextInt(); // read the input as an int and store it in a variable
System.out.println("You entered: " + num); // print the output
The output of this code will look something like this:
Enter a number: 42
You entered: 42
Performing Arithmetic Operations
One of the most useful features of programming languages is the ability to perform arithmetic operations on values and variables. In Java, you can use the following operators to perform basic arithmetic operations:
+for addition-for subtraction*for multiplication/for division%for modulus (remainder)
For example, to add two numbers, you can write:
int sum = x + y; // add x and y and store the result in sum
To subtract two numbers, you can write:
int diff = x - y; // subtract y from x and store the result in diff
To multiply two numbers, you can write:
int prod = x * y; // multiply x and y and store the result in prod
To divide two numbers, you can write:
int quot = x / y; // divide x by y and store the result in quot
To find the remainder of dividing two numbers, you can write:
int rem = x % y; // find the remainder of dividing x by y and store the result in rem
You can use these operators to perform calculations on values and variables, and print the results to the console. For example:
System.out.println("The sum of " + x + " and " + y + " is " + (x + y)); // print the sum
System.out.println("The difference of " + x + " and " + y + " is " + (x - y)); // print the difference
System.out.println("The product of " + x + " and " + y + " is " + (x * y)); // print the product
System.out.println("The quotient of " + x + " and " + y + " is " + (x / y)); // print the quotient
System.out.println("The remainder of " + x + " and " + y + " is " + (x % y)); // print the remainder
The output of this code will look something like this:
The sum of 10 and 20 is 30
The difference of 10 and 20 is -10
The product of 10 and 20 is 200
The quotient of 10 and 20 is 0
The remainder of 10 and 20 is 10
Outputting to a Console
A console is a text-based interface that allows you to interact with a program. In Java, you can use the System.out object to print output to the console. The System.out object has several methods that allow you to print different types of values, such as:
println()to print a value and move to a new lineprint()to print a value and stay on the same lineprintf()to print a formatted value using placeholders and modifiers
For example, to print a string value, you can write:
System.out.println("Hello, world!"); // print a string and move to a new line
To print an integer value, you can write:
System.out.print(42); // print an int and stay on the same line
To print a floating-point value, you can write:
System.out.printf("%.2f", 3.14); // print a double with two decimal places and stay on the same line
You can also use the + operator to concatenate (join) multiple values and print them as one. For example:
System.out.println("The answer is " + 42); // print a string and an int as one
You can use the System.out object to display the results of your calculations to the user. For example:
System.out.println("The area of a circle with radius " + r + " is " + (Math.PI * r * r)); // print the area of a circle
Level 3
Selection in Java is a way of controlling the flow of a program based on some conditions. It allows you to execute different parts of code depending on whether the conditions are true or false. In this post, I will explain how to use the two main selection statements in Java: the if statement and the switch statement.
The if Statement
The if statement is the most basic and common selection statement in Java. It evaluates a boolean expression and executes a block of code if the expression is true. If the expression is false, the block of code is skipped. The syntax of the if statement is:
if (expression) {
// block of code to execute if expression is true
}
For example, suppose you want to check if a number is positive or negative. You can use the following if statement:
int number = -5;
if (number > 0) {
System.out.println("The number is positive");
}
In this example, the expression number > 0 is false, so the block of code inside the if statement is not executed. The output of this code is nothing.
You can also use the else clause to specify a block of code to execute if the expression is false. The syntax of the if-else statement is:
if (expression) {
// block of code to execute if expression is true
} else {
// block of code to execute if expression is false
}
For example, you can modify the previous code to print a message if the number is negative:
int number = -5;
if (number > 0) {
System.out.println("The number is positive");
} else {
System.out.println("The number is negative");
}
In this example, the expression number > 0 is false, so the block of code inside the else clause is executed. The output of this code is:
The number is negative
You can also use the else-if clause to check for multiple conditions and execute different blocks of code accordingly. The syntax of the if-else-if statement is:
if (expression1) {
// block of code to execute if expression1 is true
} else if (expression2) {
// block of code to execute if expression2 is true
} else if (expression3) {
// block of code to execute if expression3 is true
} ...
else {
// block of code to execute if none of the expressions are true
}
For example, suppose you want to check the grade of a student based on their score. You can use the following if-else-if statement:
int score = 85;
if (score >= 90) {
System.out.println("You got grade A");
} else if (score >= 80) {
System.out.println("You got grade B");
} else if (score >= 70) {
System.out.println("You got grade C");
} else if (score >= 60) {
System.out.println("You got grade D");
} else {
System.out.println("You got grade F");
}
In this example, the expression score >= 90 is false, so the block of code inside the first if clause is skipped. The expression score >= 80 is true, so the block of code inside the first else-if clause is executed. The output of this code is:
You got grade B
Note that the order of the conditions is important. The if-else-if statement checks the conditions from top to bottom and executes the first block of code that matches. Once a match is found, the rest of the conditions are ignored. Therefore, you should always put the most specific conditions at the top and the most general condition at the bottom.
The switch Statement
The switch statement is another selection statement in Java that allows you to execute different blocks of code based on the value of a variable or expression. It is often used as an alternative to the if-else-if statement when you have many possible values to check. The syntax of the switch statement is:
switch (variable or expression) {
case value1:
// block of code to execute if variable or expression equals value1
break;
case value2:
// block of code to execute if variable or expression equals value2
break;
case value3:
// block of code to execute if variable or expression equals value3
break;
...
default:
// block of code to execute if none of the values match
break;
}
For example, suppose you want to print the name of a month based on its number. You can use the following switch statement:
int month = 3;
switch (month) {
case 1:
System.out.println("January");
break;
case 2:
System.out.println("February");
break;
case 3:
System.out.println("March");
break;
case 4:
System.out.println("April");
break;
case 5:
System.out.println("May");
break;
case 6:
System.out.println("June");
break;
case 7:
System.out.println("July");
break;
case 8:
System.out.println("August");
break;
case 9:
System.out.println("September");
break;
case 10:
System.out.println("October");
break;
case 11:
System.out.println("November");
break;
case 12:
System.out.println("December");
break;
default:
System.out.println("Invalid month");
break;
}
In this example, the variable month has the value 3, so the switch statement matches the case 3 and executes the block of code inside it. The output of this code is:
March
Note that each case ends with a break statement. This is to prevent the execution from falling through to the next case. If you omit the break statement, the switch statement will continue to execute the next case until it reaches a break statement or the end of the switch statement. For example, if you remove the break statement from the case 3, the output of the code will be:
March
April
May
June
July
August
September
October
November
December
This is usually not the desired behavior, so you should always include the break statement in each case. The only exception is when you want to execute the same block of code for multiple values. For example, suppose you want to print the season based on the month. You can use the following switch statement:
int month = 3;
switch (month) {
case 12:
case 1:
case 2:
System.out.println("Winter");
break;
case 3:
case 4:
case 5:
System.out.println("Spring");
break;
case 6:
case 7:
case 8:
System.out.println("Summer");
break;
case 9:
case 10:
case 11:
System.out.println("Autumn");
break;
default:
System.out.println("Invalid month");
break;
}
In this example, the variable month has the value 3, so the switch statement matches the case 3 and executes the block of code inside it. Since there is no break statement after the case 3, the execution falls through to the next case, which is 4.
However, since there is no block of code after the case 4, the execution falls through to the next case, which is 5. Again, since there is no block of code after the case 5, the execution falls through to the next case, which is 6. Finally, there is a block of code after the case 6, which prints “Spring” and breaks out of the switch statement. The output of this code is:
Spring
Note that the order of the cases does not matter in the switch statement. The switch statement will match the first case that equals the variable or expression, regardless of where it is located. Therefore, you can arrange the cases in any order you want, as long as they are unique and valid.
Level 4 Iteration
MORE TO COME!