runnable jar archives
play

Runnable JAR archives % java -cp eharold.jar MainClassName Inner - PowerPoint PPT Presentation

Runnable JAR archives % java -cp eharold.jar MainClassName Inner Classes Inner Classes Inner classes may also contain methods. They may not contain static members. Inner classes in a class scope can be public, private, protected,


  1. Runnable JAR archives % java -cp eharold.jar MainClassName

  2. Inner Classes Inner Classes � Inner classes may also contain methods. They may not contain static members. � Inner classes in a class scope can be public, private, protected, final, abstract. � Inner classes can also be used inside methods, loops, and other blocks of code surrounded by braces ({}). Such a class is not a member, and therefore cannot be declared public, private, protected, or static. � The inner class has access to all the methods and fields of the top-level class, even the private ones.

  3. Inner Classes Inner Classes public class Queue { Element back = null; public void add(Object o) { Element e = new Element(); e.data = o; e.next = back; back = e; } public Object remove() { if (back == null) return null; Element e = back; while (e.next != null) e = e.next; Object o = e.data; Element f = back; while (f.next != e) f = f.next; f.next = null; return o; } public boolean isEmpty() { return back == null; } // Here's the inner class class Element { Object data = null; Element next = null; } }

  4. java.util.Vector � void add(int index, Object element) � boolean add(Object o) � void addElement(Object obj) � int capacity() � void clear() � Object elementAt(int index) � Enumeration elements() � Object firstElement() � Object get(int index) � int indexOf(Object elem) � void insertElementAt(Object obj, int index) � boolean isEmpty() � Object lastElement() � Object remove(int index) � boolean remove(Object o) � void removeAllElements() � Object[] toArray()

  5. java.lang.String � Constructors public String() public String(String value) public String(char[] text) public String(char[] text, int offset, int count) public String(byte[] data, int offset, int length, String encoding) throws UnsupportedEncodingException public String(byte[] data, String encoding) throws UnsupportedEncodingException public String(byte[] data, int offset, int length) public String(byte[] data) public String(StringBuffer buffer)

  6. � index methods public int length() public int indexOf(int ch) public int indexOf(int ch, int fromIndex) public int lastIndexOf(int ch) public int lastIndexOf(int ch, int fromIndex) public int indexOf(String str) public int indexOf(String str, int fromIndex) public int lastIndexOf(String str) public int lastIndexOf(String str, int fromIndex)

  7. � valueOf() methods public static String valueOf(char[] text) public static String valueOf(char[] text, int offset, int count) public static String valueOf(boolean b) public static String valueOf(char c) public static String valueOf(int i) public static String valueOf(long l) public static String valueOf(float f) public static String valueOf(double d)

  8. � substring() methods public char charAt(int index) public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) public byte[] getBytes(String encoding) throws UnsupportedEncodingException public byte[] getBytes() public String substring(int beginIndex) public String substring(int beginIndex, int endIndex) public String concat(String

  9. � comparisons public boolean equals(Object anObject) public boolean equalsIgnoreCase(String anotherString) public int compareTo(String anotherString) public boolean regionMatches(int toffset, String other, int ooffset, int len) public boolean regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len) public boolean startsWith(String prefix, int toffset) public boolean startsWith(String prefix) public boolean endsWith(String suffix)

  10. � Modifying Strings public String replace(char oldChar, char newChar) public String toLowerCase(Locale locale) public String toLowerCase() public String toUpperCase(Locale locale) public String toUpperCase() public String trim()

  11. java.util.Random Random r = new Random(109876L); int i = r.nextInt(); int j = r.nextInt(); long l = r.nextLong(); float f = r.nextFloat(); double d = r.nextDouble(); int k = r.nextGaussian();

  12. Random r = new Random(); int die = r.nextInt(); die = Math.abs(die); die = die % 6; die += 1; System.out.println(die);

  13. byte[] ba = new byte[1024]; Random r = new Random(); r.nextBytes(ba); for (int i = 0; i < ba.length; i++) { System.out.println(ba[i]); }

  14. java.util.Hashtable

  15. Exceptions � What is an exception? � try-catch � finally � The different kinds of exceptions � Multiple catch clauses � The throws clause � Throwing exceptions � Writing your own exception classes

  16. What is an Exception? public class HelloThere { public static void main(String[] args) { System.out.println("Hello " + args[0]); } } C:\> java HelloThere java.lang.ArrayIndexOutOfBoundsException: 0 at HelloThere.main(HelloThere.java:5) � This is not a crash. � The virtual machine exits normally. � All memory is cleaned up. � All resources are released.

  17. What is an Exception? � Why use exceptions instead of return values ? 1. Forces error checking 2. Cleans up your code by separating the normal case from the exceptional case. (The code isn't littered with a lot of if-else blocks checking return values.) 3. Low overhead for non-exceptional case � Traditional programming languages set flags or return bad values like -1 to indicate problems. � Programmers often don't check these values. � Java throws Exception objects to indicate a problem. These cannot be ignored.

  18. try-catch public class HelloThere { public static void main(String[] args) { try { System.out.println("Hello " + args[0]); } catch (ArrayIndexOutOfBoundsException e) { System.out.println("Hello Who ever you are."); } } }

  19. What can you do with an exception once you've caught it? 1. Fix the problem and try again. 2. Do something else instead. 3. Exit the application with System.exit() 4. Rethrow the exception. 5. Throw a new exception. 6. Return a default value (in a non-void method). 7. Eat the exception and return from the method (in a void method). 8. Eat the exception and continue in the same method

  20. The finally keyword public class HelloThere { public static void main(String[] args) { try { System.out.println("Hello " + args[0]); } catch (ArrayIndexOutOfBoundsException e) { System.out.println("Hello Whoever you are."); } finally { System.out.println("How are you?"); } } }

  21. The different kinds of exceptions

  22. The Throwable class hierarchy java.lang.Object | +----java.lang.Throwable | +----java.lang.Error | +----java.lang.Exception | +----java.io.IOException | +----java.lang.RuntimeException | +----java.lang.ArithmeticException | +----java.lang.ArrayIndexOutOfBoundsException | +----java.lang.IllegalArgumentException | +----java.lang.NumberFormatException

  23. Catching multiple exceptions public class HelloThere { public static void main(String[] args) { int repeat; try { repeat = Integer.parseInt(args[0]); } catch (ArrayIndexOutOfBoundsException e) { // pick a default value repeat = 1; } catch (NumberFormatException e) { // print an error message System.err.println("Usage: java HelloThere repeat_count" ); System.err.println("where repeat_count is the number of times to say Hello" ); System.err.println("and given as an integer like 1 or 7" ); return; } for (int i = 0; i < repeat; i++) { System.out.println("Hello"); } } }

  24. Catching multiple exceptions public class HelloThere { public static void main(String[] args) { int repeat; try { // possible NumberFormatException and ArrayIndexOutOfBoundsException repeat = Integer.parseInt(args[0]); // possible ArithmeticException int n = 2/repeat; // possible StringIndexOutOfBoundsException String s = args[0].substring(5); } catch (NumberFormatException e) { // print an error message System.err.println("Usage: java HelloThere repeat_count" ); System.err.println("where repeat_count is the number of times to say Hello" ); System.err.println("and given as an integer like 1 or 7" ); return; } catch (ArrayIndexOutOfBoundsException e) { // pick a default value repeat = 1; }

  25. catch (IndexOutOfBoundsException e) { // ignore it } catch (Exception e) { // print an error message and exit System.err.println("Unexpected exception"); e.printStackTrace(); return; } for (int i = 0; i < repeat; i++) { System.out.println("Hello"); } } }

  26. The throws keyword public static void copy(InputStream in, OutputStream out) throws IOException { byte[] buffer = new byte[256]; while (true) { int bytesRead = in.read(buffer); if (bytesRead == -1) break; out.write(buffer, 0, bytesRead); } }

  27. Throwing Exceptions public class Clock { private int hours; // 1-12 private int minutes; // 0-59 private int seconds; // 0-59 public Clock(int hours, int minutes, int seconds) { if (hours < 1 || hours > 12) { throw new IllegalArgumentException("Hours must be between 1 and 12"); } if (minutes < 0 || minutes > 59) { throw new IllegalArgumentException("Minutes must be between 0 and 59"); } if (seconds < 0 || seconds > 59) { throw new IllegalArgumentException("Seconds must be between 0 and 59"); } this.hours = hours; this.minutes = minutes; this.seconds = seconds; } public Clock(int hours, minutes) { this(hours, minutes, 0); } public Clock(int hours) { this(hours, 0, 0); } }

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