html5-img
1 / 77

Java 入門與進階設計 第 1 3 章: Java Utility

Java 入門與進階設計 第 1 3 章: Java Utility. 講師:紀俊男 cnchi@ms10.url.com.tw. 本章重點. java.lang.Character java.lang.String java.lang.String Buffer java.lang.Number java.lang.Math. Character 物件. Java 提供了兩種處理字元的方法 char x = ‘ a ’ ; Character x = new Character( ‘ a ’ ); 特性比較. Character 物件.

emory
Télécharger la présentation

Java 入門與進階設計 第 1 3 章: Java Utility

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. Java 入門與進階設計第 13 章:Java Utility 講師:紀俊男 cnchi@ms10.url.com.tw

  2. 本章重點 • java.lang.Character • java.lang.String • java.lang.String Buffer • java.lang.Number • java.lang.Math

  3. Character 物件 • Java 提供了兩種處理字元的方法 • char x = ‘a’; • Character x = new Character(‘a’); • 特性比較

  4. Character 物件 • Character 建構元 • Character(char value); • 型態轉換方法 (Casting Method) • 原始 Character 物件:Character c = new Character(‘a’); • 轉成字元:char x = c.charValue(); • 轉成字串:String s = c.toString();

  5. Character 物件 • 測試函數:

  6. Character 物件 • 轉換函數

  7. Character 物件 • 範例 public class CharacterDemo { public static void main(String args[]) { Character a = new Character('a'); Character a2 = new Character('a'); Character b = new Character('b'); int difference = a.compareTo(b); if (difference == 0) { System.out.println("a is equal to b."); } else if (difference < 0) { System.out.println("a is less than b."); } else if (difference > 0) { System.out.println("a is greater than b."); }

  8. Character 物件 System.out.println("a is " + ((a.equals(a2)) ? "equal" : "not equal") + " to a2."); System.out.println("The character " + a.toString() + " is " + (Character.isUpperCase(a.charValue()) ? "upper" : "lower") + "case."); } } 回到原主題

  9. 為何有兩種字串物件 • String 物件 • 專門處理字串常數 • 如:在 Java 程式中出現 “Hello! World!”,Java 即以 String 物件處理之。 • 特性:一旦宣告後,其內容不可更動。 • StringBuffer • 專門處理字串變數 • 如:在 Java 中出現 “Hello!” + “World!”這類運算時,Java 會偷偷以 StringBuffer 處理。 • 特性:字串可以做運算。

  10. String 物件宣告 • 只要是字串常數出現的地方,Java 就會偷偷宣告一個 String 物件: • String s = “Hello! World!”; • String s = new String(“Hello! World!”); • System.out.println(“Hello! World!”); • String s = new String(“Hello! World!”);int StrLen = s.length();與下面程式等效int StrLen = “Hello! World!”.length();

  11. String 物件簡介 • 繼承物件 • java.lang.Object  java.lang.String • 原始宣告 • public final class String extends Object implements Serializable, Comparable

  12. String 物件常用方法 • 字串長度 • int length() • 範例: • String s = “Hello! World!”;System.out.println(s.length()); • 結果:13

  13. String 物件常用方法 • 字串比較 • boolean equals(Object anObject); • boolean equalsIgnoreCase(String anString); • int compareTo(String anString); • int compareToIgnoreCase(String str);

  14. String 物件常用方法 • 字串比較範例 (1) public class StringCompare { public static void main(String[] args) { String s1 = "Java"; String s2 = "Java"; String s3 = "java"; // Print out all string System.out.println(s1 + "\n" + s2 + "\n" + s3 + "\n"); // "==" test System.out.println((s1 == s2)? "s1 == s2":"s1 != s2"); // "==" test again s2 = "Ja"; s2 = s2 + "va"; System.out.println((s1 == s2)? "s1 == s2":"s1 != s2");

  15. String 物件常用方法 • 字串比較範例 (2) // equals test System.out.println((s1.equals(s2))? "s1 equals s2":"s1 not equals s2"); // equalsIgnoreCase test System.out.println((s1.equals(s3))? "s1 equals s3":"s1 not equals s3"); System.out.println((s1.equalsIgnoreCase(s3))? "s1 equals s3 without case" : "s1 not equals s3 without case"); //compareTo test System.out.println("s1 compare to s3: " + s1.compareTo(s3)); System.out.println("s1 compare to s3 without case: " + s1.compareToIgnoreCase(s3)); } }

  16. String 物件常用方法 • 字串連結 • + • String concat (String str); • 範例 • String s1 = “Hello!”;String s2 = “World!”;s1 += s2; • String s1 = “Hello!”;String s2 = “World!”;s1.concat(s2);

  17. String 物件常用方法 • 去空白: • String trim(); • 範例 • String s = “ Hello! World! “;s.trim();System.out.println(s); • 結果:“Hello! World!”

  18. String 物件常用方法 • 子字串操作 • String substring(int beginIndex); • String substring(int beginIndex, int endIndex); • char charAt(int index); • int indexOf(int ch); • int indexOf(String str); • int lastIndexOf(int ch); • int lastIndexOf(String str); • boolean startsWith(String prefix); • boolean endsWith(String postfix);

  19. String 物件常用方法 • 子字串操作範例 public class SubstringTest { public static void main (String[] args) { String s = "This is a book"; System.out.println("String: " + s); System.out.println("substring(8): " + s.substring(8)); System.out.println("substring(0, 6): " + s.substring(0, 6)); System.out.println("charAt(8): " + s.charAt(8)); System.out.println("indexOf('i'): " + s.indexOf('i')); System.out.println("indexOf(\"is\"): " + s.indexOf("is")); System.out.println("lastIndexOf('i'): " + s.lastIndexOf('i')); System.out.println("lastIndexOf(\"is\"): " + s.lastIndexOf("is")); System.out.println("startsWith(\"Th\"): " + s.startsWith("Th")); System.out.println("endsWith(\"ok\"): " + s.endsWith("ok")); } }

  20. String 物件常用方法 • 字串轉換 • String toLowerCase(); • String toUpperCase(); • String valueOf(int i); • String valueOf(long l); • String valueOf(float f); • String valueOf(double d); • String valueOf(char c); • String valueOf(boolean b);

  21. String 物件常用方法 • 字串轉換範例 public class StringConvert { public static void main (String[] args) { String s = "This is a book"; System.out.println("String: " + s); System.out.println("toLowerCase(): " + s.toLowerCase()); System.out.println("toUpperCase(): " + s.toUpperCase()); System.out.println("int 20 --> String: " + String.valueOf(20)); System.out.println("long 20L --> String: " + String.valueOf(20L)); System.out.println("float 20.0f --> String: " + String.valueOf(20.0f)); System.out.println("double 20.0d --> String: " + String.valueOf(20.0d)); System.out.println("char 'a' --> String: " + String.valueOf('a')); System.out.println("boolean true --> String: " + String.valueOf(true)); } } 回到原主題

  22. StringBuffer 物件簡介 • 繼承物件 • java.lang.Object  java.lang.String • 原始宣告 • Public final class StringBuffer extends Object implements Serializable • 特色 • 可以修改字串內容,並加以操作

  23. StringBuffer 物件常用方法 • 建構元 • StringBuffer(); • StringBuffer(int length); • StringBuffer(String str);

  24. StringBuffer 物件常用方法 • 字串長度 • int capacity(); • int length(); • void ensureCapacity(int minimumCapacity); • 將舊長度 * 2 + 2,直到大於等於要求長度為止 • void setLength(int newLength) • 原字串較短者,不足部份填 0; 較長者,多餘部份裁掉。 • 轉換函數 • String toString();

  25. StringBuffer 物件常用方法 • 字串長度範例 public class StringBufferLength { public static void main (String[] args) { StringBuffer buf = new StringBuffer(20); buf.append("This is a book"); System.out.println("StringBuffer: " + buf); System.out.println("Capacity: " + buf.capacity()); System.out.println("Length: " + buf.length()); System.out.println("------------------------------"); buf.ensureCapacity(30); System.out.println("Capacity: " + buf.capacity()); System.out.println("StringBuffer after ensureCapacity(30): " + buf); buf.setLength(10); System.out.println("Length: " + buf.length()); System.out.println("StringBuffer after setLength(10): " + buf); } }

  26. StringBuffer 物件常用方法 • 字串操作 • StringBuffer append(int i); • StringBuffer append(long l); • StringBuffer append(float f); • StringBuffer append(double d); • StringBuffer append(char c); • StringBuffer append(boolean b); • StringBuffer append(String str);

  27. StringBuffer 物件常用方法 • 字串操作 • StringBuffer insert(int offset, int i); • StringBuffer insert(int offset, long l); • StringBuffer insert(int offset, float f); • StringBuffer insert(int offset, double d); • StringBuffer insert(int offset, char c); • StringBuffer insert(int offset, boolean b); • StringBuffer insert(int offset, String str);

  28. StringBuffer 物件常用方法 • 字串操作 • StringBuffer replace(int start, int end, String str); • StringBuffer delete(int start, int end); • StringBuffer reverse();

  29. StringBuffer 物件常用方法 • 字串操作範例 public class StringBufferOperation { public static void main(String[] args) { StringBuffer buf = new StringBuffer(); buf.append("This is a book"); System.out.println("Append String: " + buf); buf.insert(10, "bad "); System.out.println("Insert String: " + buf); buf.replace(10, 13, "good"); System.out.println("Replace String: " + buf); buf.delete(10, 15); System.out.println("Delete String: " + buf); buf.reverse(); System.out.println("Reverse String: " + buf); } }

  30. StringBuffer 物件常用方法 • 子字串操作 • char charAt(int index); • void setCharAt(int index, char ch); • StringBuffer deleteCharAt(int index); • StringBuffer substring(int start); • StringBuffer substring(int start, int end);

  31. StringBuffer 物件常用方法 • 子字串範例 public class SubstringBuffer { public static void main(String[] args) { StringBuffer buf = new StringBuffer("This is a book"); System.out.println("String: " + buf); System.out.println("charAt(10): " + buf.charAt(10)); buf.setCharAt(10, 'c'); System.out.println("setCharAt(10, 'c'): " + buf); System.out.println("deleteCharAt(10): " + buf.deleteCharAt(10)); System.out.println("substring(10): " + buf.substring(10)); } }

  32. Java 也偷偷用 StringBuffer • 當 Java 遇到如下的程式碼時: • String s = “This is”;System.out.println(s + “ a book”); • 事實上,它用如下的程式碼來處理: • String s = “This is”;System.out.println(new StringBuffer(s) . append(“ a book”).toString());

  33. 總整理 • String 與 StringBuffer 有何不同? • String 繼承何種物件?其原始宣告又是什麼? • 試述下列 String 物件常用方法的功能: • length() • equals() / equalsIgnoreCase() • compareTo() / compareToIgnoreCase() • concat() / trim() • substring() / charAt() / indexOf() / lastIndexOf() • startsWith() / endsWith() • toLowerCase() / toUpperCase() • valueOf()

  34. 總整理 • StringBuffer 繼承何種物件?其原始宣告又是什麼? • 試述下列 StringBuffer 物件常用方法的功能: • capacity() / length() / ensureCapacity() / setLength() • append() / insert() / replace() / delete() / reverse() • charAt() / setCharAt() / deleteCharAt() • substring() 回到原主題

  35. Type Wrapper 觀念介紹 • 為什麼會有 Type Wrapper int x = 3; Integer x = new Integer(3); x 0x5678 0x1234 x 3 0x5678 3 toString() x = 3; 缺少操作此資料的函數 / 方法 x->Integer(3); 資料與方法同在,方便

  36. Type Wrapper 觀念介紹 • Type Wrapper 有哪些

  37. Type Wrapper 觀念介紹 • Number (java.lang) • 所有 Type Wrapper 之父類別 • Byte, Short, Integer, Long (java.lang) • 對應 byte, short, int, long 等 Primitive Type • Float, Double (java.lang) • 對應 float, double 等 Primitive Type • BigInteger, BigDecimal (java.math) • 允許程式師儲存 Primitive Type 無法表示的數值長度及精確度

  38. Type Wrapper 觀念介紹 • 為了完整對應所有的 Primitive Type,下列三者 Type Wrapper 被創造出來,但不繼承 java.lang.Number • Boolean (java.lang) boolean • Character (java.lang) char • Void (java.lang)  void

  39. Number常用屬性及方法 • Number 類別 • 繼承 java.lang.Object • abstract class  意即:僅供繼承,不供產生實體

  40. Number常用屬性及方法 • Number 常用屬性 無

  41. Number常用屬性及方法 • Number 常用方法 • byte byteValue() • short shortValue() • abstract int intValue() • abstract long longValue() • abstract float floatValue() • abstract double doubleValue()

  42. Number 範例 public class NumberDemo { public static void main(String[] args) { Number n = new Number(); } } Error Message: NumberDemo.java:3: java.lang.Number is abstract; cannot be instantiated Number n = new Number(); ^ 1 error

  43. Byte / Short 常用屬性及方法 • Byte 類別 • 繼承 java.lang.Number • final class 意即:無法再被其它類別繼承 • public final class Byte extends Number implements Comparable • Short 類別 • 繼承 java.lang.Number • final class 意即:無法再被其它類別繼承 • public final class Short extends Number implements Comparable

  44. Byte / Short 常用屬性及方法 • 建構元 • Byte(byte value);Short(short value); • Byte(String s);Short(String s); • 範例 • Byte b = new Byte(100); • Short s = new Short(“1234”);

  45. Byte / Short 常用屬性及方法 • Byte / Short 常用屬性 • .MIN_VALUE • Byte.MIN_VALUE  傳回 –128 • Short.MIN_VALUE  傳回 –32768 • .MAX_VALUE • Byte.MAX_VALUE  傳回 +127 • Short.MAX_VALUE  傳回 +32767

  46. Byte / Short 常用屬性及方法 • Byte / Short 常用方法(繼承自 java.lang.Number) • byte byteValue(); • short shortValue(); • int intValue(); • long longValue(); • float floatValue(); • double doubleValue();

  47. Byte / Short 常用屬性及方法 • Byte / Short 常用方法(實作 java.lang.Comparable 介面) • int compareTo(Byte obj);int compareTo(Short obj); • boolean equals(Object obj); • 範例 • Byte b1 = new Byte(3);Byte b2 = new Byte(4);b1.compareTo(b2);b1.equals(b2);

  48. Byte / Short 常用屬性及方法 • Byte / Short 常用方法(字串轉數字) • static Byte valueOf(String s); • static Short valueOf(String s); • 傳入十進位字串,傳回數值物件 • static Byte decode(String s); • static Short decode(String s); • 傳入十、十六、八進位字串,傳回數值物件 • static byte parseByte(String s); • static short parseShort(String s); • 傳入十進位字串,傳回數值本身

  49. Byte / Short 常用屬性及方法 • Byte / Short 常用方法(數字轉字串) • String toString()

  50. Byte / Short 範例 public class ByteShortDemo { public static void main(String[] args) { System.out.println("MAX of byte: " + Byte.MAX_VALUE); System.out.println("MIN of short: " + Short.MIN_VALUE); Byte b = Byte.valueOf("100"); System.out.println("Value of 100 with Byte: " + b.toString()); Short s = Short.decode("0x100"); System.out.println("Value of 0x100 with Short: " + s.toString()); System.out.println("Value of 2000 with Short: " + Short.parseShort("2000")); } }

More Related