Quick Reference and Glossary
Java Language Syntax, Operators and Terminology
package-private classes (non-public
class, but not a nested class).
Java in a Nutshell
Java™ was released around 1994/5 by Sun Microsystems, acquired by Oracle® in 2010. James Gosling, Mike Sheridan and Patrick Naughton drove the project. Oracle finally relented, and deprecated applets in browsers with JDK 9, announced beginning of 2016. IBM® arguably helps drive Java adoption, via donations and open source contributions since 2001. A big name in Java open source, is the Apache Software Foundation. Consideration should also be given to the OpenJDK.
Virtual Machine
Java code is compiled to bytecode with a file extension of .class
, and run in a virtual machine (JVM), which means it should run anywhere a Java Runtime Environment (JRE) is available. For performance, it is compiled to native code “just in time” (JIT). Compiled bytecode can be packaged in “jars” (Zip files with a .jar
extension).
Memory Management
The JVM manages dynamic memory allocation on the programmer’s behalf. Java employs several garbage collection algorithms to suit different requirements, as a performance consideration.
Just in Time Compilation
To enhance performance, it is the norm today for even some scripting languages, to compile an intermediate machine language code (bytecode in Java), to native machine code, when needed. The JVM does this on the fly (just in time or JIT), when a Java program is executed. The JVM optionally allows for this native code to persist, which would speed up subsequent running of the program.
Native Code
The JRE can load and run native code (typically compiled C/C++ libraries) via the Java Native Interface (JNI) or, much easier, with the third-party library: Java Native Access (JNA), which must be downloaded separately.
Language Paradigms
The Java language adheres to the object-oriented programming (OOP) paradigm, with a nod to functional programming using lambdas. For performance, Java supports templates or generic programming (“generics”).
Heritage
Syntax-wise, Java superficially resembles C/C++. Curly-brace delimited blocks thus enclose all language structures, apart from comments, optional package
declarations, and optional import
directives. Blocks create scope, and can be nested, though not all blocks are equal: class blocks, method blocks, initialiser list blocks, compound statement blocks.
Identifiers
The names allocated to variables, types, methods, and enumerated values are formally, all identifiers. Languages have rules with regard the characters that may be used in identifiers.
Syntax — Identifiers
- Cannot be a keyword, and is case sensitive.
- First character must be:
_
,a
…z
,A
…Z
.- From second character:
_
,a
…z
,A
…Z
,0
…9
.- Single underscore not allowed (JDK 9).
Apart from the last point, these are the same rules employed in many C-like languages.
Typed Language
Java is statically typed, which means types of all objects must be specified at compile time, but via reflection, can load libraries and bind methods or classes at runtime. Furthermore, Java is strongly typed, which means that an object cannot change its type once created, and that only well-defined operations are allowed on each type.
Expressions
All values are expressions. Every expression results in a value. Complex expressions may contain operators. Every value has a type, thus every expression has a type (because it results in one value regardless of complexity).
User-Defined Types
The Java Platform not only provides the primitive types, but also a huge number of types via the class libraries. Programmers can create their own types, mostly class
types.
Syntax — User-Defined Types
In a sense, all user-defined types are classes, while interface
and enum
create specialised user-defined classes.
Type Conversion
Type conversions for certain types are possible — which for primitive types, means a copy is made of a value, and transformations applied to produce a new value, with a new type. This is not a general mechanism, although every type can be converted to some string representation (toString()
). Explicit type conversion (type cast), when legal, is performed with a cast operator.
Syntax — Cast Operator
As with the results of all operators, the return value should be passed as argument, returned, or assigned to a variable of ‹type› for it to be useful. If the ‹type› of the ‹expr›ession is the same as the ‹target-type›, it is still legal, just not useful.
Access Control
Top-level classes can have public
access. If this is omitted, it is called “package-private”, but this cannot be explicitly specified. Package-private classes can only be accessed from classes in the same package. The default access for members, is also “package-private”.
Syntax — Top Level Class Access
Syntax — Member Access Specifiers
- ‹access›
public
┆protected
┆private
Member classes (nested classes), as with other members, can have public
, private
or protected
access; where private
means only code in the same class have access, while protected
is like private
, with the exception that derived classes also have access; and public
means all code have access.
Encapsulation
Modelling a type, firstly involves defining the state (fields) and behaviour (methods) of an object, called members. Encapsulation is useful in its own right, without involving inheritance or polymorphism. The physical ordering of members in a class is not significant.
Pattern — Encapsulation
[
public
]class
‹ident›{
‹state› instance data members/fields
‹behaviour› instance member functions/methods
‹construction› instance methods to initialise new objects
‹shared-members› data/methods [shared](#shared-members] by all objects of this type
‹static-initialiser› initialiser(#static-initialiser) for shared data members
}
Fields
A class can define instance data members, called fields, which are duplicated in each object. The values of the collection of fields in an object, represents its state at any given time.
Syntax — Instance Data Members/Fields
Methods
Functions can also logically be duplicated as instance methods. To avoid physical duplication, Java provides instance methods with an implicit this
first parameter, which it also passes automatically. References to other members inside an instance method, are implicit via the this
parameter.
Syntax — Instance Member Function/Method
More than one return
statement is allowed, though not necessarily good programming practice. A return;
statement is only legal where functions return void
(nothing).
Constructors
Construction / Initialisation
To facilitate custom initialisation of new objects, Java provides a syntax for instance constructors, which, like other methods, can be overloaded. Operators cannot be overloaded. Destruction is not deterministic, and patterns have to be followed for resources that requires more deterministic cleanup (override finalize
, provide a close
method, etc.).
Shared Members
Shared / static
fields as well as methods can be created. A shared method does not have an implicit this
parameter, and can thus only access other static
members.
Syntax — Shared Method/Function
- ‹access›
static
‹type›┆void
‹ident›(
[‹param›]) {
‹statement›…
return
‹expr›;
}
- ‹access›
public
┆protected
┆private
- ‹type› return type of shared method or
void
(no return value).- ‹param› optional ‹param›eters of the method: ‹type1› ‹ident1›
,
….
Symbolic Constants
Symbolic constants are created with the final static
clause, and consequently often called “finals”. They can be initialised with run time ‹expr›essions, and can be of any ‹type›. As convention, the ‹ident›ifier is often in all-capital letters.
Syntax — Symbolic Constants
Inheritance & Interfaces
Only single inheritance is allowed, but a class may implement multiple interfaces. Encapsulation supports shared (static
) fields and methods, apart from the normal instance fields and methods.
Polymorphism
It is important to understand that all instance methods in Java, that are not private
or final
, are automatically virtual functions. This means they can be overridden in derived classes. It is good convention to use the @Override
annotation when overriding methods. Note that in the example below, every public
class must be in a different file:
public class Base {
public void Foo () { … }
}
public class Deriv extends Base {
@Override
public void Foo () { … }
}
Packages
Namespaces in Java are implemented via a nested directory structure, which must match package
declarations. This is the reason behind import
directives, and CLASSPATH
settings. The term class path is prevalent, and almost all tools accept a command line option to specify a list of class paths to resolve names of required classes.
Modules
JDK 9
Source Code
Source code are stored in files with the .java
extension. Files may be UTF-8 encoded, but must be compiled with the -encoding utf8
option to javac
, the Java bytecode compiler. Only one public class per .java
file is allowed, and the class name must exactly match the file name.
Structure
Free Format
Whitespace serves only to separate tokens which would otherwise be ambiguous. Formatting is thus left up to the programmer; this means Java is a free format language.
Identifiers
User-defined identifiers (names) can start with alphabetic or underscore character. Subsequent characters may include decimal digits. Identifiers cannot be keywords. Avoid Unicode identifiers.
Javadoc
Language elements like packages, classes, methods and fields can be formally documented, and are called “Javadocs”. These are in the form of special comments, and processed with the javadoc
command line tool.
Building Blocks
Within a Java source file, several language elements can appear in a documented sequence and at specific locations. This form the grammar and syntax of the language, starting with the most fundamental: keywords.
Keywords
Java has a number of keywords which are reserved words, and cannot be used for purposes other than intended. In addition, the language specifies a number of reserved words, which has no purpose (yet), but should not be used for user-defined identifiers.
abstract default goto package synchronized
assert do if private this
boolean double implements protected throw
break else import public throws
byte enum instanceof return transient
case extends int short true
catch false interface static try
char final long strictfp void
class finally native super volatile
const float new switch while
continue for null
Note that technically, null
, true
and false
are literals, while const
and goto
are reserved; so is a single underscore (_
) character (since JDK 9).
JDK 9 adds a few more contextual, or restricted keywords, which are only keywords in certain places in the structure of a program. You should treat them as keywords anyway in new programs, even Java 8 programs to ensure a smooth eventual migration to JDK 9.
Primitive Types and Wrappers
Primitive types are not objects. They do not have any methods, but can be boxed in wrapper classes, which do have methods. This is a different approach compared to C#, where these are value types.
Wrappers classes for the primitive types above provides methods and makes the types effectively nullable.
Notice that String
is not a primitive type, but a full-blown OOP type (reference type), even if String
type values are immutable.
Literals
Some literals are keywords: null
, true
and false
. Other literals are immediate values, each with a type (all values have a type).
Numerical Literals
Character and String Literals
Operators
Javadoc
Glossary
abstract class
When a OO design dictates that a class should only provide structure to derived classes, and that it is not intended to be instantiated as an object, we can make it an abstract
class. Java will now ensure we adheres to our own design.
access control
This is a permissions control, and determines which code has the rights to access a member. It must not be confused with scope or inheritance.
annotation
Also called attributes in other languages, relating to a syntax for attaching a special value to classes or methods, that may change the behaviour of the compiler from the norm. In Java, an annotation starts with the at character (@
) e.g.: @Override
or @Deprecated
, and immediately precedes the class or method definition.
dynamic binding
When a compiler resolves name (addresses) of classes and methods at run time, we call it dynamic binding, the opposite of which, is static binding.
expression
All values are expressions. Every expression results in a value. Complex expressions may contain operators. Every value has a type, thus every expression has a type (because it results in one value regardless of complexity).
garbage collection
A term normally employed in languages that automatically manage dynamically allocated memory. They collect unused (unreferenced) memory, using reference counting. Java has several garbage collectors for different cases, but this is transparent to the programmer.
instance member
Members that are physically or logically copies, so that each object (instance of a class), has its own copy (instance) of the member. To avoid physically copy methods, OOP languages arrange for instance methods to have an implicit this
(or self
) parameter, and automatically used on references to other instance members.
JDK — Java Development Kit
A superset of the JRE (Java Runtime Environment), allowing developers to create Java programs. It includes tools, classes and documentation not available in the JRE.
JNA — Java Native Access
This third party library makes it easier to call natively compiled code (normally compiled C/C++ libraries), than the official Java Native Interface (JNI). Unless you have a good reason, rather use JNA than JNI.
JNI — Java Native Interface
The official mechanism, tools and classes that allows Java code to call compiled C/C++ functions, and interchange data. It involves access to a C/C++ compiler to write wrapper functions.
JIT — Just In Time (Compilation)
In Java, this means the bytecode in class is translated “on-the-fly” to native machine code the host CPU can execute directly. This can be done ahead of time, if needed, to speed up the start up time for bigger Java programs.
JRE — Java Runtime Environment
Java Runtime Environment. The Java Virtual Machine (JVM), packaged with class loaders, the JIT (Just In Time) compiler, and a set of supporting classes. This is the minimum package required to run programs created with the JDK. There is a “compact framework” with fewer classes, for use in smaller devices or embedded systems.
literal
Unfortunately, too often called “constant”; probably because literal values are immediate (no lookup or typical data storage) and have a constant value. The term literal therefore refers to a syntax in programming languages, to represent immediate values.
nullable
A value is nullable when its type is a reference type, which in Java, means a class
type. This means you can assign the null
literal to the value.
object
Formally, an object is “an instance of a class”. This does not mean variable. An object is explicitly or implicitly created with the new
operator in Java. Variables of the correct type, can store a reference to any object of the same type.
operator
A syntactical notation that are passed arguments (called operands), and like functions: logically a) performs a task/job and b) returns a value, or results in a value, which may or may not be of the same type as its operands. Most languages like Java, use ASCII symbols like: +
, -
, *
, /
, etc. as operators.
polymorphism
A desirable feature of OOP, where methods are dynamically bound at run time, based on the actual type of the object being referenced (not the type of the reference necessarily). In Java, this requires virtual
methods, that can be overridden in derived classes.
reference
A reference is an abstraction of a memory address. It is not called pointer or address, because you cannot use it directly as a number (which is what an address is). This term is common in most OO languages, except in C++, where you can manipulate a pointer as a number, even perform arithmetic on it (although it does have a special notation to create references). Using JNI/JNA, Java can deal with pointers.
reflection
In some programming language, either via syntax, or libraries (as in Java), code can dynamically (at runtime) resolve (or bind) the locations of classes or methods. This is called reflection, and depending on support, may even allow for the dynamic creation of language elements, even code.
shared member
When, by design, a data member (field) or member function (method) are to be shared by all objects, and not each object having its own instance of the member, we can make it shared in Java, with the static
keyword.
static binding
When the compiler resolves addresses of types and methods at run time, we call it static binding, which has nothing to do with the static
keyword. The opposite of this, is dynamic binding.
statically typed
Java is a statically typed language, which means all types must be known at compile time. The concept of types is a compile-time abstraction, that determines storage size in memory or in object files; and the bytecode instructions emitted to work with values.
type
An abstraction employed in many programming languages, including Java. All values have a type, which determines the operations (methods and operators) which can be applied to the value. In some instances, a value of one type, can be converted (cast) to a value with another type.
2017-12-25: Created. [brx]