Add

10 Useful Classes in the Java API


 

1.Applet

What Java book is complete without some mention of applets? An applet is apiece of code that runs inside a Web browser window. For example, a small currency calculator running in a little rectangle on your Web page can be a piece of code written in Java.



At one time, Java applets were really hot stuff, but nowadays, people are much more interested in using Java for business processing. Anyway, if applets are your thing, then don’t be shy. Check the Applet page of Java’s API documentation.

2.ArrayList

introduces arrays. This is good stuff but, in any programming language, arrays have their limitations. For example, take an array of size 100. If you suddenly need to store a 101st value, then you’re plain out of luck. You can’t change an array’s size without rewriting some code. Inserting a value into an array is another problem. To squeeze “Tim” alphabetically between “Thom” and “Tom”, you may have to make room by moving thousands of

“Tyler”, “Uriah”, and “Victor” names.


But Java has an ArrayList class. An ArrayList is like an array, except that ArrayList objects grow and shrink as needed. You can also insert new values without pain using the ArrayList class’s add method. ArrayList objects are very useful, because they do all kinds of nice things that arrays can’t do.

3.File

Talk about your useful Java classes! The File class does a bunch of things that aren’t included in this book’s examples. Method canRead tells you whether you can read from a file or not. Method canWrite tells you if you can write to a file. Calling method setReadOnly ensures that you can’t accidentally write to a file. Method deleteOnExit erases a file, but not until your program stops running. Method exists checks to see if you have a particular file. Methods isHidden, lastModified, and length give you even more information about a file. You can even create a new directory by calling the mkdir method. Face it, this File class is powerful stuff!



4.Integer

the Integer class and its parseInt method. The Integer class has lots of other features that come in handy when you work with int values. For example, Integer.MAX_VALUE stands for the number 2147483647. That’s the largest value that an int variable can store. The expression Integer.MIN_VALUE stands for the number –2147483648 (the smallest value that an int variable can store). A call to Integer.toBinaryString takes an int and returns its base-2 (binary) representation. And what Integer.toBinaryString does for base 2, Integer.toHexString does for base 16 (hexadecimal).

5.Math

Do you have any numbers to crunch? Do you use your computer to do exotic calculations? If so, try Java’s Math class. (It’s a piece of code, not a place to sit down and listen to lectures about algebra.) The Math class deals with π, e, logarithms, trig functions, square roots, and all those other mathematical things that give most people the creeps.

6.NumberFormat

 About the NumberFormat.getCurrencyInstance method. With this method, you can turn 20.338500000000003 into $20.34. If the United States isn’t your home, or if your company sells products worldwide, you can enhance your currency instance with a Java Locale. For example, with euro = NumberFormat.getCurrencyInstance(Locale.FRANCE), a call to euro.format(3) returns 3,00 € instead of $3.00. The NumberFormat class also has methods for displaying things that aren’t currency amounts. For example, you can display a number with or without commas, with or without leading zeros, and with as many digits beyond the decimal point as you care to include.

7.Scanner

Java’s Scanner class can do more than what it does in this book’s examples. Like the NumberFormat class, the Scanner can handle numbers from various locales. For example, to input 3,5 and have it mean “three and half,” you can type myScanner.useLocale(Locale.FRANCE). You can also tell a Scanner to skip certain input strings or use numeric bases other than 10. All in all, the Scanner class is very versatile.

8.String

Examines Java’s String class. The chapter describes (in gory detail) a method named equals. The String class has many other useful methods. For example, with the length method, you find the number of characters in a string. With replaceAll, you can easily change the phrase “my fault” to “your fault” wherever “my fault” appears inside a string. And with compareTo, you can sort strings alphabetically

9.StringTokenizer

I often need to chop strings into pieces. For example, I have a fullName variable that stores my narcissistic “Barry A. Burd” string. From this fullName value, I need to create firstName, middleInitial, and lastName values. I have one big string (“Barry A. Burd”), and I need three little strings — “Barry”, “A.”, and “Burd”. Fortunately, the StringTokenizer class does this kind of grunt work. Using this class, you can separate “Barry A. Burd” or “Barry,A.,Burd” or even “Barry<tab>A.<tab>Burd” into pieces. You can also treat each separator as valuable data, or you can ignore each separator as if it were trash. To do lots of interesting processing using strings, check out Java’s StringTokenizer class.

10.System

You’re probably familiar with System.in and System.out. But what about System.getProperty? The getProperty method reveals all kinds of information about your computer. Some of the information you can find includes your operating system name, you processor’s architecture, your Java Virtual Machine version, your classpath, your username, and whether your system uses a backslash or a forward slash to separate folder names from one another. Sure, you may already know all this stuff. But does your Java code need to discover it on the fly

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.