包装类
测试包装类
package cn.yishan.test;
public class TestWrappedClass {
public static void main(String[] args) {
//基本数据类型转成包装类对象
Integer a = new Integer(3);
Integer b = Integer.valueOf(30);
//把包装类对象转成基本数据类型
int c = b.intValue();
double d = b.doubleValue();
//把字符串转成包装类对象
Integer e = new Integer("999");
Integer f = Integer.parseInt("666666");
//把包装类对象转成字符串
String str = f.toString();
//常见的常量
System.out.println("int类型最大的整数:"+Integer.MAX_VALUE);
}
}
自动装箱和拆箱(jdk1.5之后)
测试自动装箱、自动拆箱
package cn.yishan.test;
public class TestAutoBox {
public static void main(String[] args) {
Integer a = 100; //自动装箱:Integer a = Integer.valueOf(100);
int b = a ; //自动拆箱:编译器会修改成:int b = a.intValue();
Integer c = null;
if(c != null){
int d = c; //自动拆箱:调用了:c.intValue()
}
}
}
缓存问题
测试自动装箱、自动拆箱
package cn.yishan.test;
public class TestAutoBox {
public static void main(String[] args) {
Integer a = 100; //自动装箱:Integer a = Integer.valueOf(100);
int b = a ; //自动拆箱:编译器会修改成:int b = a.intValue();
Integer c = null;
// if(c != null){
// int d = c; //自动拆箱:调用了:c.intValue()
// }
//缓存[-128,127]之间的数字。实际就是系统初始的时候,创建了[-128,127]之间的一个缓存数组。
//当我们调用valueOf()的时候,首先检查是否在[-128,127]之间,如果在这个范围则直接从缓存数组中拿出已经创建好的对象
//如果不在这个范围,则创建新的Integer对象。
Integer in1 = Integer.valueOf(-128);
Integer in2 = -128;
System.out.println(in1 == in2 );//true因为123在缓存范围内
System.out.println(in1.equals(in2));//true
Integer in3 = 1234;
Integer in4 = 1234;
System.out.println(in3 == in4);//false:因为1234不在缓存范围内
System.out.println(in3.equals(in4));//true
}
}
String
- 字符串内容全部存储到value[]数组中,而变量value是final类型的,也就是常量(即只能被赋值一次)。这就是“不可变对象”的典型定义方式
private final char value[];
测试String
package cn.yishan.test;
public class TestString {
public static void main(String[] args) { //String str = "123456"; //String str2 = str.substring(2,5); //System.out.println(str); //System.out.println(str2); //编译器做了优化,直接在编译的时候将字符串进行拼接 String str1 = "hello" + " java"; String str2 = "hello java"; System.out.println(str1 == str2);//true String str3 = "hello"; String str4 = " java"; //编译的时候不知道变量中存储的是什么,所以没有办法在编译的时候进行优化 String str5 = str3 + str4; System.out.println(str2 == str5);//false //做字符串比较的时候,使用equals 不要使用== System.out.println(str2.equals(str5));//true }
}
StringBuilder、StringBuffer
测试StringBuilder、StringBuffer可变字符序列
package cn.yishan.test;
public class TestStringBuilder {
public static void main(String[] args) { String str; //StringBuilder线程不安全,效率高(一般使用它);StringBuffer线程安全,效率低。 StringBuilder sb = new StringBuilder("abcdefg"); System.out.println(Integer.toHexString(sb.hashCode())); System.out.println(sb); sb.setCharAt(2, 'M'); System.out.println(Integer.toHexString(sb.hashCode())); System.out.println(sb); }
}
测试StringBuilder、StringBuffer可变字符序列的常用方法
package cn.yishan.test;
public class TestStringBuilder2 {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder();
for(int i = 0 ;i<26;i++){
char temp = (char)('a'+i);
sb.append(temp);
}
System.out.println(sb);
sb.reverse();//倒序
System.out.println(sb);
sb.setCharAt(3,'高');//修改
System.out.println(sb);
//链式调用。核心就是:该方法调用了 return this,把自己返回了
sb.insert(0,'我').insert(6,'爱');//添加
System.out.println(sb);
sb.delete(20,23);//删除
System.out.println(sb);
}
}
测试可变字符序列和不可变字符序列的陷阱(字符串循环累加一定要使用StringBuilder)
package cn.yishan.test;
public class TestStringBuilder3 {
public static void main(String[] args) {
//使用String进行字符串的拼接
String str8 = " ";
for (int i = 0; i < 5000;i++ ){
str8 = str8 + i;//相当于产生了1000个对象,耗时费内存,工作中一定不能出现此种代码
}
//使用StringBuilder进行字符串的拼接
StringBuilder sb1 = new StringBuilder();
for (int i = 0; i < 5000;i++){
sb1.append(i);//建议使用此种方式
}
}
}
时间处理相关类(date)
Date类的构造方法
1 | package com.yishan.date; |
Date的成员方法
1 | package com.yishan.date; |
SimpleDateFormat类
可以对Date对象,进行格式化和解析
格式化
- 格式化Date对象:Date的成员方法:format();
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package com.yishan.date;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* @author : yishan
* @date : 2021-01-02 19:36
*/
public class SimpleDateFormatDemo01 {
public static void main(String[] args) {
//创建Date对象
Date date = new Date();
//创建一个时间格式
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss" );
//SimpleDateFormat中的成员方法format(),格式化时间
String format = sdf.format(date);
System.out.println("format = " + format);
}
}
解析
1 | package com.yishan.date; |
JDK8时间日期类
JDK8时间日期类(添加一天)
1 | package com.yishan.date; |
JDK8中获取时间对象
1 | package com.yishan.date; |
JDK8中获取时间的每个值
1 | package com.yishan.date; |
LocalDateTime对象转换为 LocalDate对象 和 LocalTime对象
1 |
|
LocalDateTime 格式化和解析
1 | package com.yishan.date; |
JDK8 中增加或者减少时间的方法
···java
1 | package com.yishan.date; |
JDK8 时间类 修改时间
1 | package com.yishan.date; |
获取两个LocalDate对象的时间间隔(年,月,日)
1 | package com.yishan.date; |
获取两个LocalDateTime对象的时间间隔(秒,毫秒,纳秒)
1 | package com.yishan.date; |
Calendar日历类
Calendar类是一个抽象类,提供了关于日期计算的相关功能
测试日期类的使用
package cn.yishan.test;
import javax.xml.crypto.Data;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
public class TestCalendar {
public static void main(String[] args) {
//获得日期的相关元素
Calendar calendar = new GregorianCalendar(2999,10,9,22,10,50);
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONDAY);
//1-7表示对应的星期几。1是星期一 以此类推
int weekday = calendar.get(Calendar.DAY_OF_WEEK);
//几号
int day = calendar.get(Calendar.DATE);
System.out.println(year);
System.out.println(month);
//0-11表示对应的月份。0是1一月 以此类推
System.out.println(calendar);
System.out.println(weekday);
System.out.println(day);
//设置日期的相关元素
Calendar c2 = new GregorianCalendar();
c2.set(Calendar.YEAR,8012);
System.out.println(c2);
//日期的计算
Calendar c3 = new GregorianCalendar();
c3.add(Calendar.DATE,100);
System.out.println(c3);
//日期对象和时间对象的转化
Date d4 = c3.getTime();
Calendar c4 = new GregorianCalendar();
c4.setTime(new Date());
printCalendar(c4);
}
public static void printCalendar(Calendar c){
//打印:1918年2月3日 11:23:34 周三
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONDAY) + 1;
int date = c.get(Calendar.DAY_OF_MONTH);
int dayweek = c.get(Calendar.DAY_OF_WEEK) - 1;
String dayweek2 = dayweek==0?"日":dayweek+"";
int hour = c.get(Calendar.HOUR);
int minute = c.get(Calendar.MINUTE);
int second = c.get(Calendar.SECOND);
System.out.println(year+"年"+month+"月"+date+"日"+hour+"时"+minute+"分"+second+"秒"+" 周"+dayweek2);
}
}