The design of the Java programming language aimed mainly at five goals:
Simplicity
Java is simple compared to other object-oriented programming languages such as C++ or C# because it has a reduced language scope and does not support operator overloading and multiple inheritance, for example.
Object Orientation
Java belongs to the object-oriented programming languages.
Distributed
A number of simple options for network communication, from TCP/IP protocols to remote method invocation to Web services, are provided primarily through Java's class library; the Java language itself does not include direct support for distributed execution.
Familiarity
Because of the syntactic closeness to C++, the original similarity of the class library to Smalltalk class libraries, and the use of design patterns in the class library, Java shows no unexpected effects for the experienced programmer.
Robustness
Many of the design decisions made in the definition of Java reduce the likelihood of unintended system errors; notable among these are strong typing, garbage collection, exception handling, and abandonment of pointer arithmetic.
Security
This is supported by concepts such as the class loader, which controls the secure delivery of class information to the Java Virtual Machine, and security managers, which ensure that access is only allowed to program objects for which appropriate rights are available.
Architecture neutrality
Java is designed so that the same version of a program will, in principle, run on any computer hardware, regardless of its processor or other hardware components.
Portability
In addition to being architecture neutral, Java is portable. This means that primitive data types are standardized in their size and internal representation as well as in their arithmetic behavior. For example, a float is always an IEEE 754 float of 32 bits in length. The same applies, for example, to the class library, which can be used to create a GUI independent of the operating system.
Performance
Java has the potential to achieve better performance than languages limited to compile-time optimizations (C++, etc.) due to the run-time optimization possibility. This is countered by the overhead caused by the Java runtime environment, so that the performance of, for example, C++ programs is surpassed in some contexts, but not achieved in others.
Interpretability
Java is compiled into machine-independent bytecode, which in turn can be interpreted on the target platform. Oracle's Java Virtual Machine interprets Java bytecode before compiling and optimizing it for performance reasons.
Parallelizability
Java supports multithreading, i.e. the parallel execution of independent program sections. For this purpose, the language itself offers the keywords synchronized and volatile - constructs that support the "Monitor & Condition Variable Paradigm" by C. A. R. Hoare. The class library includes further support for parallel programming with threads. Modern JVMs map a Java thread to operating system threads and thus benefit from processors with multiple cores.
Dynamic
Java is structured in such a way that it can be adapted to dynamically changing conditions. Since the modules are linked only at runtime, parts of the software (such as libraries) can be redelivered, for example, without having to adapt the remaining program parts. Interfaces can be used as a basis for communication between two modules; however, the actual implementation can be changed dynamically and, for example, also during runtime.
Object Orientation
The basic idea of object-oriented programming is to combine data and associated functions as closely as possible in a so-called object and to encapsulate them externally (abstraction). The intention behind this is to make large software projects easier to manage and to increase the quality of the software. Another goal of object orientation is a high degree of reusability of software modules.
A new aspect of Java compared to the object-oriented programming languages C++ and Smalltalk is the explicit distinction between interfaces and classes, which is expressed by corresponding keywords interface and class. Java does not support inheritance of multiple independent base classes (so-called "multiple inheritance" as in C++ or Eiffel), but it does support the implementation of an arbitrary number of interfaces, which also solves many of the corresponding problems. Method signatures and standard implementations of methods are passed on to the derived classes, but not attributes.
Java is not completely object-oriented, since the basic data types (int, boolean, etc.) are not objects (see also under Java syntax). However, as of Java 5, they are converted automatically and transparently for the programmer into the corresponding object types and vice versa by means of autoboxing, if required.
Example
Source code
/** * This class is a general class for any animal and provides * methods common to all animals. */ public class Animal { /** * This method lets the animal communicate. The subclasses of this * class can override this method and provide an appropriate * implementation for the animal in question. */ public void communicate() { // Used by all subclasses that do not override this method. System. out. println("Animal says nothing."); } } /** * Declares the class "dog" as a subclass of the class "animal". * The class "Dog" thus inherits the fields and methods of the class "Animal". */ public class Dog extends Animal { /** * This method is implemented in the superclass "Animal". It is * overridden in this class and customized for the "Dog" animal type. */ @Override public void communicate() { // Calls the implementation of this method in the superclass "animal". super. communicate(); // Prints a text to the console. System. out. println("Dog says: 'Woof Woof'"); } } /** * Declares the class "Cat" as a subclass of the class "Animal". * The class "Cat" thus inherits the fields and methods of the class "Animal". */ public class Cat extends Animal { /** * This method is implemented in the superclass "Animal". It is * overridden in this class and adapted for the animal type "cat". */ @Override public void communicate() { // Calls the implementation of this method in the superclass "animal". super. communicate(); // Prints a text to the console. System. out. println("Cat says: 'Meow'"); } } public class Main { /** * Method that is called when the program starts. */ public static void main(String[] args) { // Declares a variable for instances of the classes "dog" and "cat". Tier tier; // Creates an instance of the class "dog" and stores the instance in // the variable "animal animal = new dog(); // Calls the method dog.communicate() animal.communicate(); // Creates an instance of the class "cat" and stores the instance in the variable // the variable "animal animal = new cat(); // calls the method cat.communicate() animal.communicate(); } }
Console output
Animal says nothing. Dog says: 'Woof Woof' Animal says nothing. Cat says, 'Meow
Reflection
Java offers a Reflection API as part of the runtime environment. This makes it possible to access classes and methods at runtime whose existence or exact specification was not known at the time of program creation. This technique is often used in connection with the factory method design pattern.
Annotations
With Java 5, Sun has extended the programming language with annotations. Annotations allow the notation of metadata and enable user-defined language extensions to a certain extent. The purpose of annotations is, among other things, the automatic generation of code and other documents important in software development for recurring patterns based on the shortest possible notes in the source code. Until now, only Javadoc comments with special JavaDoc tags were used for this purpose in Java, which were evaluated by doclets such as the XDoclet.
Annotations can also be contained in the compiled class files. Thus, the source code is not required for their use. In particular, the annotations are also accessible via the Reflection API. For example, they can be used to extend the bean concept.
Modular execution on remote computers
Java provides the ability to write classes that run in different execution environments. For example, applets can be executed in web browsers that support Java. Java's security concept can be used to ensure that unknown classes cannot cause any damage, which is particularly important for applets (see also Sandbox). Examples of Java modules that can be executed in corresponding execution environments are applets, servlets, portlets, MIDlets, xlets, translets, and Enterprise JavaBeans.