Chapter One - Basics of Java Revealed

In this first chapter I will show you Java code, and the language I use to describe it, with little explanation.

I do this to provide you with some context. I want to wrap you in the language typically used to describe Java code. And I want to show you small sections of code in context. I don’t expect you to understand it yet. Just read the pages which follow, look at the code, soak it in, accept that it works, and is consistent.

Then in later pages, I will explain the code constructs in more detail, you will write some code, and I’ll reinforce the explanations.

Java Example Code

An empty class

A class is the basic building block that we use to build our Java code base.

All the code that we write to do stuff, we write inside a class. I have named this class AnEmptyClass.

1 package com.javafortesters.chap001basicsofjava.examples.classes;
2 
3 public class AnEmptyClass {
4 }

Just like your name, class names start with an uppercase letter in Java. I’m using something called Camel Case to construct the names, instead of spaces to separate words, we write the first letter of each word in uppercase.

The first line is the package that I added the class to. A package is like a directory on the file system, this allows us to find, and use, the class in the rest of our code.

A class with a method

A class, on its own, doesn’t do anything. We have to add methods to the class before we can do anything. Methods are the commands we can call, to make something happen.

In the following example I have created a new class called AClassWithAMethod, and this class has a method called aMethodOnAClass which, when called, prints out "Hello World" to the console.

1 package com.javafortesters.chap001basicsofjava.examples.classes;
2 
3 public class AClassWithAMethod {
4 
5     public void aMethodOnAClass(){
6         System.out.println("Hello World");
7     }
8 }

Method names start with lowercase letters.

When we start learning Java we will call the methods of our classes from within JUnit tests.

A JUnit Test

For the code in this book we will use JUnit. JUnit is a commonly used library which makes it easy for us to write and run Java code with assertions.

A JUnit test is simply a method in a class which is annotated with @Test (i.e. we write @Test before the method declaration).

 1 package com.javafortesters.chap001basicsofjava.examples.classes;
 2 
 3 import org.junit.Test;
 4 
 5 public class ASysOutJunitTest {
 6 
 7     @Test
 8     public void canOutputHelloWorldToConsole(){
 9         AClassWithAMethod myClass = new AClassWithAMethod();
10         myClass.aMethodOnAClass();
11     }
12 }

In the above code, I instantiate a variable of type AClassWithAMethod (which is the name I gave to the class earlier). I had to add this class to the package, and I had to import the @Test annotation before I could use it, and I did that as the first few lines in the file.

I can run this method from the IDE without creating a Java application because I have used JUnit and annotated the method with @Test.

When I run this method then I will see the following text printed out to the Java console in my IDE:

Hello World

Summary

I have thrown you into the deep end here; presenting you with a page of possible gobbledygook. And I did that to introduce you to a the Java Programming Language quickly.

Java Programming Language Concepts:

  • Class
  • Method
  • JUnit
  • Annotation
  • Package
  • Variables
  • Instantiate variables
  • Type
  • Import

Programming Convention Concepts:

  • Camel Case
  • JUnit Tests are Java methods annotated with @Test

Integrated Development Environment Concepts:

  • Console

Over the next few chapters, I’ll start to explain these concepts in more detail.