Java is a programming language that helps you write software for many platforms. Whether you are writing a GUI program with a desktop interface, or developing server-side software, or a mobile application using Android, learning Java will serve you well. Here are some core Java concepts to help you get started.

1. The Development Cycle (Building Java Software)

For any kind of program, Java code is written in Java source files which are text files with the extension .java. These source files are compiled using a Java compiler into Java class files. The class files are then assembled into ZIP archives called JAR files. These JAR files are provided to a Java Virtual Machine for execution, which begins executing a main() program within a specified class.

Building Java Software

2. Variables

Fundamental to every program (in any language) is the concept of a variable. A variable is a named entity within a program that stores a value. A variable:

  • Has a begin-end lifecycle.
  • May be stored and retrieved from external storage.
  • May have its value changed.
  • Is used in computation.

As an example, let us say you are computing the area of a circle. You would then need to store the radius of the circle in a variable (named, say radius) and use it subsequently to compute the area. Check out the sample code below.

        static private double computeArea(double radius) {
  return Math.PI * radius * radius;
}

3. Types

Each variable within a Java program has a type. The type could be a primitive such as a number (radius in the above example has a type of double), a built-in class such a string, or a user-defined class.

The type could be any of the following:

  • A primitive type:char (for character), a byte (for a single 8-bit value), an int (for 32-bit integer), a short (for a 16-bit integer), a long (for a 64-bit integer), a float (single-precision floating point number) or a double (double-precision floating point number).
  • A built-in Java class: For example, String is a built-in Java class used for storing and manipulating strings.
  • A user-defined class: To represent more complex types, users can define their own classes (explained in detail below).

4. Classes

A class is a blueprint for a concept within a Java program. It encapsulates behavior and state. Behavior is represented using methods, and state is represented using member variables. For example, the following Circle class has a state of radius, and provides a method computeArea() to compute its area.

        public class Circle {
  private double radius;

  public double computeArea() {
    return Math.PI * radius * radius;
  }
}

5. Objects

An object is an instance of a class. The class definition serves as a blueprint for instantiating an object within a running program. Here is how you can create an instance (named circle) of the above class within the program, and invoke its method (explained below):

        Circle circle = ...;
double area = circle.computeArea();

6. Constructors

A constructor is a special method within a class which is invoked when an object is being created. It is invoked with the arguments passed in during the construction. These arguments are then used to initialize the object to a proper state. In the example below, the Circle class provides a constructor which takes the radius as an argument.

The constructor method has the same name as the class name.

        public class Circle {
  private double radius;

  public Circle(double r) {
    this.radius = r;
  }

  // more methods here ...
}

With this definition, we can now instantiate a circle object.

        Circle circle = new Circle(2.5);

7. Methods

An object method is an implementation of a specific behavior. It might compute and return a value, in which case it is defined with a return type. Or it might just update the state of the object. In this case, the method is defined with a void return type.

A method can also accept arguments which are used within the computation.

In the following example, the method computeCircumference() is defined by the class Circle for computing the circumference. It does not accept any arguments and returns a double type as its return value.

        public class Circle {
  ...

  public double computeCircumference() {
    return 2 * Math.PI * radius;
  }

  ...
}

8. Fields

Fields are declared within a class definition to represent the state of an object instance. A field has a type which can be primitive or a different class. It is usually declared private which means only the methods of the class can access the field directly. When the field is declared public, it is accessible from outside the class definition too.

The following example declares a Rectangle class with two fields length and width. The methods setLength() and setWidth() are provided to update the length and width of the rectangle.

        public class Rectangle {
  private double length, width;

  public Rectangle(double length,double width) {
    this.length = length;
    this.width = width;
  }

  public double computeArea() {
    return this.length * this.width;
  }

  public void setLength(double length) {
    this.length = length;
  }

  public void setWidth(double width) {
    this.width = width;
  }
}

9. Interfaces

An interface is a special type of declaration in Java. It represents an abstraction of a concept and lays out the blueprint that classes must implement. A class is said to implement an interface when all the methods declared in the interface have been implemented in the class. An example will make things clearer.

Among one of the most commonly used interfaces within Java is the List interface which represents an ordered collection of items. It defines methods that must be implemented by a class to be considered a List. Let us consider a simplified example of this interface, supporting the methods add(), get() and remove().

        public interface List {
  public void add(Object obj);
  public Object get(int index);
  public void remove(int index);
}

A class implementing this interface must implement all these methods. The ArrayList class implements this interface using an array-backed storage system. It might be declared as follows:

        public class ArrayList implements List {
  // private field member used for storage
  private Object[] storage;

  public void add(Object obj) {
    // implements add() here
  }

  public Object get(int index) {
    // implements get() here
  }

  public void remove(int index) {
    // implements remove() here
  }
}

10. Packages

A package in Java is a unit of organization. A class is defined within a package, and related classes are grouped together in a single package. Package names are, by convention, organized in a hierarchical naming scheme starting with the company domain name reversed. For example, a company with a domain name of example.com could define a package called com.example.shapes, and implement a class called Circle within this package.

Packages are created in a folder with the same hierarchy of sub-folders as the named components. The Circle class above would be created within the folder com/example/shapes.

Java Packages

With this brief introduction to core Java concepts, you should now have a good idea of the terminology used in the Java world and be well-equipped for further Java training.

What other Java topics would you like to see covered? Share your ideas in the comments section below!

Image Credit: Maksim Kabakou via Shutterstock.com