ช่องค้นหา

Custom Search

Iphone

Sunday, August 10, 2008

Java (programming language)

Java is a programming language originally developed by Sun Microsystems 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 has a simpler object model and fewer low-level facilities. Java applications are typically compiled to bytecode that can run on any Java virtual machine (JVM) regardless of computer architecture.
The original and reference implementation Java compilers, virtual machines, and class libraries were developed by Sun from 1995. As of May 2007, in compliance with the specifications of the Java Community Process, Sun made available most of their Java technologies as free software under the GNU General Public License. Others have also developed alternative implementations of these Sun technologies, such as the GNU Compiler for Java and GNU Classpath.

Syntax

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 exclusively as an object oriented language. As a result, almost everything is an object and all code is written inside a class. The exceptions are the intrinsic data types (ordinal and real numbers, boolean values, and characters), which are not classes for performance reasons.

Hello world program
This is a minimal Hello world program in Java with syntax highlighting:


// HelloWorld.java
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, world!");
}
}


A more comprehensive example

// OddEven.java
import javax.swing.JOptionPane;

public class OddEven {
public static void main(String[] args) {
// This is the main method. It gets called when this class is run through a Java interpreter.
OddEven number = new OddEven();
/* This line of code creates a new instance of this class called "number" and
* initializes it, and the next line of code calls the "showDialog()" method,
* which brings up a prompt to ask you for a number
*/
number.showDialog();
}
private int input; // A whole number("int" means integer)
// "input" is the number that the user gives to the computer

public OddEven() {
/* This is the constructor method. It gets called when an object of the OddEven type
* is created.
*/
}

public void showDialog() {
try
/* This makes sure nothing goes wrong. If something does,
* the interpreter skips to "catch" to see what it should do.
*/
{
input = Integer.parseInt(JOptionPane.showInputDialog("Please Enter A Number"));
calculate();
/*
* The code above 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."
*/
}
catch (NumberFormatException e)
/* This means that there was a problem with the format of the number
* (Like if someone were to type in 'Hello world' instead of a number).
*/
{
System.err.println("ERROR: Invalid input. Please type in a numerical value.");
}
}

private void calculate() {
if (input % 2 == 0)
System.out.println("Even");
/* 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 Linux users).(Assuming it's open)
*/
else
System.out.println("Odd");
}
}





No comments: