|
|
|
See how a simple Java program works in a line-by-line breakdown, along with gentle introductions to some more complex concepts.
This article goes right into the working meaning of code and expands to discuss more complex concepts common in Java programming. Learn some background information about Sun Microsystem's Java language to gain a better understanding before reading this tutorial. Introduction to Java Programming discusses some preliminary topics like classes, objects, methods, among others. The Simple ProgramThe example program code follows and should run without error when typed into an Integrated Development Environment. Afterwards a line-by-line explanation gives the definition and purpose of each phrase. class helloWorld{ public static void main(String args[ ]){ System.out.println(“Hello World!”); } } The class definitionThe first line is a declaration statement to introduce the code to the computer before it is used:
Note that this article does not list Java's coding conventions, but a good programmer learns Java Coding Conventions to save time and mistakes. The main methodThe second line starts the 'main method', where the programmer outlines every step of what they want to accomplish. Every Java application program has any number of unique methods, but must also contain at least one and only one main method. Java applets, however, do not contain main methods.
The String arrayThe third line declares an array, an element that stores a list of values like x[1,2,3]. Contrast arrays with variables that store only one value at a time, like x=3. String defines the type of values received: numbers, characters, and symbols. Args is the unique name for this array, and the brackets signal its array status. The System class & outputThe Java Development Kit has many preinstalled packages that consist of classes for common operations; the last line of code is a package reference or link to such a package. System is a class in Java's default package and handles system hardware. Out is an object of that class and handles output. Println() is a method of System and accepts Hello, world! as a parameter to output to the screen. Interpreters normally ignore spaces in stored values, so quotation marks declare this a string parameter to preserve such characters. SummaryThe first three lines of this program consist of preliminary definitions to set up the final package reference. The program must name itself and define how, where, and what it will do before it can begin processing. Because this is a tedious process, Java has many prewritten classes or packages that can simply be referenced in a new program.
The copyright of the article The Basic Java Program in Javascript/Java Programming is owned by Ana Mills. Permission to republish The Basic Java Program in print or online must be granted by the author in writing.
|
|
|
|
|
|
|
|