一、String
a,特點:
--String字符串類,用來表示"",String類被final修飾,也就是不可以被繼承。
--String類底層維護了一個char[],而且是final的,也就是說數組一旦創建長度和值都不能被修改了,字符串就變成了一個常量。
--源碼摘抄:
--public final class String{}
--private final char value[];
b,創建對象:
--String(char[] value) 分配一個新的 String,使其表示字符數組參數中當前包含的字符序列。
c,常用方法:
--char charAt(int index) 返回指定索引處的 char 值。
--String concat(String str) 將指定字符串連接到此字符串的結尾。
--boolean contains(CharSequence s) 當且僅當此字符串包含指定的 char 值序列時,返回 true。
d,測試:
public class Test1_String {
public static void main ( String[ ] args) {
char [ ] c = new char [ 10 ] ;
String s = new String ( c) ;
String s2 = "abc" ;
System. out. println ( s2. charAt ( 1 ) ) ;
System. out. println ( s2. concat ( "mno" ) ) ;
System. out. println ( s2. contains ( "bc" ) ) ;
System. out. println ( s2. endsWith ( "opq" ) ) ;
System. out. println ( s2. equals ( "abc" ) ) ;
System. out. println ( s2. hashCode ( ) ) ;
System. out. println ( s2. getBytes ( ) ) ;
System. out. println ( Arrays. toString ( s2. getBytes ( ) ) ) ;
s2= "abcaaaccc" ;
System. out. println ( s2. indexOf ( "c" ) ) ;
System. out. println ( s2. lastIndexOf ( "c" ) ) ;
System. out. println ( s2. isEmpty ( ) ) ;
System. out. println ( s2. length ( ) ) ;
System. out. println ( s2. startsWith ( "iop" ) ) ;
System. out. println ( s2. substring ( 3 ) ) ;
System. out. println ( s2. substring ( 3 , 6 ) ) ;
System. out. println ( s2. toCharArray ( ) ) ;
System. out. println ( s2. toLowerCase ( ) ) ;
System. out. println ( s2. toUpperCase ( ) ) ;
s2 = " abc " ;
System. out. println ( s2. trim ( ) ) ;
System. out. println ( String. valueOf ( 100 ) + 1 ) ;
s2= "a|b|c|d|e|f" ;
String[ ] strs = s2. split ( "\\|" ) ;
System. out. println ( Arrays. toString ( strs) ) ;
}
}
二、StringBuilder
a,創建對象
StringBuilder() 構造一個不帶任何字符的字符串生成器,其初始容量爲 16 個字符。
b,常用方法
StringBuilder append(String str) 將指定的字符串追加到此字符序列。
c,測試:測試字符串連接效率
public class Test2_StringBuilder {
public static void main ( String[ ] args) {
method1 ( ) ;
}
private static void method1 ( ) {
String str = "abcdefghijklmnopqrstuvwxyz" ;
StringBuffer sb = new StringBuffer ( ) ;
long start = System. currentTimeMillis ( ) ;
for ( int i = 0 ; i < 10000 ; i++ ) {
sb. append ( str) ;
}
long end = System. currentTimeMillis ( ) ;
System. out. println ( end- start) ;
}
private static void method ( ) {
String str = "abcdefghijklmnopqrstuvwxyz" ;
String result = "" ;
long start = System. currentTimeMillis ( ) ;
for ( int i = 0 ; i < 10000 ; i++ ) {
result = result+ str ;
}
long end = System. currentTimeMillis ( ) ;
System. out. println ( end- start) ;
}
}
三、包裝類
a,概念:
針對八大基本類型都有對應着的包裝類,目的是提供更完善的功能
– 八大基本類型: byte short int long float double char boolean
– 包裝類: Byte Short Integer Long Float Double Character Boolean
b,包裝類的繼承結構:
-- 所有的數字類型的父類 叫Number
--源碼摘抄:public abstract class Number
-- byte byteValue() 以 byte 形式返回指定的數值。
--abstract double doubleValue() 以 double 形式返回指定的數值。
--abstract float floatValue() 以 float 形式返回指定的數值。
--abstract int intValue() 以 int 形式返回指定的數值。
--abstract long longValue() 以 long 形式返回指定的數值。
--short shortValue() 以 short 形式返回指定的數值。
c,Integer
--源碼摘抄:public final class Integer
--創建對象:
--Integer(int value) 構造一個新分配的 Integer 對象,它表示指定的 int 值。
--常用方法:
--int intValue() 以 int 類型返回該 Integer 的值。
--static int parseInt(String s)將字符串參數作爲有符號的十進制整數進行解析。
--static Integer valueOf(int i)返回一個表示指定的 int 值的 Integer 實例。
測試:
public class Test3_Number {
public static void main ( String[ ] args) {
Integer i = new Integer ( 10 ) ;
Integer i2 = Integer. valueOf ( 10 ) ;
Integer i3 = Integer. valueOf ( 10 ) ;
System. out. println ( i2== i3 ) ;
System. out. println ( i == i3 ) ;
System. out. println ( i2. MAX_VALUE) ;
System. out. println ( i2. intValue ( ) ) ;
int sum = Integer. parseInt ( "100" ) ;
System. out. println ( sum ) ;
}
}
d,Double
-- 就是基本類型double 對應的包裝類型,用來爲double 提供更豐富的功能。
-- 創建對象:
-- Double ( double value) 構造一個新分配的 Double 對象,它表示基本的 double 參數。
-- static Double valueOf ( double d) 返回表示指定的 double 值的 Double 實例。
-- 常用方法:
-- double doubleValue ( ) 返回此 Double 對象的 double 值。
-- static double parseDouble ( String s) 返回一個新的 double 值
-- 測試:
package cn. tedu. number;
public class Test3_Number {
public static void main ( String[ ] args) {
Integer i = new Integer ( 10 ) ;
Integer i2 = Integer. valueOf ( 10 ) ;
Integer i3 = Integer. valueOf ( 10 ) ;
System. out. println ( i2== i3 ) ;
System. out. println ( i == i3 ) ;
System. out. println ( i2. MAX_VALUE) ;
System. out. println ( i2. intValue ( ) ) ;
int sum = Integer. parseInt ( "100" ) ;
System. out. println ( sum ) ;
Double d = new Double ( 3.14 ) ;
Double d2 = Double. valueOf ( 3.14 ) ;
System. out. println ( d2. MAX_VALUE) ;
System. out. println ( d2. doubleValue ( ) ) ;
double count = Double. parseDouble ( "3.14" ) ;
System. out. println ( count) ;
}
}
四、日期類Date
a,概念
b,創建對象
Date()分配 Date 對象並初始化此對象,以表示分配它的時間(精確到毫秒)。
c,測試
public class Test4_Date {
public static void main ( String[ ] args) {
Date date = new Date ( ) ;
System. out. println ( date. getDate ( ) ) ;
System. out. println ( date. getDay ( ) ) ;
System. out. println ( date. getHours ( ) ) ;
System. out. println ( date. getMonth ( ) ) ;
System. out. println ( date. getMinutes ( ) ) ;
System. out. println ( date. getSeconds ( ) ) ;
System. out. println ( date. getTime ( ) ) ;
System. out. println ( date. getYear ( ) ) ;
System. out. println ( date. toLocaleString ( ) ) ;
System. out. println ( date. hashCode ( ) ) ;
}
}
五、日期工具類SimpleDateFormat
a,概念
專門用來完成 Date類型的日期 和 String類型的日期 之間的轉換
b,創建對象
SimpleDateFormat(String pattern)用給定的模式和默認語言環境的日期格式符號構造 SimpleDateFormat。
c,常用方法
format(Date):把Date格式化成字符串
parse(String):把String解析成Date
d,計算你存活的天數:
public class Test5_SimpleDate {
public static void main ( String[ ] args) throws ParseException {
String birthday = new Scanner ( System. in) . nextLine ( ) ;
SimpleDateFormat sdf = new SimpleDateFormat ( "yyyy-MM-dd" ) ;
Date birth = sdf. parse ( birthday) ;
long start = birth. getTime ( ) ;
long now = System. currentTimeMillis ( ) ;
System. out. println ( ( now- start) / 1000 / 60 / 60 / 24 ) ;
}
}