Java Guide

Java is a class based, object oriented programming language written by Oracle. This guide will simple cover some of the basics of Java and help you build a hello world program to get started.

Java Terminology

Classes
All Java programs are made of at least one class. The class name must match the file. Each Class represents one real world idea.
Statements
A statement performs a single task. Statements always conclude with a semicolon.
Methods
Methods are the blocks of code that make up your classes. Methods are only run when they are called upon. Methods are similar to functions in other programming languages.

Hello World

Just to quickly kick things off, comments in java are written with // and a block of comments can be written like this:

// This is a single line comment

/* This is a block of comments
Three
Two
One*/

With that out of the way lets move on to some real information.

Any Program in Java has to have at least one class and this class has be have a name matching the file it is in. In our case for this program we are going to create a file named: HelloWorld.java. Therefore we need to create a class in this file also called HelloWorld:

public class HelloWorld {

}

We can't just leave our class like that and expect it to work because every Java program also needs at least one method named main(). Lets add our main method to our class:

public class HelloWorld {
    public static void main(Strings[] args) {

    }
}

It might seem like a lot is going on here and that is because it is. Lets now break down each piece of the method in simple terms. We will go more in depth on these concepts in the Object Oriented section.

public
In this case this method is public and can be used by other classes. Methods can also be set to private and we will go more into this more later.
static
Our method is also set to static. This concept can be a bit complex to explain and later in the guide I have a chapter on static use in Java: Static.
void
Means that this method will not return anything when it is finished running. You can also specify a return type in this spot if our method is a going to return data when it is run. For more information on what types we can list here refer to the Variables section.
main
Finally we specify that this is our main method. This section is just the name of the method followed immediately by an open parenthesis and a closing parenthesis. Inside these parenthesis we list an data types we accept as inputs for this method. We will see more examples in other programs
Strings[] args
This is the data type(s) we are taking as input for this method. The main method in particular we have to define an array of Strings named args. For more examples of inputs to methods refer to the Object Oriented section, for more information on data types refer to the Variables section, and for more information on arrays refer to the Arrays section.

Methods, like classes, are wrapped in curl braces {} and anything inside of them will be executed when they are called. Since main is executed when our program is run this is the starting point of our program.

Now lets add a print statement to print hello world:

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

This program can then be complied by running javac HelloWorld.java to complile the code. Java is a programming language that requires compling before the code can be run. Using javac compiles our code into a HelloWorld.class file. This file can be run with the following command: java HelloWorld. Note we didn't even have to include the .class to our command but we can if we want to. This program should output Hello World to your screen when it is run.

This concludes the very basics of Java from here I will break out into a couple of different org files. The information in these guides will build on each other mind you so this is the order I would view them, but feel free to skip around:

Variables
The guide on variables in Java with some examples.
Object Oriented
The guide on Java's object oriented way of programming. Probably the most important guide for someone that knows other programming languages but is new to Java.
Arrays
The guide on Arrays and ArrayLists in Java.
Loops
The guide on Loops in Java.
String Methods
The guide of various methods that come with the String object in Java.
Access and Scope
The guide on access, encapsalation, and static methods.
Inheritance
The guide on inheritance in java.
Debugging
The guide on debugging java programs.
2D Arrays
The guide on 2 dimensional Arrays in java.
Other
This is just miscellaneous notes that didn't get their own chapter.
This page was last updated: not defined. Source