1、异常。
——Throwable是鼻祖,下分Error和Exception两类。
An Error is a subclass of Throwable that indicates serious problems that a reasonable application should not try to catch.
包含的子类有:
AnnotationFormatError, AssertionError, AWTError, CoderMalfunctionError, FactoryConfigurationError, FactoryConfigurationError, IOError, LinkageError, SchemaFactoryConfigurationError, ServiceConfigurationError, ThreadDeath, TransformerFactoryConfigurationError, VirtualMachineError
The class Exception and its subclasses are a form of Throwable that indicates conditions that a reasonable application might want to catch.
包含的常见子类有:
IOException, RuntimeException
如果客户端可以通过其他的方法恢复异常,那么这种异常就是checked exception;如果客户端对出现的这种异常无能为力,那么这种异常就是Unchecked exception;从使用上讲,当异常出现的时候要做一些试图恢复它的动作而不要仅仅的打印它的信息
详情见:unchecked Exception & checked Exception
2、捕捉异常的语句是try{} catch(Exception e){} finally{}。这里的catch在python里是except,需要适应。异常的信息可以通过e.printStackTrace()打印输出。
public class Hello {
public static void main(String[] args){
int a = 10;
int b = 0;
try {
int c = a/b;
}catch (ArithmeticException e){
System.out.println("这是出现异常了!");
//e.getMessage()获得错误信息 e.toString()获得异常种类和错误信息
System.out.println(e.toString());
//直接利用这个语句输出异常信息
e.printStackTrace();
}finally {
System.out.println("这是到了finally了!");
}
}
}
3、字符串String类具有不可变性,如果需要可变值的字符串类,那么用StringBuilder类代替。
4、包装类。一个包装类通常对应着一种数据类型,比如Integer对应着int。Java目前引入了自动装箱和拆箱机制,也就是自动转换,不需要我们强制声明转换,如:
public class Hello {
public static void main(String[] args){
int a = 10;
// 基本数据类型变成包装类叫自动装箱,如下。反过来叫自动拆箱。
Integer b = a;
String c = b.toString();
System.out.println(c.getClass()); //class java.lang.String
String d = String.valueOf(a);
System.out.println(d.getClass()); //class java.lang.String
Integer e = Integer.valueOf(d);
System.out.println(e.getClass()); //class java.lang.Integer
}
}
5、字符串类和基本数据类型之间的转换比较常见,一般采用类的valueOf()方法,或者toString()方法。如上代码。
6、日期以及格式化的处理。
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class Hello {
public static void main(String[] args) throws ParseException {
//获取一个Calendar实例
Calendar c = Calendar.getInstance();
System.out.println("现在的年份是:"+c.get(Calendar.YEAR));
System.out.println("现在的分钟是:"+c.get(Calendar.MINUTE));
//获取现在的时间,类似于Date,所以可以用Calendar代替Date使用
System.out.println("现在的Date是:"+c.getTime());
System.out.println("现在的毫秒是:"+c.getTimeInMillis());
//时间的格式转换(format)
Date d = c.getTime();
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(sdf1.format(d));
//字符串格式化成时间(parse)
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
String dStr = "2016年11月25日 12:23:33";
//调用 SimpleDateFormat 对象的 parse() 方法时可能会出现转换异常,即 ParseException ,因此需要进行异常处理
try{
Date dDate = sdf2.parse(dStr);
System.out.println(dDate.getClass());
}catch (Exception e){
System.out.println("字符串转日期格式错误,出现异常!");
}
}
}
7、Math类里方法的使用。因为都是静态方法,所以直接用Math.xxx()使用。
public class Hello {
public static void main(String[] args){
int[] ages = new int[5];
for (int i = 0; i < 5; i++){
double d = Math.random();
ages[i] = (int)Math.ceil(d*100);
}
for(int i : ages){
System.out.println(i);
}
}
}
8、最重要的恐怕就是集合的知识点了。集合分为两派Collection和Map,都是接口,而它们各自都有子接口,常用的就是Collection下的ArrayList(List)和HashSet(Set)、Map下的HashMap。
——这里面需要理解为什么有了数组Array后,还要有集合。最重要的区别在于:1、数组长度是固定的不可变,这一点十分不方便;2、数组的下标都是整数,键值对的形式是在集合里面的,类似于字典;3、数组里面只能放同一类型的元素,集合可以杂着放。
数组序列的使用方法:
import java.util.ArrayList;
/**
* Created by Andy on 16/9/2.
*/
public class Hello {
public static void main(String[] args){
ArrayList l = new ArrayList();
System.out.println("数组序列的长度为:"+l.size());
l.add("Hello");
l.add(10);
System.out.println("数组序列的长度为:"+l.size());
for(Object o : l){
System.out.println(o);
}
l.clear();
System.out.println("数组序列的长度为:"+l.size());
}
}
9、这里需要注意ArrayList、HashSet和HashMap的添加、删除、修改等方法,包括add、remove、put、set、contains、equals、indexOf、lastIndexOf、containsKey、containsValue、Collections.sort()、。
——需要注意List中的contains原理是比较equals函数,而Set中的contains原理是先比较Hashcode然后再比较equals。
——进行比较的时候,如果是字符串这些都好说,如果是自定义的一个类,那么需要实现Comparable或者Comparator接口。
具体这些ArrayList、HashSet和HashMap的应用在实战中继续巩固。