Section 3.10. Java Standard Classes

   

3.10 Java Standard Classes

In Overflow.java (Listing 3.7.1, above), we made use of a constant declared in the java.lang.Integer standard class. This class makes available a number of methods to work with integers, and it defines three constant variables , including MAX_VALUE (the maximum value for an integer) and MIN_VALUE (the minimum value for an integer). There are corresponding classes defined for long , float , double , boolean , and so forth in the java.lang package. These are all standard classes.

Everything (except primitive type variables) in Java is an object. Objects are defined in terms of the data they make available and the behavior that they are capable of. This is what you do when you write Java programs ”you write classes that serve as blueprints for making objects. When you define your own classes, you are also able to make use of the classes predefined for you in the Java language (such as java.lang.Integer ). When you call on one of these classes, you can refer to the data it defines (such as MAX_VALUE ), and you can perform the actions that it makes available. For example, java.lang.Integer defines a method called toString() . This method takes an integer value and returns it as a String type:

 int x = 777;  System.out.println(x); //prints 777 // still prints 777, but now it's a String: System.out.println(java.lang.Integer.toString(x)); 

Note

When you see a Java "method" referred to, think "function" in Cold-Fusion. For instance, the java.lang.Math class defines a random method, which produces a pseudorandom double whose value is between 0.0 and 1.0. This corresponds to the random() function you've likely used in ColdFusion.


There is a way to shorten these calls to classes defined in the standard packages (or indeed any package). You do so using the import statement. We will look more at import later, but for now, know that because the java.lang package is always available (you don't have to explicitly import it), these two statements are functionally equivalent:

 String firstName = "Jeremy"; // creates a String object  java.lang.String lastName = "Allaire"; // same thing 

That is because a String object is defined in the java.lang package. We will examine objects, classes, packages, methods, and so on in depth throughout the book. For now, it is a good idea to get just this much introduction to them so that the API becomes more accessible, and so that we are free to refer to standard classes in examples. The packages are merely overviewed here, so you can get an idea of where to find things in the API.

3.10.1 java.lang and Object

java.lang and Object define all of the primitive data types ( Boolean , Double , Integer ), String , and Object . Object is at the root of the class hierarchy. That means that all of the objects that you create, or that are defined in Java, inherit the behavior of java.lang.Object .

Anytime you create a string, you are creating an object of type java.lang.String . You can look at the API and see what kinds of things strings allow you to do by default. For instance, the first method defined for strings is charAt() , which returns the character at the given index:

 String name="Java";  charAt(2); // evaluates to v 

Note

In the string "Java," the character at position 2 is "v" because Java generally starts counting at 0, not 1. I say " generally " because this is not the case for database connectivity issues, as we will see.


The Thread and Throwable classes are also defined here.

3.10.2 java.text

This class has some text formatting classes you might use for working with dates and other special textual types.

3.10.3 java.util

This package contains a huge number of important utilities that you will use frequently in programming Java. The collections ( Arrays , LinkedList , HashSet , HashMap , etc.) are all defined here. This also contains classes for working with different calendars, currency, dates, locales, properties, and much more.

Note

Java does not contain structures as defined in ColdFusion. Instead, Java uses a number of specific ways to define name value pairs; these are called collections. This is easy to remember because the attribute of the <cfloop> tag you use when looping over a structure is collection = #collectionName# .


3.10.4 java.io

java.io contains classes that allow you to work with input/output; that is, the reading and writing of files.

Note

When you would write <cffile> in ColdFusion, you implement classes in the java.io package. Instead of using a different value for the action attribute (such as action="read" or action="write" ), use the java.io classes. For instance, to delete a file, you can call the delete() method of the file object.


Note that Java 1.4 defines a new package called java.nio , which stands for New IO. By creating a new package, many of the IO classes could be reworked without compromising backward compatibility.

3.10.5 java.net

This contains defined classes necessary for working with networks, such as sockets and URLs.

3.10.6 java.awt

AWT stands for Abstract Windowing Toolkit, and this package contains numerous classes for creating graphical user interface elements of software programs, such as buttons and fonts. Much of this functionality has ascended into the javax.Swing package, which is currently the preferred way to create software GUIs.

There are a number of packages defined as javax packages. These are known as "standard extensions" (which seems like an oxymoron). These include javax.Swing (for creating graphical user interfaces) and javax.sql , which allows you to connect to and work with databases, as well as javax.naming (which enables work with naming and directory services) and javax.xml .

We will use and refer to many of the classes defined in these packages throughout the book.


   
Top


Java for ColdFusion Developers
Java for ColdFusion Developers
ISBN: 0130461806
EAN: 2147483647
Year: 2005
Pages: 206
Authors: Eben Hewitt

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