Java is a computer programming language that is synchronized, class-based, object-oriented, and purposely designed to have as few implementation dependencies as possible. It is intended to let application developers “write once, run anywhere” (WORA), meaning that code that runs on one platform does not need to be recompiled to run on another. Java applications are typically compiled to bytecode (class file) that can run on any Java virtual machine (JVM) regardless of computer architecture.
Java is, as of 2012, one of the most popular programming languages in use, particularly for client-server web applications, with a reported 9 million developers. Java was originally developed by James Gosling at Sun Microsystems(which has since merged into Oracle Corporation) and released in 1995 as a core component of Sun Microsystems’ Java platform. The language derives much of its syntax from C and C++, but it has fewer low-level facilities than either of them.
The original and reference implementation Java compilers, virtual machines, and class libraries were developed by Sun from 1991 and first released in 1995. As of May 2007, in compliance with the specifications of the Java Community Process, Sun relicensed most of its Java technologies under the GNU General Public License. Others have also developed alternative implementations of these Sun technologies, such as the GNU Compiler for Java (bytecode compiler), GNU Classpath (standard libraries), and IcedTea-Web (browser plugin for applets).
Major release versions of Java, along with their release dates:
- JDK 1.0 (January 21, 1996)
- JDK 1.1 (February 19, 1997)
- J2SE 1.2 (December 8, 1998)
- J2SE 1.3 (May 8, 2000)
- J2SE 1.4 (February 6, 2002)
- J2SE 5.0 (September 30, 2004)
- Java SE 6 (December 11, 2006)
- Java SE 7 (July 28, 2011)
- Java EE 7 (October 27, 2013)
The syntax of Java is largely derived from C++. Unlike C++, which combines the syntax for structured, generic, and object-oriented programming, Java was built almost exclusively as an object-oriented language. All code is written inside a class, and everything is an object, with the exception of the primitive data types (i.e. integers, floating-point numbers, boolean values, and characters), which are not classes for performance reasons.
Unlike C++, Java does not support operator overloading or multiple inheritance for classes. This simplifies the language and aids in preventing potential errors and anti-pattern design.
Java uses similar commenting methods to C++. There are three different styles of comments: a single line style marked with two slashes (//
), a multiple line style opened with /*
and closed with */
, and the Javadoc commenting style opened with /**
and closed with */
. The Javadoc style of commenting allows the user to run the Javadoc executable to compile documentation for the program.
// This is an example of a single line comment using two slashes /* This is an example of a multiple line comment using the slash and asterisk. This type of comment can be used to hold a lot of information or deactivate code, but it is very important to remember to close the comment. */ /** * This is an example of a Javadoc comment; Javadoc can compile documentation * from this text. */ /** Finally, an example of a method written in Java, wrapped in a class. */ package fibsandlies; import java.util.HashMap; public class FibCalculator extends Fibonacci implements Calculator { private static HashMap<Integer, Integer> memoized = new HashMap<Integer, Integer>(); static { memoized.put(1, 1); memoized.put(2, 1); } /** Given a non-negative number FIBINDEX, returns * the Nth Fibonacci number, where N equals FIBINDEX. * @param fibIndex The index of the Fibonacci number * @return The Fibonacci number itself */ @Override public static int fibonacci(int fibIndex) { if (memoized.containsKey(fibIndex)) { return memoized.get(fibIndex); } else { int answer = fibonacci(fibIndex - 1) + fibonacci(fibIndex - 2); memoized.put(fibIndex, answer); return answer; } } }
Another example:
class HelloWorldApp { public static void main(String[] args) { System.out.println("Hello World!"); // Display the string. } }
Yet another one:
// OddEven.java import javax.swing.JOptionPane; public class OddEven { private int userInput; // a whole number("int" means integer) /** * This is the constructor method. It gets called when an object of the OddEven type * is being created. */ public OddEven() { /* * In most Java programs constructors can initialize objects with default values, or create * other objects that this object might use to perform its functions. In some Java programs, the * constructor may simply be an empty function if nothing needs to be initialized prior to the * functioning of the object. In this program's case, an empty constructor would suffice. * A constructor must exist; however, if the user doesn't put one in then the compiler * will create an empty one. */ } /** * This is the main method. It gets called when this class is run through a Java interpreter. * @param args command line arguments (unused) */ public static void main(final String[] args) { /* * This line of code creates a new instance of this class called "number" (also known as an * Object) and initializes it by calling the constructor. The next line of code calls * the "showDialog()" method, which brings up a prompt to ask you for a number. */ OddEven number = new OddEven(); number.showDialog(); } public void showDialog() { /* * "try" makes sure nothing goes wrong. If something does, * the interpreter skips to "catch" to see what it should do. */ try { /* * The code below brings up a JOptionPane, which is a dialog box * The String returned by the "showInputDialog()" method is converted into * an integer, making the program treat it as a number instead of a word. * After that, this method calls a second method, calculate() that will * display either "Even" or "Odd." */ userInput = Integer.parseInt(JOptionPane.showInputDialog("Please enter a number.")); calculate(); } catch (final NumberFormatException e) { /* * Getting in the catch block means that there was a problem with the format of * the number. Probably some letters were typed in instead of a number. */ System.err.println("ERROR: Invalid input. Please type in a numerical value."); } } /** * When this gets called, it sends a message to the interpreter. * The interpreter usually shows it on the command prompt (For Windows users) * or the terminal (For *nix users).(Assuming it's open) */ private void calculate() { if ((userInput % 2) == 0) { JOptionPane.showMessageDialog(null, "Even"); } else { JOptionPane.showMessageDialog(null, "Odd"); } } }
- The import statement imports the JOptionPane class from the javax.swing package.
- The OddEven class declares a single private field of type int named userInput. Every instance of the OddEven class has its own copy of the userInput field. The private declaration means that no other class can access (read or write) the userInput field.
- OddEven() is a public constructor. Constructors have the same name as the enclosing class they are declared in, and unlike a method, have no return type. A constructor is used to initialize an object that is a newly created instance of the class.
- The calculate() method is declared without the static keyword. This means that the method is invoked using a specific instance of the OddEven class. (The reference used to invoke the method is passed as an undeclared parameter of type OddEven named this.) The method tests the expression userInput % 2 == 0 using the if keyword to see if the remainder of dividing the userInput field belonging to the instance of the class by two is zero. If this expression is true, then it prints Even; if this expression is false it prints Odd. (The calculatemethod can be equivalently accessed as this.calculate and the userInput field can be equivalently accessed as this.userInput, which both explicitly use the undeclared thisparameter.)
- OddEven number = new OddEven(); declares a local object reference variable in the main method named number. This variable can hold a reference to an object of type OddEven. The declaration initializes number by first creating an instance of the OddEven class, using the new keyword and the OddEven() constructor, and then assigning this instance to the variable.
- The statement number.showDialog(); calls the calculate method. The instance of OddEven object referenced by the number local variable is used to invoke the method and passed as the undeclared this parameter to the calculate method.
- userInput = Integer.parseInt(JOptionPane.showInputDialog(“Please Enter A Number”)); is a statement that converts the type of String to the primitive data type intby using a utility function in the primitive wrapper class Integer.
Swing Demo:
// Hello.java (Java SE 5) import javax.swing.*; public class Hello extends JFrame { public Hello() { super("hello"); super.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); super.add(new JLabel("Hello, world!")); super.pack(); super.setVisible(true); } public static void main(final String[] args) { new Hello(); } }
Sun has defined and supports four editions of Java targeting different application environments and segmented many of its APIs so that they belong to one of the platforms. The platforms are:
- Java Card for smartcards.
- Java Platform, Micro Edition (Java ME) — targeting environments with limited resources.
- Java Platform, Standard Edition (Java SE) — targeting workstation environments.
- Java Platform, Enterprise Edition (Java EE) — targeting large distributed enterprise or Internet environments.
The classes in the Java APIs are organized into separate groups called packages. Each package contains a set of related interfaces, classes and exceptions. Refer to the separate platforms for a description of the packages available.
The set of APIs is controlled by Sun Microsystems in cooperation with others through the Java Community Process program. Companies or individuals participating in this process can influence the design and development of the APIs. This process has been a subject of controversy.
Sun also provided an edition called PersonalJava that has been superseded by later, standards-based Java ME configuration-profile pairings.