1 / 31

An introduction to packages

An introduction to packages. Session 9. Objectives. Explore the java.lang package. Discuss various Wrapper classes Discuss the String and StringBuffer classes Discuss the concept of immutability Identify the methods of the following classes and interfaces Math System Object Class

wendyharris
Download Presentation

An introduction to packages

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. An introduction to packages Session 9

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

  3. Code libraries • 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 • A library comprises a group of related files • For eg: a math library will contain functions or subroutines that are used in mathematical calculations An introduction to packages / 3 of 31

  4. Creating packages in Java (1) • 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 – • package <pkgname>; • In Java, a package is a combination of classes, interfaces and sub-packages • For eg: java.awt package has a sub-package called event An introduction to packages / 4 of 31

  5. Example An introduction to packages / 5 of 31

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

  7. Packages and access control(1) • 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

  8. Packages and access control(2) • 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

  9. Wrapper classes (1) • 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’ • Wrapper classes are a part of java.lang package An introduction to packages / 9 of 31

  10. Wrapper classes (2) • 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 : • Integer(int value) • Integer(String a) throws NumberFormatException An introduction to packages / 10 of 31

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

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

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

  14. String class (2) • 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(StringBuffer buffer) – creates a new string based on a StringBuffer value • Some String class constructors: An introduction to packages / 14 of 31

  15. String class (3) • The == operator and equals() method can be used for string comparison • 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 • String length() – this method determines the length of a string An introduction to packages / 15 of 31

  16. Example Output An introduction to packages / 16 of 31

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

  18. String class (5) • Extracting portions of a String • public char charAt(int index) – used to extract a single character from given position • public char[] toCharArray() – 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

  19. Example Output An introduction to packages / 19 of 31

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

  21. Example An introduction to packages / 21 of 31

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

  23. Example Output An introduction to packages / 23 of 31

  24. Runtime class • 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 totalMemory() and freeMemory() methods • Encapsulates the runtime environment An introduction to packages / 24 of 31

  25. System class • 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 • Provides facilities such as standard input, output and error streams An introduction to packages / 25 of 31

  26. Example Output An introduction to packages / 26 of 31

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

  28. Example Object An introduction to packages / 28 of 31

  29. Object class • In the code above, we have not imported any class or package. Yet we can make use of the equals() method • 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 An introduction to packages / 29 of 31

  30. Thread, ThreadGroup and Runnable • ThreadGroup 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 ThreadGroup class • Multithreading support in Java is provided by means of the Thread and ThreadGroup classes and the Runnable interface An introduction to packages / 30 of 31

  31. Example Output An introduction to packages / 31 of 31

More Related