Java中的字符串用String的话,是不能改变的,用StringBuffer的话,可以改变长度
常见的异常
throw关键字
自定义异常
构造函数中使用泛型
这里info是个泛型
泛型数组和泛型函数可以配套使用
StringBuffer sb = new StringBuffer();
sb.append("jke");
StringBuilder的使用 可变的字符序列 比stringBuffer块很多在StringBuffer的基础上加载而来
StringBuilder a = new StringBuilder();
常见的有插入insert 追加append 等
字符串转换成数组 toCharArray
过滤字符串中存在的字符indexOf 返回bool类型 有就返回位置 否则返回-1
从字符串中取出指定的字符 charAt
异常处理
finally是一定会执行的代码
try{
throw new Myexception("自定义异常");
}catch(Myexception e){
System.out.print(e);
}
常见的异常
用throws关键字 表示此方法不处理异常
public static void tell(int i ,int j) throws Exception{
int temp = 0;
temp = i / j;
System.out.println(temp);
}
在主函数中
try{
tell(10,10);
}
catch(Exception e){
System.out.println(2);
}finally{
}
throw关键字
try{
throw new Myexception("自定义异常");
}catch(Myexception e){
System.out.print(e);
}
自定义异常
只要类继承自异常即可
class Myexception extends Exception{
public Myexception(String msg){
super(msg);
}
}
public class newDemo extends Exception{
public static void main(String args[]){
try{
throw new Myexception("自定义异常");
}catch(Myexception e){
System.out.print(e);
}
}
}
泛型
定义泛型
class Can<T>{
private T value;
public T getValue() {
return value;
}
public void setValue(T value) {
this.value = value;
}
//声明构造方法
public Can(T value){
this.value = value;
}
}
Can<String> s = new Can<String>("cao");
构造函数中使用泛型
//声明构造方法
public Can(T value){
this.value = value;
}
通配符
在函数参数中用?来代替object即可
//通配符的概念 不论什么类型都可以使用
public static void tell(info<?> i){
System.out.println(i);
}
info<String> a = new info<String>();
a.setKey("cao");
tell(a);
这里info是个泛型
泛型接口
interface Gen<T>{
public void say();
}
implements Gen<String
泛型数组和泛型函数可以配套使用
//泛型数组的使用
public static <T>void hahaha(T arr[]){
}
}
这里的void是函数的返回类型
g.hahaha(a);