耐心是一切聪明才智的基础。
String 相关
- 本质
final char[]
,不可修改,线程安全。
每次赋值都会有重新获取内存,赋值,gc旧内存的开销。
final
修饰类,很多方法native方法,如果可以被继承重写,不安全。 - 赋值
String str=“i" | “a” + “b” | intern() //方法区-常量池,可以共享。
String s = new String(i) | “dfd” + e // 堆中分配
-
StringBuffer
和StringBuilder
。char[]
没有final
可变字符串,修改开销比较小。最终是native方法实现的,不是java字节码实现的。
public static native void arraycopy(Object src, int srcPos,
Object dest, int destPos,
int length);
-
StringBuffer
线程安全 -
StringBuilder
不安全 -
String
最大多少
https://mp.weixin.qq.com/s/Kp5u4ighoME72GchAKvRcg
char[int] int有正负 大小大致是4个字节的一半
但是jvm虚拟机有 规范 String大小占2个字节 还有一个结束位置 63335-1
序列化
https://www.cnblogs.com/xdp-gacl/p/3777987.html
https://mp.weixin.qq.com/s/pQH4iCtNJeQStwfDkw4Q9g
https://www.cnblogs.com/wangg-mail/p/4354709.html
-
serialVersionUID
不能继承都得写。
不写的话就自动添加一个,发生修改,UID就会发生修改。 - 父类实现
Serializable
接口,子类就不用了。 -
static transient
修饰的不能序列化。
异常
https://mp.weixin.qq.com/s/-_ZSVvezckryAKFUp1Pp_w
- Java 7 多个异常捕获用 | 不能用||
catch(IOException | SQLException ex){
logger.error(ex);
throw new MyException(ex.getMessage());
}
方法修饰符
defalut
同包可以,不同包子类也可以。
protected
同包可以。
list和array之间转换
//不推荐:Arrays.asList,int[]不支持,转换之后执行添加删除操作会抛异常。
//推荐: Arrays.stream(list)
List<Integer> collect1 = Arrays.stream(list).boxed().collect(Collectors.toList());
collect1.add(2);
List<String> collect2 = Arrays.stream(list1).collect(Collectors.toList());
List<String> strings = Arrays.asList(list1);
collect2.add("dfd");
strings.add("23");//会抛出UnsupportedOperationException异常
//抛异常ClassCastException
String objects1[] = (String[]) collect2.toArray();
//正确用法
String[] objects1 = collect2.toArray(new String[0]);
网友评论