HOME

TheInfoList



OR:

The
syntax In linguistics, syntax () is the study of how words and morphemes combine to form larger units such as phrases and sentences. Central concerns of syntax include word order, grammatical relations, hierarchical sentence structure ( constituency) ...
of
Java Java (; id, Jawa, ; jv, ꦗꦮ; su, ) is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea to the north. With a population of 151.6 million people, Java is the world's List ...
refers to the set of rules defining how a Java program is written and interpreted. The syntax is mostly derived from C and
C++ C++ (pronounced "C plus plus") is a high-level general-purpose programming language created by Danish computer scientist Bjarne Stroustrup as an extension of the C programming language, or "C with Classes". The language has expanded significan ...
. Unlike in C++, in Java there are no global functions or variables, but there are data members which are also regarded as
global variable In computer programming, a global variable is a variable with global scope, meaning that it is visible (hence accessible) throughout the program, unless shadowed. The set of all global variables is known as the ''global environment'' or ''global s ...
s. All code belongs to classes and all values are
objects Object may refer to: General meanings * Object (philosophy), a thing, being, or concept ** Object (abstract), an object which does not exist at any particular time or place ** Physical object, an identifiable collection of matter * Goal, an ...
. The only exception is the
primitive types In computer science, primitive data types are a set of basic data types from which all other data types are constructed. Specifically it often refers to the limited set of data representations in use by a particular processor, which all compiled pr ...
, which are not represented by a class instance for performance reasons (though can be automatically converted to objects and vice versa via
autoboxing In computer science, boxing (a.k.a. wrapping) is the transformation of placing a primitive type within an object so that the value can be used as a reference. Unboxing is the reverse transformation of extracting the primitive value from its wrap ...
). Some features like
operator overloading In computer programming, operator overloading, sometimes termed ''operator ad hoc polymorphism'', is a specific case of polymorphism, where different operators have different implementations depending on their arguments. Operator overloading is ...
or
unsigned integer In computer science, an integer is a datum of integral data type, a data type that represents some range of mathematical integers. Integral data types may be of different sizes and may or may not be allowed to contain negative values. Integers are ...
types are omitted to simplify the language and to avoid possible programming mistakes. The Java syntax has been gradually extended in the course of numerous major
JDK The Java Development Kit (JDK) is a distribution of Java Technology by Oracle Corporation. It implements the Java Language Specification (JLS) and the Java Virtual Machine Specification (JVMS) and provides the Standard Edition (SE) of the Java ...
releases, and now supports capabilities such as
generic programming Generic programming is a style of computer programming in which algorithms are written in terms of types ''to-be-specified-later'' that are then ''instantiated'' when needed for specific types provided as parameters. This approach, pioneered b ...
and
function literals In computer programming, an anonymous function (function literal, lambda abstraction, lambda function, lambda expression or block) is a function definition that is not bound to an identifier. Anonymous functions are often arguments being passed to ...
(called lambda expressions in Java). Since 2017, a new JDK version is released twice a year, with each release bringing incremental improvements to the language.


Basics


Identifier

An
identifier An identifier is a name that identifies (that is, labels the identity of) either a unique object or a unique ''class'' of objects, where the "object" or class may be an idea, physical countable object (or class thereof), or physical noncountable ...
is the name of an element in the
code In communications and information processing, code is a system of rules to convert information—such as a letter, word, sound, image, or gesture—into another form, sometimes shortened or secret, for communication through a communication ...
. There are certain standard
naming conventions A naming convention is a convention (generally agreed scheme) for naming things. Conventions differ in their intents, which may include to: * Allow useful information to be deduced from the names based on regularities. For instance, in Manhatta ...
to follow when selecting names for elements. Identifiers in Java are
case-sensitive In computers, case sensitivity defines whether uppercase and lowercase letters are treated as distinct (case-sensitive) or equivalent (case-insensitive). For instance, when users interested in learning about dogs search an e-book, "dog" and "Dog" a ...
. An identifier can contain: * Any Unicode character that is a letter (including numeric letters like
Roman numerals Roman numerals are a numeral system that originated in ancient Rome and remained the usual way of writing numbers throughout Europe well into the Late Middle Ages. Numbers are written with combinations of letters from the Latin alphabet, eac ...
) or digit. *
Currency sign A currency symbol or currency sign is a graphic symbol used to denote a currency unit. Usually it is defined by the monetary authority, like the national central bank for the currency concerned. In formatting, the symbol can use various format ...
(such as ¥). * Connecting punctuation character (such as _). An identifier cannot: * Start with a digit. * Be equal to a reserved keyword, null literal or boolean literal.


Keywords


Literals

Integer literals are of int type by default unless long type is specified by appending L or l suffix to the literal, e.g. 367L. Since Java SE 7, it is possible to include underscores between the digits of a number to increase readability; for example, a number can be written as .


Variables

Variables are identifiers associated with values. They are declared by writing the variable's type and name, and are optionally initialized in the same statement by assigning a value. int count; //Declaring an uninitialized variable called 'count', of type 'int' count = 35; //Initializing the variable int count = 35; //Declaring and initializing the variable at the same time Multiple variables of the same type can be declared and initialized in one statement using comma as a delimiter. int a, b; //Declaring multiple variables of the same type int a = 2, b = 3; //Declaring and initializing multiple variables of the same type Since Java 10 it has become possible to infer types for the variables automatically by using var. // stream will have the FileOutputStream type as inferred from its initializer var stream = new FileOutputStream("file.txt"); // An equivalent declaration with an explicit type FileOutputStream stream = new FileOutputStream("file.txt");


Code blocks

The separators signify a code block and a new scope. Class members and the body of a method are examples of what can live inside these braces in various contexts. Inside of method bodies, braces may be used to create new scopes, as follows: void doSomething()


Comments

Java has three kinds of comments: ''traditional comments'', ''end-of-line comments'' and ''documentation comments''. Traditional comments, also known as block comments, start with /* and end with */, they may span across multiple lines. This type of comment was derived from C and C++. /* This is a multi-line comment. It may occupy more than one line. */ End-of-line comments start with // and extend to the end of the current line. This comment type is also present in C++ and in modern C. // This is an end-of-line comment Documentation comments in the source files are processed by the
Javadoc Javadoc (originally cased JavaDoc) is a documentation generator created by Sun Microsystems for the Java language (now owned by Oracle Corporation) for generating API documentation in HTML format from Java source code. The HTML format is used for ...
tool to generate documentation. This type of comment is identical to traditional comments, except it starts with /** and follows conventions defined by the Javadoc tool. Technically, these comments are a special kind of traditional comment and they are not specifically defined in the language specification. /** * This is a documentation comment. * * @author John Doe */


Universal types

Classes in the package java.lang are implicitly imported into every program, as long as no explicitly-imported types have the same names. Important ones include: ;java.lang.Object: Java's
top type In mathematical logic and computer science, some type theories and type systems include a top type that is commonly denoted with top or the symbol ⊤. The top type is sometimes called also ''universal type'', or ''universal supertype'' as all oth ...
. Superclass of all classes that do not declare a parent class. All values can be converted to this type, although for primitive values this involves
autoboxing In computer science, boxing (a.k.a. wrapping) is the transformation of placing a primitive type within an object so that the value can be used as a reference. Unboxing is the reverse transformation of extracting the primitive value from its wrap ...
. ;java.lang.String: Java's basic string type. Immutable. Some methods treat each
UTF-16 UTF-16 (16-bit computing, 16-bit Unicode Transformation Format) is a character encoding capable of encoding all 1,112,064 valid code points of Unicode (in fact this number of code points is dictated by the design of UTF-16). The encoding is variab ...
code unit as a "character", but methods to convert to an int[] that is effectively UTF-32 are also available. ;java.lang.Throwable: supertype of everything that can be exception handling, thrown or caught with Java's throw and catch statements.


Program structure

Java applications consist of collections of classes. Classes exist in packages but can also be nested inside other classes.


main method

Every Java application must have an entry point. This is true of both graphical interface applications and console applications. The entry point is the main method. There can be more than one class with a main method, but the main class is always defined externally (for example, in a
manifest file A manifest file in computing is a file containing metadata for a group of accompanying files that are part of a set or coherent unit. For example, the files of a computer program may have a manifest describing the name, version number, license and t ...
). The main method along with the main class must be declared public. The method must be static and is passed command-line arguments as an array of strings. Unlike
C++ C++ (pronounced "C plus plus") is a high-level general-purpose programming language created by Danish computer scientist Bjarne Stroustrup as an extension of the C programming language, or "C with Classes". The language has expanded significan ...
or C#, it never returns a value and must return void. public static void main(String[] args)


Packages

Packages are a part of a class name and they are used to group and/or distinguish named entities from other ones. Another purpose of packages is to govern code access together with access modifiers. For example, java.io.InputStream is a fully qualified class name for the class InputStream which is located in the package java.io. A package is declared at the start of the file with the package declaration: package myapplication.mylibrary; public class MyClass Classes with the public modifier must be placed in the files with the same name and extension and put into nested folders corresponding to the package name. The above class myapplication.mylibrary.MyClass will have the following path: myapplication/mylibrary/MyClass.java.


Import declaration


Type import declaration

A type import declaration allows a named type to be referred to by a simple name rather than the full name that includes the package. Import declarations can be ''single type import declarations'' or ''import-on-demand declarations''. Import declarations must be placed at the top of a code file after the package declaration. package myPackage; import java.util.Random; // Single type declaration public class ImportsTest Import-on-demand declarations are mentioned in the code. A "type import" imports all the types of the package. A "static import" imports members of the package. import java.util.*; /*This form of importing classes makes all classes in package java.util available by name, could be used instead of the import declaration in the previous example. */ import java.*; /*This statement is legal, but does nothing, since there are no classes directly in package java. All of them are in packages within package java. This does not import all available classes.*/


Static import declaration

This type of declaration has been available since
J2SE 5.0 The Java (programming language), Java language has undergone several changes since Java Development Kit, JDK 1.0 as well as numerous additions of class (computer science), classes and packages to the standard library (computer science), li ...
.
Static import Static import is a feature introduced in the Java programming language that allows members (fields and methods) which have been scoped within their container class as public static, to be used in Java code without specifying the class in which the ...
declarations allow access to static members defined in another class, interface, annotation, or enum; without specifying the class name: import static java.lang.System.out; //'out' is a static field in java.lang.System public class HelloWorld Import-on-demand declarations allow to import all the fields of the type: import static java.lang.System.*; /* This form of declaration makes all fields in the java.lang.System class available by name, and may be used instead of the import declaration in the previous example. */ Enum constants may also be used with static import. For example, this enum is in the package called screen: public enum ColorName ; It is possible to use static import declarations in another class to retrieve the enum constants: import screen.ColorName; import static screen.ColorName.*; public class Dots


Operators

Operators in Java are similar to those in
C++ C++ (pronounced "C plus plus") is a high-level general-purpose programming language created by Danish computer scientist Bjarne Stroustrup as an extension of the C programming language, or "C with Classes". The language has expanded significan ...
. However, there is no delete operator due to
garbage collection Waste collection is a part of the process of waste management. It is the transfer of solid waste from the point of use and disposal to the point of treatment or landfill. Waste collection also includes the curbside collection of recyclabl ...
mechanisms in Java, and there are no operations on
pointers Pointer may refer to: Places * Pointer, Kentucky * Pointers, New Jersey * Pointers Airport, Wasco County, Oregon, United States * The Pointers, a pair of rocks off Antarctica People with the name * Pointer (surname), a surname (including a l ...
since Java does not support them. Another difference is that Java has an unsigned right shift operator (>>>), while C's right shift operator's signedness is type-dependent. Operators in Java cannot be overloaded.


Control structures


Conditional statements


if statement

if statements in Java are similar to those in C and use the same syntax: if (i

3) doSomething();
if statement may include optional else block, in which case it becomes an if-then-else statement: if (i

2) else
Like C, else-if construction does not involve any special keywords, it is formed as a sequence of separate if-then-else statements: if (i

3) else if (i

2) else
Also, note that the ?: operator can be used in place of simple if statement, for example int a = 1; int b = 2; int minVal = (a < b) ? a : b;


switch statement

Switch statement In computer programming languages, a switch statement is a type of selection control mechanism used to allow the value of a variable or expression to change the control flow of program execution via search and map. Switch statements function some ...
s in Java can use byte, short, char, and int (note: not long) primitive data types or their corresponding wrapper types. Starting with J2SE 5.0, it is possible to use enum types. Starting with Java SE 7, it is possible to use Strings. Other
reference type In computer programming, data types can be divided into two categories: value types (or by-value types) and reference types (or by-reference types). Value types are completely represented by their meaning, while reference types are references to ano ...
s cannot be used in switch statements. Possible values are listed using case labels. These labels in Java may contain only constants (including enum constants and string constants). Execution will start after the label corresponding to the expression inside the brackets. An optional default label may be present to declare that the code following it will be executed if none of the case labels correspond to the expression. Code for each label ends with the break keyword. It is possible to omit it causing the execution to proceed to the next label, however, a warning will usually be reported during compilation. switch (ch)


=switch expressions

= Since Java 14 it has become possible to use switch expressions, which use the new arrow syntax: var result = switch (ch) ; Alternatively, there is a possibility to express the same with the yield statement, although it is recommended to prefer the arrow syntax because it avoids the problem of accidental fall throughs. var result = switch (ch) ;


Iteration statements

Iteration statements are statements that are repeatedly executed when a given condition is evaluated as true. Since
J2SE 5.0 The Java (programming language), Java language has undergone several changes since Java Development Kit, JDK 1.0 as well as numerous additions of class (computer science), classes and packages to the standard library (computer science), li ...
, Java has four forms of such statements.


while loop

In the while loop, the test is done before each iteration. while (i < 10)


do ... while loop

In the do ... while loop, the test is done after each iteration. Consequently, the code is always executed at least once. // doSomething() is called at least once do while (i < 10);


for loop

for loops in Java include an initializer, a condition and a counter expression. It is possible to include several expressions of the same kind using comma as delimiter (except in the condition). However, unlike C, the comma is just a delimiter and not an operator. for (int i = 0; i < 10; i++) // A more complex loop using two variables for (int i = 0, j = 9; i < 10; i++, j -= 3) Like C, all three expressions are optional. The following loop is infinite: for (;;)


Enhanced for loop

Enhanced for loops have been available since
J2SE 5.0 The Java (programming language), Java language has undergone several changes since Java Development Kit, JDK 1.0 as well as numerous additions of class (computer science), classes and packages to the standard library (computer science), li ...
. This type of loop uses built-in iterators over arrays and collections to return each item in the given collection. Every element is returned and reachable in the context of the code block. When the block is executed, the next item is returned until there are no items remaining. Unlike C#, this kind of loop does not involve a special keyword, but instead uses a different notation style. for (int i : intArray)


Jump statements


Labels

Labels are given points in code used by break and continue statements. Note that the Java goto keyword cannot be used to jump to specific points in the code. start: someMethod();


break statement

The break statement breaks out of the closest loop or switch statement. Execution continues in the statement after the terminated statement, if any. for (int i = 0; i < 10; i++) It is possible to break out of the outer loop using labels: outer: for (int i = 0; i < 10; i++) // Will break to this point


continue statement

The continue statement discontinues the current iteration of the current control statement and begins the next iteration. The following while loop in the code below reads characters by calling getChar(), skipping the statements in the body of the loop if the characters are spaces: int ch; while (ch

getChar())
Labels can be specified in continue statements and break statements: outer: for (String str : stringsArr)


return statement

The return statement is used to end method execution and to return a value. A value returned by the method is written after the return keyword. If the method returns anything but void, it must use the return statement to return some value. void doSomething(boolean streamClosed) int calculateSum(int a, int b) return statement ends execution immediately, except for one case: if the statement is encountered within a try block and it is complemented by a finally, control is passed to the finally block. void doSomething(boolean streamClosed)


Exception handling statements


try-catch-finally statements

Exceptions are managed within try ... catch blocks. try catch (Exception ex) finally The statements within the try block are executed, and if any of them throws an exception, execution of the block is discontinued and the exception is handled by the catch block. There may be multiple catch blocks, in which case the first block with an exception variable whose type matches the type of the thrown exception is executed. Java SE 7 also introduced multi-catch clauses besides uni-catch clauses. This type of catch clauses allows Java to handle different types of exceptions in a single block provided they are not subclasses of each other. try catch (IOException , IllegalArgumentException ex) If no catch block matches the type of the thrown exception, the execution of the outer block (or method) containing the try ... catch statement is discontinued, and the exception is passed up and outside the containing block (or method). The exception is propagated upwards through the
call stack In computer science, a call stack is a stack data structure that stores information about the active subroutines of a computer program. This kind of stack is also known as an execution stack, program stack, control stack, run-time stack, or ma ...
until a matching catch block is found within one of the currently active methods. If the exception propagates all the way up to the top-most main method without a matching catch block being found, a textual description of the exception is written to the standard output stream. The statements within the finally block are always executed after the try and catch blocks, whether or not an exception was thrown and even if a return statement was reached. Such blocks are useful for providing clean-up code that is guaranteed to always be executed. The catch and finally blocks are optional, but at least one or the other must be present following the try block.


try-with-resources statements

try-with-resources statements are a special type of try-catch-finally statements introduced as an implementation of the
dispose pattern In object-oriented programming, the dispose pattern is a design pattern for resource management. In this pattern, a resource is held by an object, and released by calling a conventional method – usually called close, dispose, free, release dep ...
in Java SE 7. In a try-with-resources statement the try keyword is followed by initialization of one or more resources that are released automatically when the try block execution is finished. Resources must implement java.lang.AutoCloseable. try-with-resources statements are not required to have a catch or finally block unlike normal try-catch-finally statements. try (FileOutputStream fos = new FileOutputStream("filename"); XMLEncoder xEnc = new XMLEncoder(fos)) catch (IOException ex) Since Java 9 it is possible to use already declared variables: FileOutputStream fos = new FileOutputStream("filename"); XMLEncoder xEnc = new XMLEncoder(fos); try (fos; xEnc) catch (IOException ex)


throw statement

The throw statement is used to throw an exception and end the execution of the block or method. The thrown exception instance is written after the throw statement. void methodThrowingExceptions(Object obj)


Thread concurrency control

Java has built-in tools for multi-thread programming. For the purposes of thread
synchronization Synchronization is the coordination of events to operate a system in unison. For example, the conductor of an orchestra keeps the orchestra synchronized or ''in time''. Systems that operate with all parts in synchrony are said to be synchronou ...
the synchronized statement is included in Java language. To make a code block synchronized, it is preceded by the synchronized keyword followed by the lock object inside the brackets. When the executing thread reaches the synchronized block, it acquires a
mutual exclusion In computer science, mutual exclusion is a property of concurrency control, which is instituted for the purpose of preventing race conditions. It is the requirement that one thread of execution never enters a critical section while a concurrent ...
lock, executes the block, then releases the lock. No threads may enter this block until the lock is released. Any non-null reference type may be used as the lock. /* Acquires lock on someObject. It must be of a reference type and must be non-null */ synchronized (someObject)


assert statement

assert statements have been available since J2SE 1.4. These types of statements are used to make assertions in the source code, which can be turned on and off during execution for specific classes or packages. To declare an assertion the assert keyword is used followed by a conditional expression. If it evaluates to false when the statement is executed, an exception is thrown. This statement can include a colon followed by another expression, which will act as the exception's detail message. // If n equals 0, AssertionError is thrown assert n != 0; /* If n equals 0, AssertionError will be thrown with the message after the colon */ assert n != 0 : "n was equal to zero";


Primitive types

Primitive types in Java include integer types, floating-point numbers,
UTF-16 UTF-16 (16-bit computing, 16-bit Unicode Transformation Format) is a character encoding capable of encoding all 1,112,064 valid code points of Unicode (in fact this number of code points is dictated by the design of UTF-16). The encoding is variab ...
code units and a boolean type. There are no unsigned types in Java except char type, which is used to represent UTF-16 code units. The lack of unsigned types is offset by introducing unsigned right shift operation (>>>), which is not present in C++. Nevertheless, criticisms have been leveled about the lack of compatibility with C and C++ this causes. char does not necessarily correspond to a single character. It may represent a part of a surrogate pair, in which case Unicode code point is represented by a sequence of two char values.


Boxing and unboxing

This language feature was introduced in
J2SE 5.0 The Java (programming language), Java language has undergone several changes since Java Development Kit, JDK 1.0 as well as numerous additions of class (computer science), classes and packages to the standard library (computer science), li ...
. ''Boxing'' is the operation of converting a value of a primitive type into a value of a corresponding reference type, which serves as a wrapper for this particular primitive type. ''Unboxing'' is the reverse operation of converting a value of a reference type (previously boxed) into a value of a corresponding primitive type. Neither operation requires an explicit conversion. Example: int foo = 42; // Primitive type Integer bar = foo; /* foo is boxed to bar, bar is of Integer type, which serves as a wrapper for int */ int foo2 = bar; // Unboxed back to primitive type


Reference types

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[] ; // Short syntax int[] numbers2 = ;


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 = ; 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

Class (computer science), 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 variable In class-based, object-oriented programming, an instance variable is a variable defined in a class (i.e. a member variable), for which each instantiated object of the class has a separate copy, or instance. An instance variable has similarities ...
s 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 // 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 The IEEE Standard for Floating-Point Arithmetic (IEEE 754) is a technical standard for floating-point arithmetic established in 1985 by the Institute of Electrical and Electronics Engineers (IEEE). The standard addressed many problems found i ...
and forbids using enhanced precision to store intermediate results.


=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 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 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 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 In computing and computer programming, exception handling is the process of responding to the occurrence of ''exceptions'' – anomalous or exceptional conditions requiring special processing – during the execution of a program. In general, an ...
(except in several special cases). Instance initializers are declared in a block without any keywords: class Foo 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 In computer programming, a default argument is an argument to a function that a programmer is not required to specify. In most programming languages, functions may take one or more arguments. Usually, each argument must be specified in full (this ...
values and methods are usually overloaded instead. class Foo 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. void openStream() throws IOException, myException


=Modifiers

= * abstract -
Abstract method A method in object-oriented programming (OOP) is a Procedure (computer science), procedure associated with a Message passing, message and an Object (computer science), object. An object consists of ''state data'' and ''behavior''; these compose an ...
s can be present only in
abstract class In programming languages, an abstract type is a type in a nominative type system that cannot be instantiated directly; a type that is not abstract – which ''can'' be instantiated – is called a ''concrete type''. Every instance of an abstra ...
es, 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 software design, the Java Native Interface (JNI) is a foreign function interface programming software framework, framework that enables Java (programming language), Java code running in a Java virtual machine (JVM) to call and be called by n ...
in platform-dependent code. Actual implementation happens outside Java code, and such methods have no body. * strictfp - Declares strict conformance to
IEEE 754 The IEEE Standard for Floating-Point Arithmetic (IEEE 754) is a technical standard for floating-point arithmetic established in 1985 by the Institute of Electrical and Electronics Engineers (IEEE). The standard addressed many problems found i ...
in carrying out floating-point operations. * 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.


=Varargs

= This language feature was introduced in
J2SE 5.0 The Java (programming language), Java language has undergone several changes since Java Development Kit, JDK 1.0 as well as numerous additions of class (computer science), classes and packages to the standard library (computer science), li ...
. 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 In mathematics and in computer programming, a variadic function is a function of indefinite arity, i.e., one which accepts a variable number of arguments. Support for variadic functions differs widely among programming languages. The term ''vari ...
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. void printReport(String header, int... numbers) // Calling varargs method printReport("Report data", 74, 83, 25, 96);


Fields

Fields, or
class variable In class-based, 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. A class variable is not an instance variable. It is a special typ ...
s, can be declared inside the class body to store data. class Foo Fields can be initialized directly when declared. class Foo


=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 In computing, serialization (or serialisation) is the process of translating a data structure or object state into a format that can be stored (e.g. files in secondary storage devices, data buffers in primary storage devices) or transmitted (e ...
. * 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 class Foobar extends Foo


=Overriding methods

= Unlike C++, all non-final methods in Java are virtual and can be overridden by the inheriting classes. class Operation class NewOperation extends Operation


=Abstract classes

= A
Abstract Class
is a class that is incomplete, or to be considered incomplete. 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.dwwwp.test; /** * @author jcrypto */ public class AbstractClass package org.dwwwp.test; /** * @author jcrypto */ public class CustomClass extends AbstractClass Output: org.dwwwp.test.AbstractClass: static block runtime org.dwwwp.test.CustomClass: static block runtime org.dwwwp.test.AbstractClass: instance block runtime org.dwwwp.test.AbstractClass: constructor runtime org.dwwwp.test.CustomClass: instance block runtime org.dwwwp.test.CustomClass: constructor runtime hello from org.dwwwp.test.AbstractClass


Enumerations

This language feature was introduced in
J2SE 5.0 The Java (programming language), Java language has undergone several changes since Java Development Kit, JDK 1.0 as well as numerous additions of class (computer science), classes and packages to the standard library (computer science), li ...
. 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 Enum constants are allowed to have constructors, which are called when the class is loaded: public enum Season Enumerations can have class bodies, in which case they are treated like anonymous classes extending the enum class: public enum Season


Interfaces

Interfaces Interface or interfacing may refer to: Academic journals * Interface (journal), ''Interface'' (journal), by the Electrochemical Society * ''Interface, Journal of Applied Linguistics'', now merged with ''ITL International Journal of Applied Lin ...
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


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. Class implementing an interface must override all its methods, otherwise it must be declared as abstract. interface RequestListener class ActionHandler implements ActionListener, RequestListener //Calling method defined by interface RequestListener listener = new ActionHandler(); /*ActionHandler can be represented as RequestListener...*/ listener.requestReceived(); /*...and thus is known to implement requestReceived() method*/


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 // A method which accepts this interface as a parameter int runCalculation(Calculation calculation) // Using a lambda to call the method runCalculation((number, otherNumber) -> number + otherNumber); // Equivalent code which uses an anonymous class instead runCalculation(new Calculation() ) Lambda's parameters types don't 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) -> ); // A functional interface with a method which has only a single parameter interface StringExtender // Initializing a variable of this type by using a lambda StringExtender extender = input -> input + " Extended";


Method references

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: /* 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 doesn't override it. interface StringManipulator // This is a valid class despite not implementing all the methods class PartialStringManipulator implements StringManipulator


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 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


Annotations

Annotations in Java are a way to embed
metadata Metadata is "data that provides information about other data", but not the content of the data, such as the text of a message or the image itself. There are many distinct types of metadata, including: * Descriptive metadata – the descriptive ...
into code. This language feature was introduced in
J2SE 5.0 The Java (programming language), Java language has undergone several changes since Java Development Kit, JDK 1.0 as well as numerous additions of class (computer science), classes and packages to the standard library (computer science), li ...
.


=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


=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(/*mandatory*/ fileSystemOperations, /*optional*/ networkOperations = true) void openOutputStream() 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

Generics, or parameterized types, or
parametric polymorphism In programming languages and type theory, parametric polymorphism allows a single piece of code to be given a "generic" type, using variables in place of actual types, and then instantiated with particular types as needed. Parametrically polymorph ...
is one of the major features introduced in
J2SE 5.0 The Java (programming language), Java language has undergone several changes since Java Development Kit, JDK 1.0 as well as numerous additions of class (computer science), classes and packages to the standard library (computer science), li ...
. Before generics were introduced, it was required to declare all the types explicitly. With generics it became possible to work in a similar manner with different types without declaring the exact types. The main purpose of generics is to ensure type safety and to detect runtime errors during compilation. Unlike C#, information on the used parameters is not available at runtime due to
type erasure In programming languages, type erasure is the load-time process by which explicit type annotations are removed from a program, before it is executed at run-time. Operational semantics that do not require programs to be accompanied by types are c ...
.Generics in the Run Time (C# Programming Guide)
/ref>


Generic classes

Classes can be parameterized by adding a type variable inside angle brackets (< and >) following the class name. It makes possible the use of this type variable in class members instead of actual types. There can be more than one type variable, in which case they are declared in a comma-separated list. It is possible to limit a type variable to a subtype of some specific class or declare an interface that must be implemented by the type. In this case the type variable is appended by the extends keyword followed by a name of the class or the interface. If the variable is constrained by both class and interface or if there are several interfaces, the class name is written first, followed by interface names with & sign used as the delimiter. /* This class has two type variables, T and V. T must be a subtype of ArrayList and implement Formattable interface */ public class Mapper When a variable of a parameterized type is declared or an instance is created, its type is written exactly in the same format as in the class header, except the actual type is written in the place of the type variable declaration. /* Mapper is created with CustomList as T and Integer as V. CustomList must be a subclass of ArrayList and implement Formattable */ Mapper mapper = new Mapper(); Since Java SE 7, it is possible to use a diamond (<>) in place of type arguments, in which case the latter will be inferred. The following code in Java SE 7 is equivalent to the code in the previous example: Mapper mapper = new Mapper<>(); When declaring a variable for a parameterized type, it is possible to use wildcards instead of explicit type names. Wildcards are expressed by writing ? sign instead of the actual type. It is possible to limit possible types to the subclasses or superclasses of some specific class by writing the extends keyword or the super keyword correspondingly followed by the class name. /* Any Mapper instance with CustomList as the first parameter may be used regardless of the second one.*/ Mapper mapper; mapper = new Mapper(); mapper = new Mapper(); /* Will not accept types that use anything but a subclass of Number as the second parameter */ void addMapper(Mapper mapper)


Generic methods and constructors

Usage of generics may be limited to some particular methods, this concept applies to constructors as well. To declare a parameterized method, type variables are written before the return type of the method in the same format as for the generic classes. In the case of constructor, type variables are declared before the constructor name. class Mapper /* This method will accept only arrays of the same type as the searched item type or its subtype*/ static boolean contains(T item, V[] arr)


Generic interfaces

Interfaces can be parameterized in the similar manner as the classes. interface Expandable // This class is parameterized class Array implements Expandable // And this is not and uses an explicit type instead class IntegerArray implements Expandable


See also

*
Java Platform, Standard Edition Java Platform, Standard Edition (Java SE) is a computing platform for development and deployment of portable code for desktop and server environments. Java SE was formerly known as Java 2 Platform, Standard Edition (J2SE). The platform uses Ja ...


References

*
Patrick Naughton Patrick Naughton is an American software developer and convicted sex offender. He is one of the creators of the Java programming language. Career Early career In 1983, Naughton co-wrote a MacPaint clone, ''Painter's Apprentice'', with ...
,
Herbert Schildt Herbert Schildt is an American computing author, programmer and musician. He has written books about various programming languages. He was also a founding member of the progressive rock band Starcastle. Life Schildt holds both graduate and un ...
. ''Java 2: The Complete Reference'', third edition. The McGraw-Hill Companies, 1999. * *


External links


The Java Language Specification, Third edition
Authoritative description of the Java language * {{Java (Sun) Programming language syntax Java (programming language)