异常的处理:
- catch块要先子类后父类,否则会报错
- 处理运行时异常,采用逻辑去合理规避同时辅助try-catch处理
- 对于不确定的代码,也可以加上try-catch,处理潜在的异常
- 尽量去处理异常,切忌只是简单的调用printStackTrace()去打印输出
- 具体如何处理异常,要根据不同的业务需求和异常类型去决定
- 尽量添加finally语句去释放占用的资源
String 字符串
- 通过String s1=”字符串1”;声明了一个字符串对象,s1存放了到字符串对象的引用。通过 s1 =”test”+s1;改变了字符串s1,其实质是创建了新的字符串,变量s1指向了新创建的字符串对象
- 一旦一个字符串在内存中创建,则这个字符串将不可改变,如果需要一个可以改变的字符串。我们可以使用StringBuffer或者StringBuilder
- 每次new 一个字符串就产生一个新的对象,即使连两个字符串的内容相同,使用“==”比较也为“false”,如果只需比较内容是否相同,应使用“equals()”方法
- 如果需要创建一个内容可以改变的字符串对象,应优先考虑使用StringBuilder类,因为其于StringBuffer相比没有实现线程安全功能,所以性能略高
Java的基本类型和包装类
1.转换
在JDK1.5引入自动装箱和拆箱机制后,包装类和基本类型之间的转换就更加轻松
装箱:
int i = 10;//定义一个int基本类型值
Integer x = new Integer(i);//手动装箱
Integer y = i;//自动装箱
拆箱:
Integer j = new Integer(8);//定义一个Integer包装类对象,值为8
int m = j.intValue();//手动拆箱int类型
int n = j;//自动拆箱int类型
2.与字符串之间的转化:
基本类型->字符串
int c = 10;
String str1 = Integer.toString(c);//方法一
String str2 = String.valueOf(c);//方法二
String str3 = c+"";//方法三
字符串->基本类型-
String str = "8";
int d = Integer.parseInt(str);
int e = Integer.valueOf(str);
3.使用Date和SimpleDateFormat类表示时间
Date d = new Date();//使用默认的构造方法创建的事实Date对象,代表当前时间
System.out.println(d);
1.使用format()方法将日期转换为指定格式的文本
Date d = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM--dd HH:mm:ss");
String today = sdf.format(d);
System.out.println(today);
2.使用parse()方法将文本转换为日期
String day = "2014年02月14日10:30:25";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
Date date = df.parse(day);
System.out.println(date);
4.Calendar的使用
Calendar rightNow = Calendar.getInstance();
获得年:rightNow.get(Calendar.YEAR);
月:rightNow.get(Calendar.MONTH);
日:rightNow.get(Calendar.DAY_OF_MONTH);
时:rightNow.get(Calendar.HOUR_OF_DAY);
分:rightNow.get(Calendar.MINUTE);
秒:rightNow.get(Calendar.SECOND);
Date date = rightNow.getTime();//转换
Long time = rightNow.getTimeInMillis();//获取当前毫秒数
5.使用Math类操作数据
/**
* abs求绝对值
*/
System.out.println(Math.abs(-10.4)); //10.4
System.out.println(Math.abs(10.1)); //10.1
/**
* ceil天花板的意思,就是返回大的值,注意一些特殊值
*/
System.out.println(Math.ceil(-10.1)); //-10.0
System.out.println(Math.ceil(10.7)); //11.0
System.out.println(Math.ceil(-0.7)); //-0.0
System.out.println(Math.ceil(0.0)); //0.0
System.out.println(Math.ceil(-0.0)); //-0.0
/**
* floor地板的意思,就是返回小的值
*/
System.out.println(Math.floor(-10.1)); //-11.0
System.out.println(Math.floor(10.7)); //10.0
System.out.println(Math.floor(-0.7)); //-1.0
System.out.println(Math.floor(0.0)); //0.0
System.out.println(Math.floor(-0.0)); //-0.0
/**
* max 两个中返回大的值,min和它相反,就不举例了
*/
System.out.println(Math.max(-10.1, -10)); //-10.0
System.out.println(Math.max(10.7, 10)); //10.7
System.out.println(Math.max(0.0, -0.0)); //0.0
/**
* random 取得一个大于或者等于0.0小于不等于1.0的随机数
*/
System.out.println(Math.random()); //0.08417657924317234
System.out.println(Math.random()); //0.43527904004403717
/**
* rint 四舍五入,返回double值
* 注意.5的时候会取偶数
*/
System.out.println(Math.rint(10.1)); //10.0
System.out.println(Math.rint(10.7)); //11.0
System.out.println(Math.rint(11.5)); //12.0
System.out.println(Math.rint(10.5)); //10.0
System.out.println(Math.rint(10.51)); //11.0
System.out.println(Math.rint(-10.5)); //-10.0
System.out.println(Math.rint(-11.5)); //-12.0
System.out.println(Math.rint(-10.51)); //-11.0
System.out.println(Math.rint(-10.6)); //-11.0
System.out.println(Math.rint(-10.2)); //-10.0
/**
* round 四舍五入,float时返回int值,double时返回long值
*/
System.out.println(Math.round(10.1)); //10
System.out.println(Math.round(10.7)); //11
System.out.println(Math.round(10.5)); //11
System.out.println(Math.round(10.51)); //11
System.out.println(Math.round(-10.5)); //-10
System.out.println(Math.round(-10.51)); //-11
System.out.println(Math.round(-10.6)); //-11
System.out.println(Math.round(-10.2)); //-10
Java中集合框架
- 进行排序(String是根据首字符排序
排列顺序:
数字:0–9
大写字母:A–Z
小写字母:a–z
)
public void testSort1(){
List<Integer> integerList = new ArrayList<Integer>();
//插入十个100以内的不重复随机数
Random random = new Random();
Integer k;
for(int i=0;i<10;i++){
do{
k=random.nextInt(100);
}while(integerList.contains(k));
integerList.add(k);
System.out.println("成功添加整数:"+k);
}
System.out.println("--------排序前----------");
for(Integer integer:integerList){
System.out.println("元素:"+integer);
}
Collections.sort(integerList);
System.out.println("--------排序后----------");
for(Integer integer:integerList){
System.out.println("元素:"+integer);
}
}