Menu Close

Articles

Java: Fundamentals

Java is a language inspired from C++. Therefore if you know C++ you will find many familiar notations and operators. In this article I will explain the fundamentals of java syntax.

Punctuation

Java is a case sensitive language. It has reserved keywords that may be full English words or abbreviations. Most keywords are lowercase. For names Java is using a curious notation that is based on alternation between uppercase and lowercase letters called CammelCase. 

Java is pure Object Oriented language. For this we define “classes”. Each class name must start with uppercase letter then can be followed by lowercase letters and uppercase letters or numbers or underscore. 

Statement

Java is a statement based language in contrast with other newer languages that can be expression based. So you need to understand this concept first before you learn the language. Later we explain also the expressions. Java uses simple statements or composite statements.

  • A simple statement usually is an expression on a single row or multiple lines. A statement is ending with semicolon “;”.  We can have multiple statements in a single line.
  • A composite statement  usually starts with a keyword and can contain one or more blocks of code that contain statements. So we can have nested statements.

Java uses curly brackets {…} to mark the beginning and ending of a “block of code” like C and any other language from curly bracket family. This makes the program sometimes hard to read, therefore curly brackets are organized using spaces and are usually aligned using code indentation.

Code indentation is optional in Java. However it is strongly recommended. For proper indentation professional developers convert tabs into 4 spaces and do not use tab indentations in a program file.

See also: Java Keywords

Declarations

Java is a strong typed language. That means we need to define data using types and names before we can create expressions. To do this we create identifiers and we associate data types to these identifiers.

An identifier start with a letter that can be lowercase or uppercase. Identifiers are associated to data types or can represent data types themselves. After we define one data type we can create other identifiers to use this data type. In Java user defined data types are called “Classes”.

Expressions

An expression is usually created with one or more identifiers, operators and parentheses. Sometimes multiple expressions are enumerated using semicolon “;” or comma “,”. Round parentheses are used to modify the order of operations like in mathematics. However the square parentheses […] and curly {…}  parenthesis are used a little bit different.

Operators 

Learning the operators is the easy part of a language and is important for learning everything else. So read this table careful to understand Java expressions later. Many developers consider operators trivial but you can do many mistakes in a program if you do not understand them properly.

See also: Java Operators

Primitive Types

Java has eight special types named primitive types. Users can’t create primitive types. Learning primitive data types is a fundamental skill you need before you can declare classes, properties and methods. Here are these types:

  • boolean, the type whose values are either true or false
  • char, the character type whose values are 16-bit Unicode characters
  • the integral types: byte, short, int, long  
  • the floating-point types: float, double

Control Flow

The control flow is a metaphor. Imagine the computer program as a river. The river flow from top of the mountain that is the start point. Downhill it can form branches that can join together back into the main flow. Likewise logic of the program can split and join back to the main branch. For this we use special statements that control the program actions.

See also: Control Flow

Arrays

An array is a group of variables that are referred to by a common name. Variables in array are called array elements. Each element in array are accessed using an index number that start from 0 to n-1 where n is the length of the array.

See also: Java Arrays

Classes

In Java we define new data types using primitive data types and concept the concept of Classes. This is a fundamental concept of Object Oriented programming paradigm. To understand this paradigm you should study CSE: Engineering class. But if you do not here is a short reminder of this paradigm:

In Object Oriented programming we model a problem using Classes and Objects. A Class is a template or we can say it is a group of statements that can define properties and methods. The properties are like variables and the methods are like functions. 

So with other words a Class is a composite data type with associated methods. A class has members that are properties or methods. To use a member we can use the name of the class followed by dot operator and followed by the member name. Not all members can be used in this way.

See also: Java Classes

Objects

Once you define a Class you can use this class like a data type to define Objects. The Objects are like variables but in Java we do not define variables we define “object instances”. So an “object instance” will be a concrete representation of data.

For examples we define a Class that will represent all Persons in general. One person can have a different name and age. An individual person is represented using the concept of “Person Instance”. The name and age are two properties of a Person.

See also: Java Objects

Properties

In Java we do not declare variables like in other languages. Instead we declare properties or attributes. A property do not hold a value until an object is instantiated. That means each property is related to an object. You can have multiple objects from the same class. That means one property can have multiple values, one value for each object instance.

Methods

A method is a group of statements that have a name, a list of parameters and a result type. In Java a method can have no result. In this case we must specify type “void”. So user knows the method is not a function and can’t be used in expressions. 

One method can return a result or not. If a method create a result it is also called function. Unfortunately Java is not a functional programming language, so in Java we do not have a “function” keyword to differentiate a method from a function declaration.

Access Modifiers

Java is using “access modifiers” to hide or expose class members to external use. Here are the modifiers you can use in diverse member declarations to modify access level:

  • public
  • private
  • protected

Other Modifiers

Java provide “non access modifiers” for other purposes:

  • static
  • final
  • abstract
  • synchronized 
  • volatile 

Static Members

Static members belong to Class and not to Object instance. That is something difficult to grasp for beginners. It is a work around to the fact that Java is “pure” Object Oriented. You can’t define anything outside of a class. 

In object-oriented programming, a “class variable” is a variable defined in a class of which a single copy exists, regardless of how many instances of the class exist. So if we define a property of a Class and we use “static” keyword then the property become “class variable”.

If we define a “static” method this method becomes “class method” and can be used from outside of the class using dot operator. There is a catch though. In a static method we can’t use any other member that is not “static”.

Generics

Generics are classes that are not very specific for a particular data type. For example a dynamic list has the same logic but can apply to different primitive types: List of integers, List of floats and List of Double. In this case we can use a generic library to create a specific List.

This mechanism was created to relax a little bit the strong typing paradigm. It reduces the required lines of code we need to write for a specific design pattern. For dynamic programming languages this is not necessary since a particular object or data member can have any data type.

Encapsulation

Encapsulation in Java is a mechanism of wrapping the data (properties and attributes) and code acting on the data (methods) together as a single unit. Declare the variables of a class as private makes encapsulation stronger.

Inheritance

In Java you can define one class based on other class. This mechanism is called Inheritance. All classes are derived from a primary class called the Object class. So in other words a Class can “extend” another class. 

In practice sometimes this model do not work. So we need to define other concepts: “mixin” and “dependency injection” that are advanced topics for future articles.

Polymorphism

A derived class “inherit” all properties and methods from base class. Sometimes we can “overwrite” a method to change the default behavior of base class. This allow us to extend and modify one class to create a different class that have similar but not exact the same behavior.

Abstraction

We can use “abstract” modifier to declare a special Class that can’t be instantiated. This kind of class is usually used as a base class to create “extended” class using a pattern. The pattern implements some but not all the functionality. Some functionality is implemented in the extended class. 

Interfaces

Interfaces are “abstract” concepts that force a specific behavior. Interfaces are almost the same as “abstraction” except that one class can implement multiple interfaces but can extend only one abstract class. So that’s the main difference.

An interface use to have all methods abstract in older Java versions.  Now you can create methods that have a default implementation using an interface. However most developers are using abstract method declarations for interfaces.

Packages

In Java the source code is created using files. Each file can contain one single Public class. You can create a Java program without packages. However a larger project need packages. One package is an abstract concept used for 3 main advantages.

  • Preventing naming conflicts;
  • Making searching/locating and usage of classes easier;
  • Providing controlled access to package members;

Packages can be reused. A package that is reusable is called library. Java packages use a curious name convention. It is closely related to folder names and is backward domain name and folder name structure. For example:  “net.sagecode.axia.engine” can be the name of a package used by Axia project.

Read next: Regular Expression