Writing Java Programs

You're probably already familiar with Java, if only because of Java applets. Applets, windowed Java applications designed to work in browsers, took the world by storm when first introduced, and all major browsers support Java these days. You can find millions of applets on the Internet, and whole banks of them you can pick up for free. There are even applets out there that work with XML.

A Java applet takes up a predefined area in a browser and can display graphics, controls such as buttons and text fields, text, and more. It's interactive because it runs in your browser. As mentioned, applets took the Internet by storm when they were first introduced. However, they're on the wane now, largely because of other solutions that are easier to program, such as Dynamic HTML, or more powerful, such as Macromedia Flash.

Don't worry about Java, though; as applets have become less popular (although they're still very popular), Java applications have gathered strength. The main reason that Java applications have become so powerful is that they're nearly as powerful as C++, but they're also cross-platform: You can use the same application in Windows or UNIX, for example. Many large corporations have switched from using C++ internally to using Java for most programming. (Another option is to use Java Servlets or JavaServer Pages, JSP, on the server to handle XMLI'll take a look at these options in Chapter 20, "WML, ASP, JSP, Servlets, and Perl.")

A Java application does not run in a browser like an appletit's a free-standing program. Java applications can themselves create windows, like applets can, and we'll see how to do that here. In fact, Java applications can act as browsers, and we'll see an example of that in the next chapter with a Java application that reads an XML document from the Internet and uses it to display graphics. In that case, the XML document specifies circles to draw, and we'll be creating a graphical, not text-based, browser that is typical of the kinds of things you can do when you create your own XML applications.

Our XML Java work will center on writing Java applications, not applets. (For security reasons, applets are very restricted in terms of what they can dothey can't handle most types of file access, and we don't want to restrict our XML programs to work only in browsers.) So how do you create a Java application? You write applications as Java code and then compile them with the Java Software Development Kit (the Java SDKbefore Java 2, the SDK was called the Java Development Kit [JDK], and some people and some Web pages at Sun still call it that). The compiled application is ready to run, and we'll see how to do that here.

I'll make this more concrete with an example. Here's how to create an application named ch10_01 , which I'll store in a file named ch10_01.java (I'll go through the details of this application in this chapter):

Listing ch10_01.java
 public class ch10_01 {     public static void main(String[] args)     {         System.out.println("Welcome to Java");     } } 

I can use the Java compiler, which is named javac, to compile this file into a bytecode file named ch10_01.class, and ch10_01.class is what you actually run. The bytecodes in the ch10_01.class are what Java reads and executes. (Java bytecodes are very compact compared to text, which makes applets fast to download. You can run the same bytecode file on many different operating systems, which makes it cross-platform.) Here's how you use javac to compile ch10_01.java (I'm using % as a generic command-line prompt, following the UNIX usage, where the prompt often is % ; on an operating platform like Windows, this prompt will be something like C:\XML> ):

 %javac ch10_01.java 

This creates ch10_01.class, and you use that file when running that application. To run the application, you use the tool named java (which comes with the Java SDK) like this:

 %javac ch10_01.java  %java ch10_01  Welcome to Java 

As you can see, the java tool executes the bytecode file ch10_01.class, and the resultthe text Welcome to Java appears. As mentioned, I'm using % as a generic command-line prompt because Java is available on many platforms. In Windows, you use these tools in an MS DOS window like this:

 C:\>java ch10_01  Welcome to Java 

That's what running a Java application looks like. As we'll see, there are many similarities between Java and JavaScriptbut there are also significant differences. For example, we'll need to indicate the type of variables in Java, which you don't have to do in JavaScript. Java is also a lot more object-oriented than JavaScript.



Real World XML
Real World XML (2nd Edition)
ISBN: 0735712867
EAN: 2147483647
Year: 2005
Pages: 440
Authors: Steve Holzner

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net