using a class from the class library
play

Using a class from the class library public class URLSplitter { - PowerPoint PPT Presentation

Using a class from the class library public class URLSplitter { public static void main(String[] args) { for (int i = 0; i < args.length; i++) { try { java.net.URL u = new java.net.URL(args[i]); System.out.println("Protocol: " +


  1. Using a class from the class library public class URLSplitter { public static void main(String[] args) { for (int i = 0; i < args.length; i++) { try { java.net.URL u = new java.net.URL(args[i]); System.out.println("Protocol: " + u.getProtocol()); System.out.println("Host: " + u.getHost()); System.out.println("Port: " + u.getPort()); System.out.println("File: " + u.getFile()); System.out.println("Ref: " + u.getRef()); } catch (java.net.MalformedURLException e) { System.err.println(args[i] + " is not a valid URL"); } } } }

  2. Importing Classes � Fully qualifed names like java.net.URL are not the most convenient thing to have to type. � You can use the shorter class names like URL without the java.net part if you first import the class by adding an import statement at the top of the file.

  3. Importing Classes import java.net.URL; import java.net.MalformedURLException; public class URLSplitter { public static void main(String[] args) { for (int i = 0; i < args.length; i++) { try { URL u = new URL(args[i]); System.out.println("Protocol: " + u.getProtocol()); System.out.println("Host: " + u.getHost()); System.out.println("Port: " + u.getPort()); System.out.println("File: " + u.getFile()); System.out.println("Ref: " + u.getRef()); } catch (MalformedURLException e) { System.err.println(args[i] + " is not a valid URL"); } } } }

  4. Package Imports � Instead of importing each class you need individually, you can import an entire package by replacing the class name with an asterisk (*) like this:

  5. Package Imports import java.net.*; public class URLSplitter { public static void main(String[] args) { for (int i = 0; i < args.length; i++) { try { URL u = new URL(args[i]); System.out.println("Protocol: " + u.getProtocol()); System.out.println("Host: " + u.getHost()); System.out.println("Port: " + u.getPort()); System.out.println("File: " + u.getFile()); System.out.println("Ref: " + u.getRef()); } catch (MalformedURLException e) { System.err.println(args[i] + " is not a valid URL"); } } } }

  6. Name Conflicts when importing packages Name Conflicts when importing packages � There are a couple of name conflicts in the class library. � The worst offenders are java.util.List and java.awt.List. � The second worst offenders are java.sql.Date and java.util.Date, though this case is somewhat mitigated because java.sql.Date is a subclass of java.util.Date. � If you need to use one or both of the conflictingly named classes as well as importing the other package, you simply have to use the full package qualified name, inconvenient as it may be to type.

  7. You don't need to import java.lang.* � There is one exception to the import rule. All classes in the java.lang package are imported by default. Thus you do not need to import java.lang.*; to use them without fully qualifed names.

  8. The java.lang package � Interfaces in java.lang – Cloneable – Runnable

  9. The java.lang package � Classes in java.lang – Object – Boolean – Process – Byte – Runtime – Character – SecurityManager – Class – Short – ClassLoader – String – Compiler – StringBuffer – Double – System – Float – Thread – Integer – ThreadGroup – Long – Throwable – Math – Void – Number

  10. The java.lang package � Exceptions in java.lang – IndexOutOfBoundsException – InstantiationException – ArithmeticException – InterruptedException – ArrayIndexOutOfBoundsException – NegativeArraySizeException – ArrayStoreException – NoSuchFieldException – ClassCastException – NoSuchMethodException – ClassNotFoundException – NullPointerException – CloneNotSupportedException – NumberFormatException – Exception – RuntimeException – IllegalAccessException – SecurityException – IllegalArgumentException – StringIndexOutOfBoundsException – IllegalMonitorStateException – IllegalStateException – IllegalThreadStateException

  11. The java.lang package � Errors in java.lang – NoClassDefFoundError – NoSuchFieldError – AbstractMethodError – NoSuchMethodError – ClassCircularityError – OutOfMemoryError – ClassFormatError – StackOverflowError – Error – ThreadDeath – ExceptionInInitializerError – UnknownError – IllegalAccessError – UnsatisfiedLinkError – IncompatibleClassChangeError – VerifyError – InstantiationError – VirtualMachineError – InternalError – LinkageError

  12. java.lang.Object � The java.lang.Object class is the ultimate superclass of all objects If a class does not explicitly extend a class, then the compiler assumes it extends java.lang.Object. � The Methods of java.lang.Object – public Object() – public final Class getClass() – public int hashCode() – public boolean equals(Object obj) – protected Object clone() throws CloneNotSupportedException – public String toString() – public final void notify() – public final void notifyAll() – public final void wait(long timeout) throws InterruptedException – public final void wait(long timeout, int nanos) throws InterruptedException – public final void wait() throws InterruptedException – protected void finalize() throws Throwable

  13. java.lang.Math double x = Math.sqrt(9.0);

  14. Examples of java.lang.Math Methods public class MathLibraryExample { public static void main(String args[]) { int i = 7; int j = -9; double x = 72.3; double y = 0.34; System.out.println("i is " + i); System.out.println("j is " + j); System.out.println("x is " + x); System.out.println("y is " + y); // The absolute value of a number is equal to // the number if the number is positive or // zero and equal to the negative of the number // if the number is negative. System.out.println("|" + i + "| is " + Math.abs(i)); System.out.println("|" + j + "| is " + Math.abs(j)); System.out.println("|" + x + "| is " + Math.abs(x)); System.out.println("|" + y + "| is " + Math.abs(y)); // Truncating and Rounding functions // You can round off a floating point number // to the nearest integer with round() System.out.println(x + " is approximately " + Math.round(x)); System.out.println(y + " is approximately " + Math.round(y));

  15. // The "ceiling" of a number is the // smallest integer greater than or equal to // the number. Every integer is its own // ceiling. System.out.println("The ceiling of " + i+ " is "+ Math.ceil(i)); System.out.println("The ceiling of " + j+ " is "+ Math.ceil(j)); System.out.println("The ceiling of " + x+ " is "+ Math.ceil(x)); System.out.println("The ceiling of " + y+ " is "+ Math.ceil(y)); // The "floor" of a number is the largest // integer less than or equal to the number. // Every integer is its own floor. System.out.println("The floor of " + i +" is " + Math.floor(i)); System.out.println("The floor of " + j +" is " + Math.floor(j)); System.out.println("The floor of " + x +" is " + Math.floor(x)); System.out.println("The floor of " + y +" is " + Math.floor(y)); // Comparison operators // min() returns the smaller of the two arguments you pass it System.out.println("min(" + i + "," + j + ") is " + Math.min(i,j)); System.out.println("min(" + x + "," + y + ") is " + Math.min(x,y)); System.out.println("min(" + i + "," + x + ") is " + Math.min(i,x)); System.out.println("min(" + y + "," + j + ") is " + Math.min(y,j));

  16. // There's a corresponding max() method // that returns the larger of two numbers System.out.println("max(" + i + "," + j + ") is " + Math.max(i,j)); System.out.println("max(" + x + "," + y + ") is " + Math.max(x,y)); System.out.println("max(" + i + "," + x + ") is " + Math.max(i,x)); System.out.println("max(" + y + "," + j + ") is " + Math.max(y,j)); // The Math library defines a couple // of useful constants: System.out.println("Pi is " + Math.PI); System.out.println("e is " + Math.E); // Trigonometric methods // All arguments are given in radians // Convert a 45 degree angle to radians double angle = 45.0 * 2.0 * Math.PI/360.0; System.out.println("cos(" + angle + ") is " + Math.cos(angle)); System.out.println("sin(" + angle + ") is " + Math.sin(angle));

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend