03 Java API Documentation
03 Java API Documentation
Lecture 3
Java API Documentation
CCS2304
Using Classes from the Java Library
• You will frequently use the classes in the Java library to
develop programs.
2
Package
Built-in Packages
• A package is a collection of related classes. User-defined Packages
3
Using Classes from the Java Library
4
Using Classes from the Java Library
5
Using Classes from the Java Library
6
Using Classes from the Java Library
7
Using Classes from the Java Library
8
Java API Packages (a subset)
java.lang
The Java Language Package contains classes and interfaces that are required by many
Java programs. This package is imported by the compiler into all programs.
java.util
The Java Utilities Package contains utility classes and interfaces, such as date and time
manipulations, random-number processing capabilities with class Random, Data
structures
javax.swing
The Java Swing GUI Components Package contains classes and interfaces for Java’s
Swing GUI components that provide support for portable GUIs. (still functional but being
replaced by javafx)
java.net
The Java Networking Package contains classes that enable programs to communicate via
networks .
java.sql
Provides the API for accessing and processing data stored in a data source (usually a
relational database) using the JavaTM programming language..
9
Import Statements
• A Class full name is its package name.class name.
• import statements specify the packages of the classes used in
your Java programs.
Without import
javax.swing.JApplet x = …
With import
import javax.swing.JApplet;
…
JApplet x =
10
class java.lang.Math
import java.lang.Math;
11
class javax.swing.JOptionPane
Package: javax.swing
Class JOptionPane
public static void showMessageDialog(Component parentComponent, Object message)
12
class javax.swing.JOptionPane
import javax.swing.JOptionPane;
13
class java.util.Random
Constructor
Random()
Methods
import java.util.Random;
15
class java.util.Scanner
import java.util. Scanner;
class ScannerDemo {
public static void main (String [] args) {
Scanner scan = new Scanner(System.in);
16
class java.lang.String
• The java.lang.String class encapsulate a sequence of
characters as a string.
• Constructing a String:
– String message = "Welcome to Java“;
– String message = new String("Welcome to Java“);
17
Constructing Strings
• String newString = new String(stringLiteral);
18
Strings are Immutable
• A String object is immutable; its contents cannot be
changed.
Displayed: Displayed:
hello HELLO 19
Interned Strings
• Strings are immutable and are frequently used, to
improve efficiency and save memory.
• The JVM uses the same instance for string literals with
the same character sequence.
20
Examples
String s1 = “Welcome to Java”;
String s2 = new String(“Welcome to Java”);
String s3 = “Welcome to Java”;
Displayed:
s1 == s2 is false
s1 == s3 is true
21
String Comparisons
Returns true if this string is equal to string s1.
24
Getting String Length and Characters, and
Combining Strings
Returns the number of characters in
this string.
25
Finding a Character or a Substring in a String
26
Formatting Strings
Returns a formatted string using the specified format string
and arguments.
• The String class contains the static format method to create a
formatted string.
• The syntax to invoke this method is:
String.format(format, item1, item2, ..., itemk)
• This method is similar to the printf method except that the
format method returns a formatted string, whereas the printf
method displays a formatted string.
Ex:
String s = String.format("%7.2f%6d%-4s", 45.556, 14, "AB");
System.out.println(s);
Displays:
2 6 4
7
27
StringBuilder and StringBuffer
• The StringBuilder/StringBuffer class is an alternative to
the String class.
28
StringBuilder Constructors
java.lang.StringBuilder
+ StringBuilder()
+ StringBuilder(capacity: int)
+ StringBuilder(s: String)
29
Modifying Strings in the Builder
30
Examples
StringBuilder strBuilder = new StringBuilder(“Welcome to Java”);
W e l c o m e t o J a v a
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
31
Wrapper Class
public void m(Object obj) {
}
32
The Integer and Double Classes
33
Methods for Parsing Strings into Numbers
• The parseInt method in the Integer class parse a numeric
string into an int value and the parseDouble method in the
Double class to parse a numeric string into a double value.
34
Automatic Conversion Between Primitive
Types and Wrapper Class Types
• Converting a primitive value to a wrapper object is called
boxing. The reverse conversion is called unboxing. Java allows
primitive types and wrapper classes to be converted
automatically.
Auto-boxing
Auto-unboxing
35
Any Questions?
36
Why Strings Are Immutable
37
StringBuilder and StringBuffer
38
Wrapper Class
There are certain needs for using the Wrapper class in Java as
mentioned below:
1. They convert primitive data types into objects. Objects are needed if we
wish to modify the arguments passed into a method (because primitive
types are passed by value).
2. The classes in java.util package handles only objects and hence wrapper
classes help in this case also.
40