Wednesday, March 12, 2008

Facts about Java

Since Tivoli is writing more and more applications in Java, I thought I would put together some tips to help Java novices understand some of concepts used in Java programming.

What is Java?

Technically speaking, Java is a high-level, object-oriented programming language. To write a Java program, you need to create a file with a name like MyFirstProgram.java. The contents of that file would need to have the following lines at a minimum:public class MyFirstProgram { static public main() { System.out.println("Hello World"); }}You would then need to compile the program, which requires the Java Software Development Kit (aka JDK or SDK). Most Tivoli installs only include the Java Runtime Environment (JRE), which does NOT include the javac Java compiler. But once you download the JDK , you can compile your program with a command similar to:C:\jdk1.5_06\bin\javac MyFirstProgram.javaIf all went well, the above command will produce no output (no news is good news in this case). You then need to run your program, which could be done with the command:C:\jdk1.5_06\bin\java -cp . MyFirstProgramAnd this would produce the output:
Hello World

OK, I can recognize some fairly familiar syntax in that program, but how did you know about "System.out.println"?

Well, when learning Java, you must first learn the syntax of the language (where the curly braces go, how to define variables, etc.), AND you've got to get familiar with some number of the objects provided by Java and the methods they support. This information is available in the SDK documentation, available from Sun at:http://java.sun.com/j2se/1.5.0/docs/ - for 1.5http://java.sun.com/j2se/1.4.2/docs/ - for 1.4.2Click on the link titled "Java 2 Platform API Specification" for information about all of the classes (i.e. types of bjects) available to you as a programmer. If you look in the lower left frame and scroll down, you will see a class named "System". You'll see that class cannot be instantiated, but you can access it directly using the class name, "System", or "java.lang.System" if you want to be complete about it. You'll see that it has a "field", i.e. attribute, named "out", which is an object of type "PrintStream" that represents standard output. If you click on the datatype of that attribute ("PrintStream"), you'll see all of the methods supported by that object. One of those methods is named "println" and accepts a single parameter of type String. If you click on that method, you will get a description of it.So as a Java programmer, you need to get familiar with *some number* of the classes provided within the Application Programming Interface (API) so that you will more quickly be able to figure out which objects you need to use to accomplish a particular task.For example, if you want to create a Java application that connects to an HTTP server, you would create an instance of the java.net.URL class and invoke the getContent() method on that object. How did I know that? By reading through the SDK API documentation. There are probably other ways to accomplish this same task, but this is the one I came up with by simply reading through the API trying to find an object that works the way I need it to work.

What's the deal with Java, Java2, Java2SE, J2EE, etc.?

Java is the name of the language, which, at its core, hasn't really changed appreciably since its introduction. The name Java2 was introduced with Java 1.2 (the last version before Java2 was 1.1.8), and it basically refers to any Java runtime version greater than 1.2. The versions and names get really weird at this point, however. For example, the package whose ZIP file is named jdk1.5_06.ZIP is known as "Java 2 Platform Standard Edition 5.0" (release 6). So version 1.5 is advertised as "5.0". Yep, confusing as anything, so here's my tip:If you're discussing features in different versions, use the actual version number that is returned when you run the command:
java -version
That'll keep you safe.Java2EE stands for "Enterprise Edition", and is basically a specification for the services and capabilities that must be provided by any application server (like WAS, Tomcat, JBoss, etc.) that will be labelled "J2EE Compliant". From a practical standpoint, this means that you would never be writing a J2EE standalone application - for that you would use J2SE (standard edition). Any J2EE application you write will be running inside an application server.

What do I need to know about JSPs, Servlets, EJBs, etc.?

Well, those are all part of the J2EE specification. So if you're writing a standalone app (like the MyFirstProgram above, or like the TEP desktop client), you don't have to worry about them at all!However, if your application is a web-based application that will be running inside WAS or another app server, you're interested in them. Specifically:JSP stands for Java Server Pages, and a .JSP file contains both HTML and Java code, where the Java code is compiled and interpreted by the app server (WAS). This basically allows a Java developer to easily insert Java code into web pages to perform operations (like accessing a backend database, etc.).A Servlet is a Java class that implements the HttpServlet interface, meaning that it implements the doGet() and doPost() methods to return HTML to the browser. The main difference between JSP and a servlet is that the developer never has to directly compile the JSP - the app server does that on her behalf - whereas a servlet is written as a .JAVA file and must be compiled by the developer.JSPs and servlets are both short-lived, in that they are accessed/invoked when a user access that resource, but then they go away.An Enterprise Java Bean is a Java class that will be instantiated by the application server and in general remain in memory for a "long" time. An EJB performs several functions, such as persistence and access control, among others.

Aren't you tired of typing all this in?

Yes I am, so I'll leave it here for now. There are resources all over the web, but one that I really like is http://en.wikipedia.org. While their information on individuals may get tainted from time to time, their technical information seems to be spot-on :)

No comments: