Reference types include class types, interface types, and array types. When the constructor is called, an object is created on the heap and a reference is assigned to the variable. When a variable of an object gets out of scope, the reference is broken and when there are no references left, the object gets marked as garbage. The garbage collector then collects and destroys it some time afterwards. A reference variable is null when it does not reference any object.
Arrays Arrays in Java are created at runtime, just like class instances. Array length is defined at creation and cannot be changed. int[] numbers = new int[5]; numbers[0] = 2; numbers[1] = 5; int x = numbers[0];
Initializers // Long syntax int[] numbers = new int[] {20, 1, 42, 15, 34}; // Short syntax int[] numbers2 = {20, 1, 42, 15, 34};
Multi-dimensional arrays In Java, multi-dimensional arrays are represented as arrays of arrays. Technically, they are represented by arrays of references to other arrays. int[][] numbers = new int[3][3]; numbers[1][2] = 2; int[][] numbers2 = {{2, 3, 2}, {1, 2, 6}, {2, 4, 5}}; Due to the nature of the multi-dimensional arrays, sub-arrays can vary in length, so multi-dimensional arrays are not bound to be rectangular unlike C: int[][] numbers = new int[2][]; // Initialization of the first dimension only numbers[0] = new int[3]; numbers[1] = new int[2];
Classes Classes are fundamentals of an object-oriented language such as Java. They contain members that store and manipulate data. Classes are divided into
top-level and
nested. Nested classes are classes placed inside another class that may access the private members of the enclosing class. Nested classes include
member classes (which may be defined with the
static modifier for simple nesting or without it for inner classes),
local classes and
anonymous classes.
Declaration Instantiation Non-static members of a class define the types of the
instance variables and methods, which are related to the objects created from that class. To create these objects, the class must be instantiated by using the new operator and calling the class constructor. Foo foo = new Foo();
Accessing members Members of both instances and static classes are accessed with the . (dot) operator.
Accessing an instance member Instance members can be accessed through the name of a variable. String foo = "Hello"; String bar = foo.toUpperCase();
Accessing a static class member Static members are accessed by using the name of the class or any other type. This does not require the creation of a class instance. Static members are declared using the static modifier. public class Foo { public static void doSomething() { // ... } } // Calling the static method Foo.doSomething();
Modifiers Modifiers are keywords used to modify declarations of types and type members. Most notably there is a sub-group containing the access modifiers. •
abstract - Specifies that a class only serves as a base class and cannot be instantiated. •
static - Used only for member classes, specifies that the member class does not belong to a specific instance of the containing class. •
final - Classes marked as final cannot be extended from and cannot have any subclasses. •
strictfp - Specifies that all floating-point operations must be carried out conforming to
IEEE 754 and forbids using enhanced precision to store intermediate results.
Abstract class Final class Access modifiers The
access modifiers, or
inheritance modifiers, set the accessibility of classes, methods, and other members. Members marked as public can be reached from anywhere. If a class or its member does not have any modifiers, default access is assumed. public class Foo { int baz() { return 0; } private class Bar { } } The following table shows whether code within a class has access to the class or method depending on the accessing class location and the modifier for the accessed class or class member:
Constructors and initializers A
constructor is a special method called when an object is initialized. Its purpose is to initialize the members of the object. The main differences between constructors and ordinary methods are that constructors are called only when an instance of the class is created and never return anything. Constructors are declared as common methods, but they are named after the class and no return type is specified: class Foo { String str; // Constructor with no arguments Foo() {} // Constructor with one argument Foo(String str) { this.str = str; } } Initializers are blocks of code that are executed when a class or an instance of a class is created. There are two kinds of initializers,
static initializers and
instance initializers. Static initializers initialize static fields when the class is created. They are declared using the static keyword: class Foo { static { // Initialization } } A class is created only once. Therefore, static initializers are not called more than once. On the contrary, instance initializers are automatically called before the call to a constructor every time an instance of the class is created. Unlike constructors instance initializers cannot take any arguments and generally they cannot throw any
checked exceptions (except in several special cases). Instance initializers are declared in a block without any keywords: class Foo { { // Initialization } } Since Java has a garbage collection mechanism, there are no
destructors. However, every object has a finalize() method called prior to garbage collection, which can be
overridden to implement finalization.
Methods All the statements in Java must reside within
methods. Methods are similar to functions except they belong to classes. A method has a return value, a name and usually some parameters initialized when it is called with some arguments. Similar to C++, methods returning nothing have return type declared as void. Unlike in C++, methods in Java are not allowed to have
default argument values and methods are usually overloaded instead. class Foo { int bar(int a, int b) { return (a * 2) + b; } /* Overloaded method with the same name but different set of arguments */ int bar(int a) { return a * 2; } } A method is called using . notation on an object, or in the case of a static method, also on the name of a class. Foo foo = new Foo(); int result = foo.bar(7, 2); // Non-static method is called on foo int finalResult = Math.abs(result); // Static method call The throws keyword indicates that a method throws an exception. All checked exceptions must be listed in a comma-separated list. import java.io.IOException; import java.util.zip.DataFormatException; // Indicates that IOException and DataFormatException may be thrown void operateOnFile(File f) throws IOException, DataFormatException { // ... }
Modifiers •
abstract -
Abstract methods can be present only in
abstract classes, such methods have no body and must be overridden in a subclass unless it is abstract itself. •
static - Makes the method static and accessible without creation of a class instance. However static methods cannot access non-static members in the same class. •
final - Declares that the method cannot be overridden in a subclass. •
native - Indicates that this method is implemented through
JNI in platform-dependent code. Actual implementation happens outside Java code, and such methods have no body. •
strictfp - Declares strict conformance to
IEEE 754 in carrying out floating-point operations. Now obsolete. •
synchronized - Declares that a thread executing this method must acquire monitor. For synchronized methods the monitor is the class instance or java.lang.Class if the method is static. • Access modifiers - Identical to those used with classes.
Final methods Varargs This language feature was introduced in
J2SE 5.0. The last argument of the method may be declared as a variable
arity parameter, in which case the method becomes a variable arity method (as opposed to fixed arity methods) or simply
varargs method. This allows one to pass a variable number of values, of the declared type, to the method as parameters - including no parameters. These values will be available inside the method as an array. // numbers represents varargs void printReport(String header, int... numbers) { System.out.println(header); for (int num : numbers) { System.out.println(num); } } // Calling varargs method printReport("Report data", 74, 83, 25, 96);
Fields Fields, or
class variables, can be declared inside the class body to store data. class Foo { double bar; } Fields can be initialized directly when declared. class Foo { double bar = 2.3; }
Modifiers •
static - Makes the field a static member. •
final - Allows the field to be initialized only once in a constructor or inside initialization block or during its declaration, whichever is earlier. •
transient - Indicates that this field will not be stored during
serialization. •
volatile - If a field is declared volatile, it is ensured that all threads see a consistent value for the variable.
Inheritance Classes in Java can only
inherit from
one class. A class can be derived from any class that is not marked as final. Inheritance is declared using the extends keyword. A class can reference itself using the this keyword and its direct superclass using the super keyword. class Foo { } class Foobar extends Foo { } If a class does not specify its superclass, it implicitly inherits from java.lang.Object class. Thus all classes in Java are subclasses of Object class. If the superclass does not have a constructor without parameters the subclass must specify in its constructors what constructor of the superclass to use. For example: class Foo { public Foo(int n) { // Do something with n } } class Foobar extends Foo { private int number; // Superclass does not have constructor without parameters // so we have to specify what constructor of our superclass to use and how public Foobar(int number) { super(number); this.number = number; } }
Overriding methods Unlike C++, all non-final methods in Java are
virtual and can be overridden by the inheriting classes. class Operation { public int doSomething() { return 0; } } class NewOperation extends Operation { @Override public int doSomething() { return 1; } }
Abstract classes An Abstract Class is a class that is incomplete, or is to be considered incomplete, so cannot be instantiated. A class C has abstract methods if any of the following is true: • C explicitly contains a declaration of an abstract method. • Any of C's superclasses has an abstract method and C neither declares nor inherits a method that implements it. • A direct superinterface of C declares or inherits a method (which is therefore necessarily abstract) and C neither declares nor inherits a method that implements it. • A subclass of an abstract class that is not itself abstract may be instantiated, resulting in the execution of a constructor for the abstract class and, therefore, the execution of the field initializers for instance variables of that class. package org.example.test; public class AbstractClass { private static final String hello; static { System.out.printf("%s: static block runtime%n", Abstract.class.getName()); hello = String.format("hello from %s", AbstractClass.class.getName()); } { System.out.printf("%s: instance block runtime%n", Abstract.class.getName()); } public AbstractClass() { System.out.printf("%s: constructor runtime%n", Abstract.class.getName()); } public static void hello() { System.out.println(hello); } } package org.example.test; public class CustomClass extends AbstractClass { static { System.out.printf("%s: static block runtime%n", CustomClass.class.getName()); } { System.out.printf("%s: instance block runtime%n", CustomClass.class.getName()); } public CustomClass() { System.out.printf("%s: constructor runtime%n", CustomClass.class.getName()); } public static void main(String[] args) { CustomClass nc = new CustomClass(); hello(); AbstractClass.hello(); //also valid } } Output: org.example.test.AbstractClass: static block runtime org.example.test.CustomClass: static block runtime org.example.test.AbstractClass: instance block runtime org.example.test.AbstractClass: constructor runtime org.example.test.CustomClass: instance block runtime org.example.test.CustomClass: constructor runtime hello from org.example.test.AbstractClass
Enumerations This language feature was introduced in
J2SE 5.0. Technically enumerations are a kind of class containing enum constants in its body. Each enum constant defines an instance of the enum type. Enumeration classes cannot be instantiated anywhere except in the enumeration class itself. enum Season { WINTER, SPRING, SUMMER, AUTUMN } Enum constants are allowed to have constructors, which are called when the class is loaded: public enum Season { WINTER("Cold"), SPRING("Warmer"), SUMMER("Hot"), AUTUMN("Cooler"); Season(String description) { this.description = description; } private final String description; public String getDescription() { return description; } } Enumerations can have class bodies, in which case they are treated like anonymous classes extending the enum class: public enum Season { WINTER { String getDescription() {return "cold";} }, SPRING { String getDescription() {return "warmer";} }, SUMMER { String getDescription() {return "hot";} }, FALL { String getDescription() {return "cooler";} }; }
Interfaces Interfaces are types which contain no fields and usually define a number of methods without an actual implementation. They are useful to define a contract with any number of different implementations. Every interface is implicitly abstract. Interface methods are allowed to have a subset of access modifiers depending on the language version, strictfp, which has the same effect as for classes, and also static since Java SE 8. interface ActionListener { int ACTION_ADD = 0; int ACTION_REMOVE = 1; void actionSelected(int action); }
Implementing an interface An interface is implemented by a class using the implements keyword. It is allowed to implement more than one interface, in which case they are written after implements keyword in a comma-separated list. A class implementing an interface must override all its methods, otherwise it must be declared as abstract. interface RequestListener { int requestReceived(); } class ActionHandler implements ActionListener, RequestListener { public void actionSelected(int action) { // ... } public int requestReceived() { // ... } } // Calling method defined by interface // ActionHandler can be represented as RequestListener... RequestListener listener = new ActionHandler(); // ...and thus is known to implement requestReceived() method listener.requestReceived();
Functional interfaces and lambda expressions These features were introduced with the release of Java SE 8. An interface automatically becomes a functional interface if it defines only one method. In this case an implementation can be represented as a lambda expression instead of implementing it in a new class, thus greatly simplifying writing code in the
functional style. Functional interfaces can optionally be annotated with the
@FunctionalInterface annotation, which will tell the compiler to check whether the interface actually conforms to a definition of a functional interface. // A functional interface @FunctionalInterface interface Calculation { int calculate(int someNumber, int someOtherNumber); } // A method which accepts this interface as a parameter int runCalculation(Calculation calculation) { return calculation.calculate(1, 2); } // Using a lambda to call the method runCalculation((number, otherNumber) -> number + otherNumber); // Equivalent code which uses an anonymous class instead runCalculation(new Calculation() { @Override public int calculate(int someNumber, int someOtherNumber) { return someNumber + someOtherNumber; } }) Lambda's parameters types do not have to be fully specified and can be inferred from the interface it implements. Lambda's body can be written without a body block and a return statement if it is only an expression. Also, for those interfaces which only have a single parameter in the method, round brackets can be omitted. // Same call as above, but with fully specified types and a body block runCalculation((int number, int otherNumber) -> { return number + otherNumber; }); // A functional interface with a method which has only a single parameter interface StringExtender { String extendString(String input); } // Initializing a variable of this type by using a lambda StringExtender extender = input -> input + " Extended";
Method references Java allows method references using the operator :: (it is not related to the C++ namespace qualifying operator ::). It is not necessary to use lambdas when there already is a named method compatible with the interface. This method can be passed instead of a lambda using a method reference. There are several types of method references: The code above which calls runCalculation could be replaced with the following using the method references: runCalculation(Integer::sum);
Inheritance Interfaces can inherit from other interfaces just like classes. Unlike classes it is allowed to inherit from multiple interfaces. However, it is possible that several interfaces have a field with the same name, in which case it becomes a single ambiguous member, which cannot be accessed. /* Class implementing this interface must implement methods of both ActionListener and RequestListener */ interface EventListener extends ActionListener, RequestListener { }
Default methods Java SE 8 introduced default methods to interfaces which allows developers to add new methods to existing interfaces without breaking compatibility with the classes already implementing the interface. Unlike regular interface methods, default methods have a body which will get called in the case if the implementing class does not override it. interface StringManipulator { String extendString(String input); // A method which is optional to implement default String shortenString(String input) { return input.substring(1); } } // This is a valid class despite not implementing all the methods class PartialStringManipulator implements StringManipulator { @Override public String extendString(String input) { return String.format("%s Extended", input); } }
Static methods Static methods is another language feature introduced in Java SE 8. They behave in exactly the same way as in the classes. interface StringUtils { static String shortenByOneSymbol(String input) { return input.substring(1); } } StringUtils.shortenByOneSymbol("Test");
Private methods Private methods were added in the Java 9 release. An interface can have a method with a body marked as private, in which case it will not be visible to inheriting classes. It can be called from default methods for the purposes of code reuse. interface Logger { default void logError() { log(Level.ERROR); } default void logInfo() { log(Level.INFO); } private void log(Level level) { SystemLogger.log(level.id); } }
Annotations Annotations in Java are a way to embed
metadata into code. This language feature was introduced in
J2SE 5.0.
Annotation types Java has a set of predefined annotation types, but it is allowed to define new ones. An annotation type declaration is a special type of an interface declaration. They are declared in the same way as the interfaces, except the interface keyword is preceded by the @ sign. All annotations are implicitly extended from java.lang.annotation.Annotation and cannot be extended from anything else. @interface BlockingOperations { } Annotations may have the same declarations in the body as the common interfaces, in addition they are allowed to include enums and annotations. The main difference is that abstract method declarations must not have any parameters or throw any exceptions. Also they may have a default value, which is declared using the default keyword after the method name: @interface BlockingOperations { boolean fileSystemOperations(); boolean networkOperations() default false; }
Usage of annotations Annotations may be used in any kind of declaration, whether it is package, class (including enums), interface (including annotations), field, method, parameter, constructor, or local variable. Also they can be used with enum constants. Annotations are declared using the @ sign preceding annotation type name, after which element-value pairs are written inside brackets. All elements with no default value must be assigned a value. @BlockingOperations( fileSystemOperations, // mandatory networkOperations = true // optional ) void openOutputStream() { // annotated method } Besides the generic form, there are two other forms to declare an annotation, which are shorthands.
Marker annotation is a short form, it is used when no values are assigned to elements: @Unused // Shorthand for @Unused() void travelToJupiter() { } The other short form is called
single element annotation. It is used with annotations types containing only one element or in the case when multiple elements are present, but only one elements lacks a default value. In single element annotation form the element name is omitted and only value is written instead: /* Equivalent for @BlockingOperations(fileSystemOperations = true). networkOperations has a default value and does not have to be assigned a value • / @BlockingOperations(true) void openOutputStream() { } ==Generics==