Java Language

From NovaOrdis Knowledge Base
Jump to navigation Jump to search

External

Internal

Lexical Structure

Keywords

Statements

Programming - Statements

synchronized

The synchronized statement acquires a mutual-exclusion lock on behalf of the executing thread, executes a block, then releases the lock. While the executing thread owns the log, no other thread may acquire the lock.

synchronized (expression) block

The type of expression must be a reference type.

Also see:

'synchronized' mechanism

return

Control-flow statement.

Modifiers

Method Modifiers

synchronized

A synchronized class method synchronizes on the monitor associated with the Class object of that class.

A synchronized instance method synchronizes on the monitor associated with this - the object instance it is invoked onto.

Also see:

'synchronized' mechanism
default

Literals

Operators

Remainder Operator %

The binary operator % produces the remainder of its operands from an implied division. The left-hand operand is the dividend and the right-hand operand is the divisor. It accepts integral and floating point operands. The remainder operation for operands that are integers after binary numeric promotion produces a value such that (a/b)*b + (a%b) is equal to a.

TODO: Document the difference between the remainder and modulo.

Types

Java is a statically typed language, which means that every variable and every expression has a type that is known at compile time.

Java is strongly typed language, because the types limit the values a variable can hold or that an expression can produce, limit the operations supported by those types and determine the meaning of operations.

null Type

Primitive Types

The primitive types are boolean and the numeric types.

The numeric types are the integral types byte, short, int, long and char, and the floating-point types float and double.

For more details on how the numbers are represented, see:

Numeric Values Representation in Java

boolean

Integral Primitive Types

byte

1 signed byte (two's complement). Covers values from -128 to 127. More more details see byte representation in two's complement.

short

2 bytes, signed (two's complement), -32,768 to 32,767. For more details see short representation in two's complement.

int

4 bytes, signed (two's complement). -2,147,483,648 to 2,147,483,647. For more details see int representation in two's complement. Like all numeric types ints may be cast into other numeric types (byte, short, long, float, double). When lossy casts are done (e.g. int to byte) the conversion is done modulo the length of the smaller type.

long

8 bytes signed (two's complement). Ranges from -9,223,372,036,854,775,808 to +9,223,372,036,854,775,807. For more details see long representation in two's complement.

char

Contains an 2-byte (0x0000 - 0xFFFF, 0 - 65,535) unsigned Unicode code point. For a detailed discussion on Unicode representation in Java see:

Java and Unicode

Floating Point Primitive Types

float

4 bytes, IEEE 754. Covers a range from 1.40129846432481707e-45 to 3.40282346638528860e+38 (positive or negative). Like all numeric types floats may be cast into other numeric types (byte, short, long, int, double). When lossy casts to integer types are done (e.g. float to short) the fractional part is truncated and the conversion is done modulo the length of the smaller type.

double

8 bytes IEEE 754. Covers a range from 4.94065645841246544e-324d to 1.79769313486231570e+308d (positive or negative).

Reference Types

Java does not use pointers.

There are four kinds of reference types: class types, interface types, type variables and array types.

Class Types

Interface Types

Type Variables

Array Types

In Java arrays are threaded as objects with an array type, even if the primitive type arrays.

An object of an array type has all its elements initialized with 0 or null upon initialization.

Values

The value of a reference type is a references to an object instance.

In case of a lambda expressions, the value of the lambda expression is the reference to a functional interface instance.

Variables

A variable is a storage location and has an associated type - the compile-time type - that is either a primitive type or a reference type.

The value of a variable is changed by an assignment or by a prefix/postfix increment/decrement operators.

Variable Initializer

Variable Types

There are eight kinds of variables:

Class Variable

A class variable is a field declared using the keyword static within a class declaration, or with or without the keyword static within an interface declaration.

Instance Variable

An instance variable is a field declared within a class declaration without using the keyword static. An instance variable is also referred to as field.

Instance variables are stored on the heap, within the heap representation of the instance.

Array Component

Array components are unnamed variables that are created and initialized to default values whenever a new object that is an array is created.

Method Parameter

Method parameters name argument values passed to a method. For every parameter declared in a method declaration, a new parameter variable is created each time that method is invoked. The new variable is initialized with the corresponding argument from the method invocation.

Constructor Parameter

Constructor parameters name argument values passed to a constructor. For every parameter declared in a constructor declaration, a new parameter variable is created each time a class instance creation expression or explicit constructor invocation invokes that constructor. The new variable is initialized with the corresponding argument from the creation expression or constructor invocation.

Lambda Parameter

A lambda parameter name argument values passed to a lambda expression body. For every parameter declared in a lambda expression, a new parameter variable is created each time a method implemented by the lambda body is invoked, and initialized with the corresponding argument value from the invocation.

Exception Parameter

An exception parameter is created each time an exception is caught by a catch clause of a try statement.

Local Variable

Local variables are declared by local variable declaration statements. The local variables are created whenever the flow of control enters a block of a for statement.

Local variable are allocated on the stack.

Final Variables

A final variable may be assigned to once.

Java 8 Effectively Final Variable

A non-final local variable or method parameter whose value is never changed after initialization is known as effectively final. Lambda expression capture and use these variables.

Expressions

Programming - Expressions

Interface

An interface groups related methods together into a contract.

All methods on an interface are implicitly public and abstract (https://docs.oracle.com/javase/specs/jls/se8/html/jls-9.html#jls-9.4). "Every method declaration in the body of an interface is implicitly public. It is permitted, but discouraged as a matter of style, to redundantly specify the public modifier for a method declaration in an interface."

Class

Anonymous Class

Can be used for behavior parameterization.

Abstract Class

Class Instance

Field

See

Instance Variable

Package

Package Name

A package consists in identifiers separated by ".". The identifiers must not be a keyword, boolean literal or null literal and must contain only identifier characters. The first character must be a letter, and the following characters can be letters or digits.

Upper cases and lower cases are allowed and treated distinctly.