Simple Java Application

To give you an early feel for the language, the multiply. Java example shows a Java application that outputs the product of two integers supplied as parameters.

Multiply. Java

public class Multiply
{
public static void main(String[] args)
{
String resultString;
int arg1;
int arg2;
int result;
arg1= Integer.parseInt(args[0]);
arg2= Integer.parseInt(args[1]);
result=arg1 * arg2; resultString = Integer.toString(result);
System.out.println(''The product of " + args[0] + " and " + args[1] + " is " + resultString);
}
}

The program consists of a class, multiply, in a source file, multiply.Java. We will cover classes in detail in below. At this stage, it is sufficient to note that every program must contain one public class. Public is an access modifier, which specifies that other programs can access our class. We discuss access modifiers in later. We can determine the class from the declaration (line 1)

public class Multiply
{

The source file name must be the same as the class name. The source file suffix must be .java. If these two conditions are not met, the program will not compile. The program is compiled using the javac compiler, for example,

> javac Multiply.java

Where > is the command prompt. We use > to indicate a command prompt in general. This could be a > on Windows or a % on UNIX. If compilation is successful, the compiler will produce a byte code file multiply.Class. All compiled byte code files have the suffix .class.

With many programming languages, compilation produces machine code. Each platform will have its own machine-specific code, so a program compiled on one platform will need to be recompiled on another before it can be run. Java bytecode is an intermediate code between source code and machine code. The byte code can be run by any Java interpreter that conforms to the Java Virtual Machine (JVM) specification. A JVM can be a standalone interpreter or embedded in a browser or electronic device. So having produced our byte code on one platform, we can run it on any other platform that supports a JVM.

To run the application, we can use the java interpreter as follows:

> java Multiply 7 12


The product of 7 and 12 is 84

Note that we do not add the .class suffix when specifying the program name. Following the program name are optional parameters separated by one or more spaces.

At this stage, we do not expect you to have a detailed understanding of the code. The starting brace, {, in line 1 denotes that following statements are part of the multiply class. Line 16 consists of a closing brace,}, which denotes the end of the class. We use these braces not just to delimit classes but also, for example, to delimit blocks of code that follow an if or else statement. Because the program is a standalone application, it must contain the declaration (line 3)

public static void main(String[] args)
{


We will describe the keywords public, static, and void in later chapters. At this point, you should just note that they must precede main in the declaration line. A method is roughly equivalent to a procedure or function in a nonobject-oriented language. Every Java application must have a main method.

Note that Java is case sensitive; using PUBLIC instead of public, for example, will be rejected by the compiler. The main method has as a parameter an array of String objects named args. The declarations are

String resultString;
int arg1;
int arg2;

int result;

declare variables of type String and int. Note that a semicolon is used as a terminator, so statements can span more than one line.

The statement in line 9 takes the first supplied parameter, the first element in the args array identified by args[0], and converts it to the int type variable arg1. This is done using the parseInt method of the supplied Java language class Integer. The syntax for calling static methods, such as parseInt, is classname.methodname or Integer.parseInt in our case. We will learn about static methods in below. Line 11 multiplies the two input parameters, and line 12 converts the result to a String variable, resultString, using the supplied Integer.toString method.

In lines 13–14, System.out.println prints a line to the standard output stream, and then terminates the line. System.out is an object in the java.lang.System class, which is of type java.io.PrintStream. In turn, println is a method with in the PrintStream class, which takes a String as a parameter. This format of objectname.methodname(parameters) for invoking a method, which is not static, is standard Java syntax.

There are two main types of development environments in Java. The first is the Software Development Kit (SDK), which can be downloaded for free from Sun's Web site, The site contains installation instructions for Windows, Solaris, and Linux environments, as well as a link to start the download itself. The SDK contains the javac compiler and java interpreter, various Java libraries, and tools. Once Java has been installed, programs are typically developed using a text editor and compiled and run from the command line as we have shown. In particular, there is a link to the Java 2 Platform API (application programming interface) Specification. This provides documentation about all the Java- supplied classes and methods. The API documentation can be viewed online or downloaded.

The second kind of development environment is an IDE (integrated development environment) available from many sources such as Borland's JBuilder, Oracle's JDeveloper, and Sun's Forte. Some of these are free for personal use in a nonproduction environment. These IDEs are window-driven environments and have all the SDK features as well as features such as default code generation, advanced debugging, and code coaches. Compiling or running a Java program can be done by clicking on a menu or tool bar in the IDE. A typical development feature is to bring up a list of methods in a popup window once a class or object has been typed, thereby doing away with the need to memorize the large number of methods provided by the Java libraries. Although IDEs are fine tools for experienced Java developers, they have a large number of features that have to be assimilated. If you are new to both Java and IDEs, you will find yourself at first spending as much time learning about the IDE as Java itself.



> javac MyClass.java

> java MyClass




to indicate compiling or running Java programs, whether from the command line SDK or a Windows-driven IDE. To distinguish user input from any output, we will use the convention of highlighting user input in bold.

No comments:

Post a Comment