Learn Java Fast: First Steps


So, you want to learn Java?

There are many great programming languages out there and Java is one of my personal favorites. What first made me feel intimidated by Java, like its statically typed nature, are elements that I now really enjoy about the language. This series is intended to not only get you coding fast, but also well. Many important concepts will be introduced along the way in a logical and, hopefully, easy to understand manner. Some concepts will also be introduced later than some technical manuals would traditionally do in order to avoid confusion for the new Java coder. In any case, you will be creating useful console and GUI applications in no time.

Learning a programming language doesn’t have to take a lot of time, but mastering it will. Programming is something that takes a lot of commitment to become skilled at, so I won’t waste your time by introducing concepts slowly. I will put you on the front-line immediately by introducing new concepts frequently. I consider this a more productive approach because most of your learning will come from experimenting with the concepts and examples demonstrated. I will hit on the primary considerations and I will leave the experimenting to you. We will move fast, but with a plan. Getting too wrapped up in the small details in the beginning is cumbersome and something that will be learned through repetition as you code. We will cover the details as we progress, but first let’s discuss the main concepts.

Ok, let’s get going!

Your First Program

The traditional first program that most of us write when using a new language is a brief Hello World example. The purpose is to get used to the structure and compilation process of a new programming language and/or environment. I will primarily be using Netbeans for these examples. I recommend that anyone new to programming download Netbeans to follow along.

Open Netbeans and start a new project. Choose the Java option on the left column and Java Application option on the right. Name the project HelloWorld and press finish.

Now type the following code in the HelloWorld.java file.

The Code
package helloworld;

public class HelloWorld {

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


Press the green arrow in the top toolbar of Netbeans to run the application. You should see the output Hello World! in the lower display area of Netbeans.

The above is a typical Hello World application. As you can see the System.out.println(); method prints a line of text, called a String, to the Command Line Interface (CLI). This will be looked at in more detail shortly. You will notice that the command is within a method public static void main(String[] args). This is the method that executes the application. The main() method is within a class called HelloWorld, public class HelloWorld.

In Java everything is an object. An object is an instance of a class. If you’re squinting at me with skepticism, do not fear – it is a simpler concept than you might be imagine. Think of a class definition as a blueprint for a house. That blueprint can be used to create many houses. When you use a blueprint to create a house, you are creating an “instance” of the house laid out in the blueprint (remember the blueprint is our class definition). So, you use a class definition to create an instance of an object.

OOP (Object Oriented Programming)

As I previously stated, everything in Java is an object. Java is an Object Oriented language – which is a good thing in case you were wondering. Objects are instances of class definitions. In this next example you will create a custom object by defining a class and then creating a new instance of that class; a process called instantiation.

Ok, so now we are going to get crazy and create a new project. Open Netbeans and create a new project called FirstObject. When the project has been created you will see a file called FirstObject.java under a package looking icon titled firstobject. Right click on that package and choose New->Java Class. Title the new class MyObject and press finish. Now there should be two files in your project: FirstObject.java and MyObject.java

The Code

First, double click on MyObject.java and type the following code.

package firstobject;

public class MyObject {
    public void sayHello() {
        String helloWorld = "Hello World!";

Now, double click on FirstObject.java and type the following code.

package firstobject;

public class FirstObject {

    public static void main(String[] args) {
        MyObject myObject = new MyObject();

Press the green arrow to run the application. You should see the same output as you saw in your HelloWorld application. The difference is that instead of invoking the println() method directly, we created a class with a method called sayHello() in the MyObject.java file. In the sayHello() method we put the System.out.println(); command.

The println() method takes a String as an argument. A String can be represented as text between quotations as in the first HelloWorld example or as the String data type in this second example; the println() method doesn’t care either way.

Then we went back to the FirstObject.java file and within the main() method we created a reference of type MyObject which we named myObject and then we created an instance of that class using new MyObject(); which creates the actual object from the class definition.

It is important to note that creating a reference and instantiating the object are two separate steps. The = sign actually assigns the created object to the reference. This could be done over two lines of code for example.

// this defines the object reference:
MyObject myObject; 
// this instantiates a new object 
// and assigns it to the definition:
myObject = new MyObject(); 

The above will produce the same results as the original single line technique.

Congratulations, you are now an OO programmer! This is admittedly a very abbreviated introduction to Java and OOP, but you need to get your feet wet sometime. Objects are the cornerstone of Java, and any object oriented language, so there isn’t a significant benefit to delay their introduction.

The next lesson is a primer that will cover the topics necessary to complete the rest of the lessons in this tutorial.

Next Lesson Learn Java Fast: Primer


About Author

James Cathcart has a Bachelors of Science for Software Application Programming and is currently pursuing his Masters of Science for Software Engineering. He became a computer enthusiast before his teenage years and is passionate about software development.

Leave A Reply