JAVA常用类和jdk源码
1. String类
String类又称作不可变字符序列。 String位于java.lang包中,Java程序默认导入java.lang包下的所有类。 Java字符串就是Unicode字符序列,例如字符串“Java就是4个Unicode字符’J’、’a’组成的。 Java没有内置的字符串类型,而是在标准Java类库中提供了一预定义的类String,每个用双引号括起来的字符串都是String类的一个实例。
2. StringBuffer和StringBuilder
String:不可变字符序列。 StringBuffer和StringBuilder:可变字符序列,方法一一样。 StringBuffer:线程安全,做线程同步检查,效率较低。 StringBuilder:线程不安全,不做线程同步检查,因此效率较高。建议采用该类。 用String做字符串拼接时等于每次创建一个新的String对象占用内存多耗时长,StringBuilder不会所以占用内存少且时间很短。
3.包装类
JAVA是面向对象的语言,但并不是“纯面向对象”。他不像Python,一切都是对象。java中,基本数据类型不是对象。这时候,我们可以用包装类把基本数据类型变成对象。
基本数据类型 包装类 byte Byte boolean Boolean short Short char Character int Integer long Long float Float double Double
Integer int1 = Integer . value0f ( 100 )
int int2 = int1. intValue ( )
long long1 = int1. longValue ( ) ;
Integer int3 = Integer . parseInt ( "324" ) ;
System . out. println ( int3) ;
System . out. println ( int3. tostring ( ) ) ;
System . out. println ( "" + int3) ;
System . out. println ( "int能表示的最大整数:" + Integer . MAX_VALUE ) ;
自动装箱和自动拆箱
Integer a = 100 ;
int b = a;
Integerc = null ;
int d = c;
Integer a= Integer . value0f ( 100 ) ;
Integer b = 100 ;
System . out. println ( a== b) ;
Integer c= 300 ;
Integer d= 300 ;
System . out. println ( c== d) ;
4.时间转化字符串
Date类
long nowNum = System . currentTimeMillis ( ) ;
System . out. println ( nowNum) ;
Date d1 = new Date ;
System . out. println ( d1. getTime ( ) ) ;
Date d2 = new Date ( 1000L * 3600 * 24 * 365 * 150 ) ;
System . out. println ( d2) ;
Date d3 = new Date ( ) ;
d3 . getDate ( ) ;
DateFormat类
DateFormat df = new SimpleDateFormat ( pattern: "yyyy-MM-dd hh:mm:ss" ) ;
Date d2 = new Date ( 1000L * 3600 * 24 * 365 * 150 ) ;
String str2 = df. format ( d2) ;
System . out. println ( str2) ;
String str3 = "2049-10-1 10:10:20" ;
Date d3 = df. parse ( str3) ;
System . out. println ( d3. getTime ( ) )
DateFormat df2 = new SimpleDateFormat ( pattern: "yyyy年MM月dd日" ) ;
DateFormat df3 = new SimpleDateFormat ( pattern: "D" ) ;
Date date3 = new Date ( ) ;
System . out. println ( df3. format ( date3) ) ;
日历类
Calendar calendar = new GregorianCalendar ( year: 2999 , month: 9 , dayofMonth: 10 , hourofDay: 11 , minute: 25 , second: 40 ) ;
int year = calendar. get ( Calendar . YEAR ) ;
int month = calendar. get ( Calendar . MONTH ) ;
System . out. println ( year) ;
System . out. println ( month) ;
calendar. set ( Calendar . YEAR , 2049 ) ;
System . out. println ( calendar. get ( Calendar . YEAR ) ) ;
System . out. println ( calendar. getTime ( ) ) ;
System . out. println ( calendar. getTimeInMillis ( ) ) ;
calendar. add ( Calendar . DATE , 1000 ) ;
System . out. println ( calendar. getTime ( ) ) ;
calendar. add ( Calendar . YEAR , amount: - 30 ) ;
System . out. println ( calendar. getTime ( ) ) ;