字符串
理解:字符串为不可改变的量,一般存储在方法区中方便内存的优化
用法:
- 用=直接赋值 ,结果同一储存在方法区中,不用开辟新的空间
String a= 10;
String str = "abc"+"d";
- 使用运算符运算结果,直接在堆中直接开辟空间,地址不同 必须得用equals才能判断其中的内容是否相等
String a = "abcd";
String b ="abc"+"d";
a.eqauls(b);
- 总:字符串为不可改变的量,存在方法区中,
- 推荐省略的形式,避免创建多个对象
- 变量操作运算,都会在堆中开辟对象
- 推荐使用,访问常量池的地址intern() 一般来说,字符串使用常量池
String msg2 ="abcd";
**split **
截取字符串,结果储存在一个数组中,类似于从中间截取2断
String msg ="laope@sxt.con";
String [] arr = msg.split("@");
System.out.println(arr[0]);
System.out.println(arr[1]);
包装类
理解:基本类型装换成引用类型
基本类型 引用类型
int Integer
double Double
char Character
boolean Boolean
byte Byte
short Short
long Long
float Float
装箱
int -> Integer 通过其中的valueof() 来转换
int num=5;
Integer numInt= Integer.valueOf(num);
int ->String //在运算时带有字符串
String ->Integer
numInt=Integer.valuesOf("5555");
拆箱
把引用类型数据转化为基本类型数据,通过intValueI()方法实现
//自动装箱
Integer numInt = 5; //Integer.valueOf(5)
//自动拆箱
int num = numInt; //numInt.intValue()
在这里要注意一下拆箱失败(空指针异常)
//拆箱失败 编译蜜糖
Integer numInt2 = null;
num = numInt2.intValue(); //拆箱 java.lang.NullPointerException
IntegerCache: 缓存 [-128,127] 整数对象放到缓存池中,不需要每次new. .
含义:常用的放入池中,不常用用的时候再创建
日期
理解:在计算机中日期一般用长整型来表示
jdk8之前
-
Date : new Date() getTime() setTime()
-
SimpleDateFormat: yyyy-MM-dd HH:mm:ss
-
1)、format(): 格式化为字符串
2)、parse(): 转成日期
jdk1.8之后
LocalDateTime now = LocalDateTime.now();
DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String nowStr = now.format(format); //格式化日期字符串
System.out.println(nowStr);
//转成日期
String dateStr= "2020-01-11 11:23:04";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime date= LocalDateTime.parse(dateStr, formatter);
System.out.println(date.getDayOfWeek());
枚举
常量的集合,比如星期,月份啊,游戏之类的
用法:定义类为
enum 常量数据类型且不可修改
1)、继承体系
2)、构造器:
有: new
不让用: 不需要的对象 | 有方法返回对象
3)、方法: 是否为静态 方法签名(名称+类型)+返回值 作用