Java Core Concepts

Jakob Jenkov
Last update: 2015-02-13

As mentioned in the text about what Java is, Java is more than just the programming language itself. Java is a platform with many subtopics and concepts. Within the Java language itself though, there are a handful of core concepts that are essential to understand. This text will take a closer look at these core Java concepts.

Variables

Computer programs, regardless of programming language, typically read data from somewhere (file, keyboard, mouse, network etc.), process the data, and write some data somewhere again (to screen, file, network etc.).

In a Java program data is kept in variables. Your Java program first declares the variables, then read data into them, execute operations on the variables, and then write the variables (or data based on the variables) somewhere again. Variables are explained in more detail in the text on Java variables.

Each variable has a data type. The data type determines what kind of data the variable can contain, and what operations you can execute on it. For instance, a variable could be a number. Numbers can be added, subtracted, multiplied, divided etc. Or, a variable could be a string (text). String's can be divided into substrings, searched for characters, concatenated with other strings etc. Java comes with a set of built-in data types. These data types are described in more detail in the text on Java data types.

Here is a simple Java variable declaration and operation example. Don't worry if you don't understand it now. Later texts in this Java language tutorial explains the details. The purpose of the example is just to give you a feeling for how working with Java variables look.

int myNumber;

myNumber = 0;

myNumber = myNumber + 5;

The first line of this example declares a variable named myNumber of the data type int. An int is a 32 bit integer (number without fractions).

The second line sets the value of the myNumber variable to 0.

The third line adds 5 to the current value of myNumber (which we just set to 0 in the line above).

Operations

Operations in Java are the instructions you can use to process the data in variables. Some operations read and write the values of variables (as you have already seen examples of), while other operations control the program flow. The most important operations are:

All of these operations are explained in detail in their own texts.

Here are a few examples of operations:

int number = 0;
int abs    = 0;
    
//imagine some operations that assign a value to number 
// - but left out of this example.
    
if(number >= 0) {
    abs = number;    
} else {
    abs = -number; 
}

This example first declares two variables named number and abs. The variable abs is supposed to contain the absolute value of number. The absolute value of a number is always positive. For positive numbers the absolute value is the number itself. For negative numbers the absolute value is the number without the negative sign. For instance, the absolute value of -10 is 10.

The if operation checks the value of the number variable to see if it is larger than or equal to 0. If it is, the absolute value assigned to the abs variable is the value of the number variable. If the number value is less than 0, then the value assigned to number is equal to -number. Negating a negative number gives a positive number, remember? -(-10) is 10.

Classes + Objects

Classes group variables and operations together in coherent modules. A class can have fields, constructors and methods (plus more, but that is not important now). I will shortly describe fields, constructors and methods here, but they are explained in more details in their own texts too.

Objects are instances of classes. When you create an object, that object is of a certain class. The class is like a template (or blueprint) telling how objects of that class should look. When you create an object, you say "give me an object of this class".

If you think of a factory producing lots and lots of the same items, then the class would be the blueprint / manual of how the finished product should look, and the objects would be each of the finished products. If the factory produced cars, then the blueprint / design manual of the cars to produce corresponds to a Java class, and the physical cars produced corresponds to Java objects.

Here is a simple diagram illustrating the principle of objects being of a certain class. The class determines what fields and methods the objects of that class have.

A Java class can contain fields, constructors and methods.
A Java class can contain fields, constructors and methods.

Here is an example Java class declaration:

public class Car {

}

This example declares a class named Car. The Car class does not contain any fields, constructors or methods. It is empty. The example primarily serves to show you an example of how a class declaration looks in Java code.

Fields

A field is a variable that belongs to a class or an object. It is a piece of data, in other words. For instance, a Car class could define the field brand which all Car objects would have. Each Car object could then have a different value for the brand field.

Fields are covered in more detail in the text on Java fields.

Here is the Car class declaration from above with a field name brand added:

public class Car {
    private String brand;
}

This example declares a field named brand of data type String which is text.

Constructors

Constructors are a special kind of method that is executed when an object of that class is created. Constructors typically initialize the objects internal fields - if necessary.

Constructors are covered in more detail in the text on Java constructors.

Here is the Car class from before with a constructor that initializes the brand field:

public class Car {
    
    private String brand;

    public Car(String theBrand) {
        this.brand = theBrand;
    }
}

Methods

Methods are groups of operations that carry out a certain function together. For instance, a method may add to numbers, and divide it by a third number. Or, a method could read and write data in a database etc.

Methods are typically used when you need to group operations together, that you need to be able execute from several different places. Or, if you just want your code to be easier to read. In other programming languages methods may be called "procedures" or "functions".

Methods are covered in more details in the text on Java methods.

Here is the Car class from before with a single, simple method named getBrand added:

public class Car {
    
    private String brand;

    public Car(String theBrand) {
        this.brand = theBrand;
    }
    
    
    public String getBrand() {
        return this.brand;
    }
}

Interfaces

Interfaces is a central concept in Java. An interface describes what methods a certain object should have available on it. A class can implement an interface. When a class implements an interface, the class has to implement all the methods described in the interface. Interfaces are described more in my text about Java interfaces.

Packages

Packages in Java is another central concept. A package is a directory containing Java classes and interfaces. Packages provides a handy way of grouping related classes and interfaces, thus making modularization of your Java code easier. Packages are described in more detail in my text about Java packages.

Jakob Jenkov

Featured Videos

Java ConcurrentMap + ConcurrentHashMap

Java Generics

Java ForkJoinPool

P2P Networks Introduction

















Close TOC
All Tutorial Trails
All Trails
Table of contents (TOC) for this tutorial trail
Trail TOC
Table of contents (TOC) for this tutorial
Page TOC
Previous tutorial in this tutorial trail
Previous
Next tutorial in this tutorial trail
Next