Приглашаем посетить
Литература (lit-info.ru)

3.1 Beginning Java programming

Table of Contents

Previous Next

3.1 Beginning Java programming

3.1.1 An introduction to Java and J2SDK

Java was the first object-oriented programming language created for the Internet. It adds dynamic features to numerous, otherwise static Web pages. Since its arrival on the Internet scene in 1996, it has been a powerful programming tool for Web programmers and developers. Countless Web applications have been developed using Java, particularly in the graphics and Internet game areas. Today, even though we have more new technologies and choices on the Web, Java still remains a strong competitor in presentation (or business) graphics and multimedia for the Web community. For example, Java is still one of the best languages that provide powerful graphics libraries to create all-singing, all-dancing 2D graphics on the Web. This chapter is not an introductory text on the Java language and features. Instead, we will concentrate on how to use it to generate presentation graphics and distribute them on Web pages. With Java, incorporating graphics into XHTML documents is no longer a difficult challenge.

Java started as a language called Oak in the early 1990s. It was designed by Sun Microsystems and originally intended to be used to develop programmable applications for smart consumer electronics products. This had little success. The language was then rebranded as Java and officially released in early 1996 to the Internet community. Since then Java has taken off and enjoyed immense popularity in the area of Web programming. One reason was due to its capability to run on Web pages. Browsers can download a Java program (called a Java applet) included on a Web page and run it locally. Java applets can be used to create animations, graphics, games, and a wide range of exciting special effects.

Java is a compiled and interpreted language. When a Java source is compiled, it produces an intermediate code called bytecode. Bytecode instructions would allow any machine equipped with a bytecode interpreter to run the code. This feature is platform or operating system independent and sometimes is referred to "Java Run-Time Environment" (JRE) or "Java Virtual Machine" (JVM). The idea of bytecode is also used to develop the Wireless Markup Language (WML) for devices such as mobile phones. The WML and Mobile Internet will be discussed later in Chapter 21.

If you want to use Java, you need first to install a Java software development environment on your computer. A minimum Java development environment includes a "Java 2 Software Development Kit" (J2SDK) with a JRE or JVM. You need the JVM to run Java applets and applications embedded into your Web pages. The J2SDK is mainly used to develop Java programs. The J2SDK and JRE used in this chapter are j2sdk-1_3_1_02 and j2re-1_4_0_01.

If you are using IE5.x and NS4.x you already have JVM installed on your machine. For Microsoft XP, IE6.+, and NS6.+, however, JRE or JVM is no longer an integral part of the package. You may download these packages free from Sun's Java Web site

http://www.javasoft.com

The site is regularly updated and is the primary source for Java technology containing tutorials, documentations, and a vast collection of Java libraries.

The J2SDK environment is basically everything you need to build Java applications and use them on your Web pages. After installation, you will have several directories (or folders) containing a large number of programs on your machine. Two of them are particularly important and will be used frequently in this chapter. They are:

javac To activate the Java compiler.

java To activate the Java interpretor.

The first program is used to compile Java source code (or program) into bytecode. The second one is to interpret the bytecode and run it on your machine. Both of them can be found in the bin directory of your J2SDK installation. One of the best ways to understand javac and java is to use them to develop a simple application. Also, if the Java program HelloWorld.java in section 3.1.2 runs successfully on your machine, it is likely that you have a proper J2SDK installation.

3.1.2 My first Java program

Once you have J2SDK installed on your machine, you can start programming Java and build Java applications on the Web. Since the J2SDK development environment has no GUI, the package and related tools are all run from the console window such as the DOS command line. This means that you start your Java tool by typing its name, file names, or any other option arguments into the console window. Let's look at the classic "Hello World" Java application program. With a plain text editor such as Notepad, you type the code as shown in the following example HelloWorld.java:



Example: HelloWorld.java - My First Java Program

1: class HelloWorld
2: {
3:  public static void main(String args[])
4:  {
5:  System.out.println("Hello, World!");
6:  }
7: }

This is a simple Java program to print a simple text to screen. Line 1 defines a class with the identity HelloWorld. Line 3 sets the status of the object to be static (i.e., unchanged), and accessible by any classes without limitation (i.e., public). The println()function or method in line 5 is then used to print a text string to the screen.

As a standard Java convention, you save your Java program file with the .java extension. For instance, the file in this example is called HelloWorld.java. Without this extension, the Java compiler may not be able to recognize your program. You also need to make sure that your program and the Java compiler are in the same directory, or a correct path is specified. To compile this Java program, you can use the javac command such as



javac HelloWorld.java

Note that the Java language is case sensitive. You need to make sure that any commands and file names are matched exactly, or they will not be recognized. If the program compiles successfully, it will create a Java bytecode file called HelloWorld.class. This file name needs to be matched. The bytecode can be executed by the interpreter java. Type the command



java HelloWorld

and you should see the text string "Hello World" printed to your screen. A screen shot of this example is shown in Fig. 3.1.

Figure 3.1. "HelloWorld"

graphics/03fig01.gif


Due to the practical nature of this book, we will not discuss the full language details of Java here. For example, we will not discuss the full aspects of object-oriented features of Java or provide the full definitions of function, method, and class. In fact, we consider functions are basically the same as methods in this chapter. In order to have a good starting point, some basic language features such as loops and conditionals are demonstrated in the next section.

3.1.3 A Java program with basic language features

Let's look at a slightly more complex Java example. The following program, AddingNumbers.java, can read a set of integers and print out the total sum.



Example: AddingNumbers.java - A Java Program To Add Numbers

 1: class AddingNumbers
 2: {
 3:   public static void main(String[] args)
 4:   {
 5:     int sum=0, size;
 6:     int[] intNumbers;
 7:
 8:     size = args.length;
 9:     intNumbers = new int[size];
10:
11:     if (size>0)
12:     {
13:       sum=0;
14:       for (int i=0; i<size; i++)
15:       {
16:         intNumbers[i]=Integer.parseInt(args[i]);
17:         sum = sum + intNumbers[i];
18:       }
19:     }
20:     System.out.println("The sum of " + size + " numbers is : " + sum);
21:   }
22: }

This program demonstrates the following language features of Java in a simple way:

  • the use of variables (line 5);

  • the use of arrays (lines 3 and 6);

  • the if (conditional) statement in Java (see lines 1119);

  • the for-loop (see lines 1418).

This program will accept a series of command arguments as input and store them into a string array called args (see line 3). For example, after you have compiled the program, the program can be activated by the Java interpreter as



java AddingNumbers 3 4 6

The input numbers 3, 4, and 6 will be considered as a string and stored in the element args[0], args[1], and args[2] respectively. The length of this array is args.length, which has the value of 3 and is assigned to the variable size in line 8. This variable is an integer (i.e., int size) and is used to specify the dimension of an array called intNumbers in line 9.

The conditional statement in line 11 states that if the size of the input argument is bigger than zero (i.e., size > 0 ), the process in lines 1119 is activated to calculate the sum. The actual calculation is carried out by the for-loop (see lines 1418). Since the input numbers are considered as strings, the parseInt() function in line 16 is used to convert the ith string args[i] into an integer. For example, the statement



intNumber[0]=Integer.parseInt(args[0]);

will convert the first input argument args[0], which has the text "3," into an integer and store it in the integer array intNumber[0].

The for-loop in lines 1418 is simply adding together all values in the array intNumbers[]. The final result (i.e., sum) is output to the screen by the statement in line 20. A screen shot of this example is shown in Fig. 3.2.

Figure 3.2. Adding numbers

graphics/03fig02.gif


Again, it is not feasible to show you all the language features of Java in one chapter. This program shows the language features of Java that we are going to use in the remaining sections.

Now that you have some idea about Java programming it's time to consider how to use Java on Web pages.

    Table of Contents

    Previous Next