An introduction to packages Session 9 Objectives n

An introduction to packages Session 9

Objectives n n n Explore the java. lang package Discuss various Wrapper classes Discuss the String and String. Buffer classes Discuss the concept of immutability Identify the methods of the following classes and interfaces n n n Math System Object Class Thread. Group Runtime An introduction to packages / 2 of 31

Code libraries n A library comprises a group of related files n n For eg: a math library will contain functions or subroutines that are used in mathematical calculations The basic idea behind using a code library is to sort out files or functions based on their functionality Using these codes saves a lot of coding time In C they are known as header files, in C++ as class libraries and in Java as packages An introduction to packages / 3 of 31

Creating packages in Java (1) n In Java, a package is a combination of classes, interfaces and sub-packages n n n For eg: java. awt package has a sub-package called event A package in Java can be created by including a package statement as the first statement in a Java program Syntax to define a package is – n package <pkgname>; An introduction to packages / 4 of 31

Example An introduction to packages / 5 of 31

Creating packages in Java (2) n n When a Java program is executed, the JVM searches for the classes used within the program on the file system Uses one of two elements to find a class – n n n The package name The directories listed in the CLASSPATH environment variable If no CLASSPATH is defined, then JVM looks for the default javalib directory and the current working directory An introduction to packages / 6 of 31

Packages and access control(1) n n A public member of a class can be accessed from anywhere; within the package, outside the package, within a subclass, as well as within a non-subclass A member of a class that is declared private can be accessed only within the class but nowhere outside the class An introduction to packages / 7 of 31

Packages and access control(2) n n A protected member of a class can be accessed from any class in the same package and from a subclass that is outside the package If no access specifier is given, the member would be accessible within any class in the same package but not outside the package An introduction to packages / 8 of 31

Wrapper classes (1) n n Wrapper classes are a part of java. lang package They encapsulate simple primitive types in the form of classes Useful whenever we need object representations of primitive types All numeric Wrapper classes extend the abstract superclass ‘Number’ An introduction to packages / 9 of 31

Wrapper classes (2) n n The six numeric Wrapper classes are Double, Float, Byte, Short, Integer and Long Character is a wrapper class for the primitive char data type Boolean is a wrapper class for boolean values Constructor of Integer Wrapper class : n n Integer(int value) Integer(String a) throws Number. Format. Exception An introduction to packages / 10 of 31

Example (1) Output An introduction to packages / 11 of 31

Example (2) Output An introduction to packages / 12 of 31

String class (1) n n In Java, a string literal is an object of type String class Hence manipulation of strings will be done through the use of the methods provided by the String class An introduction to packages / 13 of 31

String class (2) n Some String class constructors: n n public String() – creates an empty string public String(String value) – creates a new string that is a copy of the given string public String(char[] value, int begin, int count) – this constructs a new string based on the character array starting from the position begin, which is count characters long public String(String. Buffer buffer) – creates a new string based on a String. Buffer value An introduction to packages / 14 of 31

String class (3) n n String length() – this method determines the length of a string The == operator and equals() method can be used for string comparison n n The == operator checks if the two operands being used are one and the same object The equals() method checks if the contents of the two operands are the same An introduction to packages / 15 of 31

Example Output An introduction to packages / 16 of 31

String class (4) n Searching Strings n n index. Of() method searches within a string for a given character or String Syntax : n n public int index. Of (int ch) public int index. Of(String value) An introduction to packages / 17 of 31

String class (5) n Extracting portions of a String n n public char. At(int index) – used to extract a single character from given position public char[] to. Char. Array() – used to obtain an entire string in the form of character array public String substring(int index) : used to extract portions of a string starting from position index public String substring(int beginindex, int endindex) – used to extract portions of a string starting from beginindex position to endindex position An introduction to packages / 18 of 31

Example Output An introduction to packages / 19 of 31

Immutability n n n Strings in Java once created cannot be changed directly This is known as immutability in Strings To overcome this, Java provides String. Buffer class An introduction to packages / 20 of 31

Example An introduction to packages / 21 of 31

Math class n n n This class defines methods for basic numeric operations as well as geometric functions All methods of this class are static The class is final and hence cannot be subclassed An introduction to packages / 22 of 31

Example Output An introduction to packages / 23 of 31

Runtime class n n Encapsulates the runtime environment Used for memory management and executing additional processes Every Java program has a single instance of this class We can determine memory allocation details by using total. Memory() and free. Memory() methods An introduction to packages / 24 of 31

System class n n Provides facilities such as standard input, output and error streams Provides means to access properties associated with the Java runtime system Fields of this class are in, out and err that represent the standard input, output and error respectively The System class cannot be instantiated to create objects An introduction to packages / 25 of 31

Example Output An introduction to packages / 26 of 31

Class n n n Instance of this class encapsulates the run time state of an object in a running Java application This allows us to retrieve information during runtime There is no public constructor for Class An introduction to packages / 27 of 31

Example Object An introduction to packages / 28 of 31

Object class n n n Object class is the superclass of all classes Even is a user-defined class does not extend any other class, it extends Object by default In the code above, we have not imported any class or package. Yet we can make use of the equals() method An introduction to packages / 29 of 31

Thread, Thread. Group and Runnable n n n Multithreading support in Java is provided by means of the Thread and Thread. Group classes and the Runnable interface Thread. Group is used to create a group of threads Whenever it is necessary to manipulate a group of threads as a whole, it is handy to use the Thread. Group class An introduction to packages / 30 of 31

Example Output An introduction to packages / 31 of 31
- Slides: 31