My first Java App - HelloWorld!

Our first Java program! We previously learned how to create a new Java project in Eclipse and we will pick up right were we left off. Let's get started making our first application!

Things you may want to read up on first.

Creating the first program

We already have the project created, now let's get to work.

Step 1: Creating a package

A package is a namespace that organizes a set of related classes and interfaces. Think of a package as being similar to different folders on your computer. You keep photos in one folder, music in another, etc. Because software written in Java can be composed of hundreds or thousands of individual classes, it makes sense to keep things organized by placing related classes and interfaces into packages.



Package names are unique and are always written in lowercase letters. A common approach to naming your package is to start with the top level of your domain name followed by the components you intend to create. In my example, I use the package name: com.houselantaff.XXX. If I were to create another package I would continue this approach: com.houselantaff.YYY.



Step 2: Create a Java class

A class file is where you write your code. It will have a .java extension until it gets compiled. When you compile it the extension will become .class and will contain your code, but will be unrecognizable to you because it has been transformed into bytecode. This bytecode is executed in the JVM (Java virtual machine) and then you will see the results of your creation!



Give your class a name

Class names always start with an uppercase letter. If more than one word is needed to describe your class, it is standard practice to use camelcase. Camelcase convention is where you combine words together with the first letter of the new word in uppercase. In the below example I created a class in the com.houselantaff.tutorials package with a name of HelloWorldApp. Also, for this example, you need to check the box below which creates a public static void main method.



HelloWorld!

Finally we can write the code and execute our first program. You can choose to copy and paste from below and modify the words in the quotation marks, but make sure it looks like the screenshot in the end. What we are doing is writing out some information to the console in the form of text. Some developers do this so they can see what a particular object has stored for a value when that piece of code gets ran.


package com.houselantaff.tutorials;

public class HelloWorldApp {

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

	}

}

				



Run the application

Running the application is simple. Just right click on your Class file and choose Run As > Java Application.



Verify the output

After you are able to run the application you should see a window toward the bottom of your screen, with a tab called "Console". Anytime you use System.out.println("Text here"); it will display in the console.




Download this code from GitHub: HelloWorldApp