google search

Custom Search

Friday, August 8, 2008

Syntax: Hello world program

Main article: Java 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.

[edit] 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!");
}
}

To execute a Java program, the code is saved as a file named HelloWorld.java. It must first be compiled into bytecode using a Java compiler, which produces a file named HelloWorld.class. This class is then launched.

The above example merits a bit of explanation.

* All executable statements in Java are written inside a class, including stand-alone programs.
* Source files are by convention named the same as the class they contain, appending the mandatory suffix .java. A class that is declared public is required to follow this convention. (In this case, the class HelloWorld is public, therefore the source must be stored in a file called HelloWorld.java).
* The compiler will generate a class file for each class defined in the source file. The name of the class file is the name of the class, with .class appended. For class file generation, anonymous classes are treated as if their name was the concatenation of the name of their enclosing class, a $, and an integer.
* The keyword public denotes that a method can be called from code in other classes, or that a class may be used by classes outside the class hierarchy.
* The keyword static indicates that the method is a static method, associated with the class rather than object instances.
* The keyword void indicates that the main method does not return any value to the caller.
* The method name "main" is not a keyword in the Java language. It is simply the name of the method the Java launcher calls to pass control to the program. Java classes that run in managed environments such as applets and Enterprise Java Beans do not use or need a main() method.
* The main method must accept an array of String objects. By convention, it is referenced as args although any other legal identifier name can be used. Since Java 5, the main method can also use variable arguments, in the form of public static void main(String... args), allowing the main method to be invoked with an arbitrary number of String arguments. The effect of this alternate declaration is semantically identical (the args parameter is still an array of String objects), but allows an alternate syntax for creating and passing the array.
* The Java launcher launches Java by loading a given class (specified on the command line) and starting its public static void main(String[]) method. Stand-alone programs must declare this method explicitly. The String[] args parameter is an array of String objects containing any arguments passed to the class. The parameters to main are often passed by means of a command line.
* The printing facility is part of the Java standard library: The System class defines a public static field called out. The out object is an instance of the PrintStream class and provides the method println(String) for displaying data to the screen while creating a new line (standard out).

0 comments:

 

blogger templates | Make Money Online