THE JAVA API

advertisement
THE JAVA API
The Java API – which stands for Application Programming Interface – is a repository of
prewritten classes provided by the creators of Java to enhance its usefulness. There are classes
for creating windows with menus and buttons, for drawing two-dimensional figures, for
downloading pages from the Internet – all kinds of stuff. Classes within the API are organized
into libraries called packages, each of which has a like theme.
Some of the Java API Packages
Package
java.lang
java.util
java.io
Theme
Classes that are considered
essential to every Java program
Some Classes Within It
Math
Object
String
System
Calendar
Date
Random
Scanner
File
PrintStream
Classes that provide helpful utilities
Class that perform system input
and output
The classes in the API are already compiled and available to your Java programs. To use any
class other than those in java.lang, specify its full name – package name . class identifier.
This allows the compiler to resolve the external reference by finding the class file in the Java
library.
Example
The Scanner class is in package java.util. The following statement correctly references it.
java.util.Scanner input = new java.util.Scanner( System.in );
The import Statement
You can inform the Java compiler that you are using a class from a package by placing an
import statement at the top of your Java file. The syntax is:
import package name . class identifier ;
Using this import statement allows you to refer to the class by its shorter class identifier.
The Java API
Page 1
Example
1
2
3
4
5
6
7
8
9
10
import java.util.Scanner;
public class MyApp
{
public static void main( String [] args )
{
Scanner input = new Scanner( System.in );
. . .
}
}
You can also use an asterisk (*) as a wildcard in the import statement to tell the compiler to
“import the whole package.” The syntax is:
import package name . * ;
Example
1
2
3
4
5
6
7
8
9
10
import java.util.*;
public class MyApp
{
public static void main( String [] args )
{
Scanner input = new Scanner( System.in );
. . .
}
}
Using the wildcard to “import the whole package” does not reduce the run-time efficiency of
your code because the import statement creates no executable operations and it doesn’t add
unnecessary machine code to your program. It is a compiler declaration that simply allows the
compiler to refine its search pattern when resolving the external references. The only machine
code added to your program is that for the classes that your program actually references.
The Java API
Page 2
The java.lang Package
The classes in package java.lang are considered so essential that you can use their shorter
class identifiers without an import statement.
Example
The Java statement:
java.lang.String name = new java.lang.String( "JAVA" );
Can be replaced by the following without having to import java.lang.String:
String name = new String( "JAVA" );
The import static Statement
Class variables and methods can be imported so that you can use their shorter class identifiers.
Use Java’s import static statement. The syntax is:
import static package name . class identifier . * ;
Example
Java’s String class contains this static method:
static String valueOf( double d )
// Returns the string representation of the double argument.
Without an import static statement, this method is called using:
String.valueOf( d )
Or, you can import the static items in String and dispense with the qualifier:
import static java.lang.String.*;
. . .
valueOf( d )
The Java API
Page 3
Exercises
For each application below, circle what's wrong and explain. Two of them are correct.
1.
import Scanner;
import Math;
import System;
public class MyApp
{
public static void main ( String [] args )
{
Scanner in = new Scanner( System.in );
java.lang.System.out.println( "Enter radius: " );
. . .
area = Math.PI * Math.pow( radius, 2 );
. . .
}
}
2.
public class MyApp
{
public static void main ( String [] args )
{
Scanner in = new Scanner( System.in );
java.lang.System.out.println( "Enter radius: " );
. . .
area = Math.PI * Math.pow( radius, 2 );
. . .
}
}
The Java API
Page 4
3.
public class MyApp
{
public static void main ( String [] args )
{
java.util.Scanner in = new java.util.Scanner( System.in );
System.out.println( "Enter radius: " );
. . .
area = Math.PI * Math.pow( radius, 2 );
. . .
}
}
4.
import java.util.Scanner;
import java.lang.Math;
import java.lang.System;
public class MyApp
{
public static void main ( String [] args )
{
Scanner in = new Scanner( in );
out.println( "Enter radius: " );
. . .
area = PI * pow( radius, 2 );
. . .
}
}
The Java API
Page 5
5.
import java.util.Scanner;
import static java.lang.Math;
import static java.lang.System;
public class MyApp
{
public static void main ( String [] args )
{
Scanner in = new Scanner( in );
out.println( "Enter radius: " );
. . .
area = PI * pow( radius, 2 );
. . .
}
}
6.
import java.util.Scanner;
import static java.lang.Math.*;
import static java.lang.System.*;
public class MyApp
{
public static void main ( String [] args )
{
Scanner in = new Scanner( in );
out.println( "Enter radius: " );
. . .
area = PI * pow( radius, 2 );
. . .
}
}
The Java API
Page 6
7.
import java.util.Scanner;
import static java.lang.Math.*;
import static java.lang.System.*;
public class MyApp
{
public static void main ( String [] args )
{
Scanner input = new Scanner( in );
out.println( "Enter radius: " );
. . .
area = PI * pow( radius, 2 );
. . .
}
}
The Java API
Page 7
Download