JAVA_9


JAVA常用类和jdk源码

1. String类

  1. String类又称作不可变字符序列。
  2. String位于java.lang包中,Java程序默认导入java.lang包下的所有类。
  3. Java字符串就是Unicode字符序列,例如字符串“Java就是4个Unicode字符’J’、’a’组成的。
  4. Java没有内置的字符串类型,而是在标准Java类库中提供了一预定义的类String,每个用双引号括起来的字符串都是String类的一个实例。

2. StringBuffer和StringBuilder

  1. String:不可变字符序列。
  2. StringBuffer和StringBuilder:可变字符序列,方法一一样。
  3. StringBuffer:线程安全,做线程同步检查,效率较低。
  4. StringBuilder:线程不安全,不做线程同步检查,因此效率较高。建议采用该类。
  5. 用String做字符串拼接时等于每次创建一个新的String对象占用内存多耗时长,StringBuilder不会所以占用内存少且时间很短。

3.包装类

  1. JAVA是面向对象的语言,但并不是“纯面向对象”。他不像Python,一切都是对象。java中,基本数据类型不是对象。这时候,我们可以用包装类把基本数据类型变成对象。
基本数据类型包装类
byteByte
booleanBoolean
shortShort
charCharacter
intInteger
longLong
floatFloat
doubleDouble
//基本数据类型转化成Integer对象
Integer int1 = Integer.value0f(100)
//包装类对象转成基本数据类型
int int2 = int1.intValue()
long long1 = int1.longValue();
//字符串转成Integer对象
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);
  1. 自动装箱和自动拆箱
//自动装箱。编译器添加:Integera=Integer.value0f(100)
Integer a = 100;
//自动拆箱。编译器添加:intb=a.intValue();
int b = a;

//空指针异常
Integerc = null;
int d =c;//自动拆箱。编译器添加:intd=c.intValue();会报错

//整型、char类型所对应的包装类。在自动装箱时,对于-128到127之间的值会进行缓存的处理,为了提高效率。
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.时间转化字符串

  1. Date类
/**
* 测试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);//距离1970年150年后的日期,忽略闰月
System.out.println(d2);
Date d3 = new Date();
d3 .getDate();//方法被废弃。可以用,但是不建议使用
  1. DateFormat类
/**
*测试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));
  1. 日历类
//月份:0-11。0:1月,1:2月,...,11:12月
//星期:1-7.1:周日,2:周一,... 7:周六
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());//返回对应的Date对象
System.out.println(calendar.getTimeInMillis());//返回对应的毫秒数

//日期的计算
calendar.add(Calendar.DATE, 1000);//加了1000天
System.out.println(calendar.getTime());
calendar.add(Calendar.YEAR,amount: -30);//往前30年
System.out.println(calendar.getTime());

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值