您的位置:首页 > 编程语言 > Java开发

Java中的String字符串,异常处理,和泛型

2016-07-04 21:03 411 查看
Java中的字符串用String的话,是不能改变的,用StringBuffer的话,可以改变长度
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);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: