Lesson 1 – Hello World

Now that we’re finally setup; It’s time to have a look at some code. This lesson is a short primer; it will contain a reasonable amount of theory which will give you a good grounding. The lessons after this will be more hands on, but for now, it’s important to understand some core java concepts (hang in there).

Downloads

To get started you can use the basic starter project and follow the steps to compile the code examples for the lesson.

Project structure and packages

Hopefully at this stage you should have the starter project imported into Eclipse (If not see the setting up lesson). In the ‘Package Explorer’ you should see the project in a directory structure.

If you cannot see the package explorer, use the Menu bar and select: Window > Show View > Other > Package Explorer

Let’s talk about each level of the projects directory structure:

eclipse java project structure

 

1. Project

In Simple terms, this is a Folder! It matches the name of the project and contains all of the files and JAR (Java ARchive) files required to run your project. There are also some settings files and folders which will be used by eclipse to build your project (These are nice to know about but not overly important right now).

2. Source Folders

Source folders are top-level folders in the project hierarchy. Source folders allow you to structure the project in some logical manner. The most common use for these is to separate source code, resources (xml or txt files for example) and test code into distinct sections.

3. Packages

A Java package is a mechanism for organising Java classes even further. The industry standard is to use your website in reverse order followed by a descriptive name for that group of files. For example, com.codenerve.main would describe a package that would contain the Main java file (or class file) that would start my application. You can use these to separate code that is for a particular purpose (for example web ui vs. backend services like database connections)

4. Source Files

Source files in java have the .java file extension. These files contain the java source code that you write and later are compiled into byte code and run in the Java Virtual Machine.

5. Java System Library

The JRE System Library is added to the project and makes all of the Java core classes, supporting libraries and the Java Virtual Machine available to your project.  The Java Runtime Environment (JRE) is part of the Java Development Kit (JDK). The JDK contains additional development tools, such as the Java compiler (javac). The Java Runtime Environment provides the minimum requirements for executing a Java application.

The main class

In Eclipse open the HelloJava.java file:

HelloWorld java

Main method signature

public static void main(String[] args)

The main method signature is interpreted by breaking it up into multiple sections:

public – This denotes the Access level modifier of the method. In short it describes the visibility of this method from other files in the project. Public means this method is visible from any other file in the project. For other available Access level modifiers see the oracle documentation.

static – This denotes that the method in this class(file) is static. This means they can be used without the need to create an instance of the class. We’ll learn more about ‘instances’ later in the lessons. This static keyword is always required on the main method for the jvm to start your application.

void – The void keyword denotes that the method will not return any value from the code within it. The main method will run, and once complete the application will close.

main – This is simply the name of the method. It’s used to describe the purpose of the method. Method names must be unique to each class file.

args – The String[] args inside the parenthesis is called a parameter. In java it is possible to pass values into a method. In this case these values would be passed into you application when starting it.

Running the code

Alright, thats a lot of theory!! Let’s run the example and in the next lesson, we’ll begin to focus on writing and running code samples.

To run the code:

Right-click on the Main.java file and select:

Run As -> Java Application. This will print “Hello World” in the ‘console’ view.

Next: Primitive types

Menu