12 introduction to java api

20
12 Introduction to Java API

Upload: zorina

Post on 12-Feb-2016

50 views

Category:

Documents


0 download

DESCRIPTION

12 Introduction to Java API. Contents. Java API Specifications java.lang package Object class Class class System class String and StringBuffer classes Math class java.util package. Objectives. Navigate the Java API Specifications Describe the java.lang package - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: 12  Introduction to Java API

12 Introduction to Java API

Page 2: 12  Introduction to Java API

2

Contents

• Java API Specifications• java.lang package• Object class• Class class• System class• String and StringBuffer classes• Math class• java.util package

Page 3: 12  Introduction to Java API

3

Objectives

• Navigate the Java API Specifications• Describe the java.lang package• Explore fundamental classes in java.lang package:

• Object• Class• System• String• StringBuffer• Math

• Describe the java.util package

Page 4: 12  Introduction to Java API

4

Java API Specifications

List of Packages

List of Classes

Package / Class Description

http://java.sun.com/j2se/1.5.0/docs/api/

Page 5: 12  Introduction to Java API

5

java.lang package• java.lang provides classes that are fundamental to the

design of the Java programming language. • Object class, the root of the class hierarchy.• Class class, represents classes at run time.• Wrapper classes represent primitive types as objects.• Math class provides mathematical functions. • String and StringBuffer classes provide operations on

strings.• System classes provide system operations. • Throwable class represents errors and exceptions.

• java.lang is implicitly imported in every Java source file.

Page 6: 12  Introduction to Java API

6

Object classDeclaration: public class Object • Class Object is the root of the class hierarchy. Every class has

Object as a superclass. All objects inherit the methods of this class.

Method Summaryprotected  Object clone()

 boolean equals(Object obj) protected  void finalize()

 Class<? extends Object> getClass()

 int hashCode()

 void notify()  void notifyAll()

 String toString()

 void wait()

 void wait(long timeout)

 void wait(long timeout, int nanos)

Page 7: 12  Introduction to Java API

7

Class classDeclaration: public final class Class extends Object

implements Serializable, GenericDeclaration, Type, AnnotatedElement

• Instances of the class Class represent classes and interfaces in a running Java application.

• Every array also belongs to a class that is reflected as a Class object that is shared by all arrays with the same element type and number of dimensions.

• The primitive Java types (boolean, byte, char, short, int, long, float, and double), and the keyword void are also represented as Class objects.

• Class has no public constructor. Instead Class objects are constructed automatically by the Java Virtual Machine as classes are loaded in the class loader.

Page 8: 12  Introduction to Java API

8

Class classMethod Summary (Partial List)

static Class forName(String className)

 Class[] getClasses()

 Constructor[] getConstructors()

 Field[] getFields()

 Class[] getInterfaces()

 Method[] getMethods()

 int getModifiers()

 String getName()

 Package getPackage()

 String getSimpleName()

 Class getSuperclass()

 boolean isArray()

 boolean isInstance(Object obj)

 boolean isInterface()

 boolean isLocalClass()

 boolean isMemberClass()

 boolean isPrimitive()

Page 9: 12  Introduction to Java API

9

System classDeclaration: public final class System extends Object

• The System class contains several useful class fields and methods which are related to the following operations: • standard input, standard output, and error output streams.• access to externally defined properties and environment variables.• a means of loading files and libraries.• and a utility method for quickly copying a portion of an array.

Page 10: 12  Introduction to Java API

10

System classMethod Summary (Partial List)

static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length)

static String clearProperty(String key)

static long currentTimeMillis()

static void exit(int status)

static void gc()

static Map<String,String> getenv()

static String getenv(String name)

static Properties getProperties() .

static SecurityManager getSecurityManager()

static void load(String filename)

static long nanoTime()

static void runFinalization()

static void runFinalizersOnExit(boolean value)

static void setErr(PrintStream err)

static void setIn(InputStream in)

static void setOut(PrintStream out)

static void setProperties(Properties props)

static void setSecurityManager(SecurityManager s)

Field Summarystatic 

PrintStream err

static InputStream in

static PrintStream out

Page 11: 12  Introduction to Java API

11

String classDeclaration: public final class String extends Object

implements Serializable, Comparable<String>, CharSequence

• The String class represents character strings. All string literals in Java programs are implemented as instances of this class.

• Strings are immutable, their values cannot be changed after they are created

• The class String includes methods for examining individual characters of the sequence, for comparing strings, for searching strings, for extracting substrings, and for creating a copy of a string with all characters translated to uppercase or to lowercase.

Page 12: 12  Introduction to Java API

12

String classMethod Summary (Partial List)

 char charAt(int index)

 int compareTo(String anotherString)

 String concat(String str)  boolean contains(CharSequence s)  boolean endsWith(String suffix)

 boolean equals(Object anObject)

static String format(String format, Object... args)

 int hashCode()

 int indexOf(int ch)

 int length()  boolean matches(String regex)

 String replace(char oldChar, char newChar)

 String[] split(String regex)

 boolean startsWith(String prefix)

 String substring(int beginIndex)

 String toLowerCase()

 String toUpperCase()

 String trim()

Page 13: 12  Introduction to Java API

13

StringBuffer classDeclaration: public final class StringBuffer

extends Object implements Serializable, CharSequence

• StringBuffer is a thread-safe, mutable sequence of characters. A StringBuffer is like a String, but can be modified.

• StringBuffer class has been supplemented with an equivalent class designed for use by a single thread, StringBuilder.

• The StringBuilder class should generally be used in preference to StringBuffer as it supports all of the same operations but is faster as it performs no synchronization.

Page 14: 12  Introduction to Java API

14

StringBuffer classMethod Summary (Partial List)

 StringBuffer append(String str)

 StringBuffer append(StringBuffer sb)

 int capacity()

 char charAt(int index)

 StringBuffer delete(int start, int end)

 StringBuffer deleteCharAt(int index)

 int indexOf(String str)  StringBuffer insert(int offset, String str)

 int lastIndexOf(String str)

 int length()

 StringBuffer replace(int start, int end, String str)

 StringBuffer reverse()

 void setCharAt(int index, char ch)

 void setLength(int newLength)

 String substring(int start)

 String substring(int start, int end)

 String toString()

 void trimToSize()

Page 15: 12  Introduction to Java API

15

Math classDeclaration: public final class Math extends Object

• Math class contains methods for performing basic numeric operations such as elementary exponential, logarithm, square root, and trigonometric functions.

• Math class cannot be extended (it is declared final) nor instantiated (its constructor is private).

• Its methods are declared static and can be invoked using its class name.

Page 16: 12  Introduction to Java API

16

Math classMethod Summary (Partial List)

static double abs(double a)

static double ceil(double a)

static double cos(double a)

static double exp(double a)

static double floor(double a)

static double log(double a)

static double log10(double a)

static double log1p(double x)

static double max(double a, double b)

static double min(double a, double b)

static double pow(double a, double b)

static double random() static long round(double a)

static double sin(double a)

static double sqrt(double a)

static double tan(double a)

static double toDegrees(double angrad)

static double toRadians(double angdeg)

Field Summarystatic doubl

e E

static double PI

Page 17: 12  Introduction to Java API

17

Wrapper Classes

The wrapper classes serve two primary purposes:

• To provide a mechanism to "wrap" primitive values in an object so that the primitives can be included in activities reserved for objects, such as being added to collections, or returned from a method with an object return value.

• To provide an assortment of utility functions for primitives. Most of these functions are related to various conversions: converting primitives to and from String objects, and converting primitives and String objects to and from different bases (or radix), such as binary, octal, and hexadecimal.

Page 18: 12  Introduction to Java API

18

Wrapper Classes Constructors

short or StringShortshort

long or StringLonglong

int or StringIntegerint

float, double, or StringFloatfloat

double or StringDoubledouble

charCharacterchar

byte of StringBytebyte

boolean or String or nullBooleanboolean

Constructor ArgumentsWrapper ClassPrimitive

Page 19: 12  Introduction to Java API

19

java.util package

Contains classes related to the following:• Collections framework• Legacy collection classes• Event model• Date and time facilities• Internationalization• Miscellaneous utility classes

• StringTokenizer, random-number generator, and bit array

Page 20: 12  Introduction to Java API

20

Key Points• The Java API is prewritten code organized into packages of similar topics • java.lang provides classes that are fundamental to the design of the

Java programming language• java.lang contains the following classes: Object, Class, Math,

String, StringBuffer, Throwable,Wrapper classes and System classes

• java.lang package is implicitly imported in every Java source file• Object is the superclass of all classes• String objects are immutable• java.util contains classes related to the following:

• Collections framework• Legacy collection classes• Event model• Date and time facilities• Internationalization• Miscellaneous utility classes