JavaSE面试题整理
Object类
- Object类自带哪些方法?
- hashCode()
hashCode相等,不一定equals
equals时,hashCode一定相等
如果不重写hashcode(),在HashSet中添加两个equals的对象,会将两个对象都加入进去。 - toString()
- equals(Object obj)
- clone()
实现对象的浅复制 - finalize()
垃圾回收机制 - getClass()
getClass()经常用于java反射机制 - 5个线程方法
notify() notifyAll() 3个wait()
String类
- 构造方法
String()
String(String value)
String(byte[] bytes, int offset, int length)
String(byte[] bytes, String charsetName)
String(byte[] bytes, int offset, int length, String charsetName) //指定字符集
String(char[] value)
String(char[] value, int offset, int count)
String(StringBuffer buffer)
String(StringBuilder builder)
- 方法
- isEmpty()
- length()
- charAt(int index)
返回指定索引的字符值 - indexOf()
indexOf(int ch)
indexOf(String str)
indexOf(int ch, int fromIndex)
indexOf(String str, int fromIndex)
- lastIndexOf()
lastIndexOf(int ch)
lastIndexOf(String str)
lastIndexOf(int ch, int fromIndex)
lastIndexOf(String str, int fromIndex)
字符串加工
- concat(String str)
将指定字符串连接到字符串结尾 - subString(int beginIndex)
-subString(int beginIndex, int endIndex)
字符串切割 - toUpperCase()
全大写
-全小写 LowerCase() - compareTo(String anotherString)
- equalsIgnoreCase(String anotherString)
- trim()
- valueOf()
int,long,float,double,char,char数组,Object转换成String的方法 - getBytes()
把String字符串转换为字节数组
-byte[] getBytes(String charsetName) - replace(char oldChar, char newChar)
与正则表达式相关 - matches(String regex)
- split(String regex)
切割得到的字符串数组不包含作为分割的字符
- String、StringBuffer、StringBuilder的区别
- String 是不可变的对象, 因此在每次对 String 类型进行改变的时候其实都等同于生成了一个新的String 对象,然后将指针指向新的 String 对象,所以经常改变内容的字符串最好不要用 String类型 ,因为每次生成对象都会对系统性能产生影响。
- StringBuffer 和 StringBuilder 二者都继承了 AbstractStringBuilder ,底层都是利用可修改的char数组(JDK 9 以后是 byte数组)。
| String | StringBuffer | StringBuilder |
|---|---|---|
| 可以赋空值 | 不可以赋空值 | 不可以赋空值 |
| 不可变 | 可变 | 可变 |
| 线程安全 | 线程安全 | 线程不安全 |
| 速度●○○ | 速度●●○ | 速度 ●●● |
- StringBuffer、StringBuilder 类只对字符串对象本身进行操作,而不是生成新的对象,所以速度快。且StringBuffer方法synchronized修饰(方法加锁),所以执行较慢。
Collection集合

- collection和collections的区别
- java.util.Collection 是一个集合接口(集合类的一个顶级接口)。

2万+

被折叠的 条评论
为什么被折叠?



