一、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 ) ;
}
}