JavaSE.20190506.异常.常用类.API(部分常见).基本数据类型的包装类.Math类.Date类.枚举.

一.异常

     1.异常:
                     Throwable类
                     /        \
                 Error        Exception
                 |                |        \
            Unchecked        Checked        Runtime
            Exception        Exception    Exception  
      2.Error:这类错误不需要程序员管理
         Exception:异常
         检查时异常|编译时异常:如果程序一旦出现检查时异常,程序必须要经过处理,否则无法运行
         运行时异常:增强程序的健壮性就可以处理
         一般运行时异常都会直接或者间接的继承自RuntimeException      
      3.常见的运行时异常:
         1)空指针  NullPointerException
         2)数组越界异常 ArrayIndexOutOfBoundsException
         3)负数异常|数组的长度为负数异常 NegativeArraySizeException
         4)数学异常  ArithmeticException
         5)类型转换异常  ClassCastException
         6)数字转换异常  NumberFormatException

public class ExceptionDemo01 {
	public static void main(String[] args) {
		/*1.空指针  NullPointerException
		String s=null;
		//增强程序的健壮性
		if(s!=null){
			s.length();
		}else{
			System.out.println("s是空的");
		}*/
		
		//2.数组越界  ArrayIndexOutOfBoundsException
		//int[] arr=new int[3];
		//System.out.println(arr[3]);
		
		//3.负数异常  NegativeArraySizeException
		//int[] arr=new int[-3];
		
		//4.数学异常|分母为0异常
		//System.out.println(2/0);
		
		//5.类型转换异常 ClassCastException
		/*Person p =new Student();
		Teacher t=(Teacher)p;*/
		
		//6.数字转换异常 NumberFormatException
		String s="123abc";
		System.out.println(Integer.valueOf(s));
		
		//编译时异常
		//InputStream is=new FileInputStream("D://test.txt");
	}
	
}

      4.throw 制造异常
         throws 抛出异常
         捕获异常 try..catch
                    try {
                       可能会出现异常的代码;
                    } catch (FileNotFoundException e) {
                      如果出现对应的异常执行的代码
                    } catch (NullPointerException e){
                    } catch (Exception e){
                    } finally{
                      无论是否出现异常,一定会执行的代码
                    }
         注意:
            1)如果try中的代码出现异常,下面的代码不会执行,直接执行对应的catch中的代码
            2)一个try至少存在一个或者多个catch
            3)catch中捕获异常的顺序从小到大写

       5.自定义异常类: 
          要直接或者间接继承自Exception或者它的子类;

二.常用类 (即常常会使用的类)
     1.String 不可变长字符串|字符序列
     2.StringBuilder:可变长字符串 ,线程不安全,效率较高
     3.StringBuffer:可变长字符串 ,线程安全的,效率较低
     4.学习类的API:
        String 
            作用:String 类代表字符串。Java 程序中的所有字符串字面值(如 "abc" )都作为此类的实例实现。
            构造器
            方法:
                成员方法
                静态方法

public class StringDemo01 {
	public static void main(String[] args) throws UnsupportedEncodingException {
		String str1="abc";  //一个对象  "abc" 字符串常量池
		String str2="abc";  
		
		//构造器
		String str3=new String(); //构建一个空字符串数组
		System.out.println(str3);
		
		//String(String original) 
		String str4=new String("yinwei");
		System.out.println(str4);
		
		//String(char[] value) 
		String str5=new String(new char[]{'a','b','c','d'});
		System.out.println(str5);
		System.out.println(str5.length());
		
		//String(char[] value, int offset, int count) 
		String str6=new String(new char[]{'a','b','c','d'},1,2);
		System.out.println(str6);
		
		//String(byte[] bytes) 通过使用平台的默认字符集解码指定的 byte 数组,构造一个新的 String。
		String str7=new String(new byte[]{66,67,68});
		System.out.println(str7);
		
		//String(byte[] bytes, int offset, int length) 通过使用平台的默认字符集解码指定的 byte 子数组,构造一个新的 String。
		String str8=new String(new byte[]{66,67,68},1,2);
		System.out.println(str8);
		
		String s="因为";
		byte[] bytes=s.getBytes("gbk");
		System.out.println(Arrays.toString(bytes));
		System.out.println(new String(bytes,"utf-8"));
	}
}

三.API (常用的几种)

      1.char charAt(int index)  返回指定索引处的 char 值。 
      2.int codePointAt(int index)  返回指定索引处的字符(Unicode 代码点)。
      3.int compareTo(String anotherString) 按字典顺序比较两个字符串。 
         相等为0 ,如果this比参数对象大返回整数,否则返回负数
      4.compareToIgnoreCase(String str) 按字典顺序比较两个字符串,不考虑大小写
      5.String concat(String str)   将指定字符串连接到此字符串的结尾。 
      6.boolean contains(CharSequence s)    当且仅当此字符串包含指定的 char 值序列时,返回 true。 
      7.static String copyValueOf(char[] data)  返回指定数组中表示该字符序列的 String。 
      8.boolean endsWith(String suffix) 测试此字符串是否以指定的后缀结束。 
      9.boolean startsWith(String prefix) 测试此字符串是否以指定的前缀开始。 
      10.byte[] getBytes()  字符串转字节数组
      11.int indexOf(String str)   返回指定子字符串在此字符串中第一次出现处的索引。 
      12.String replace(char oldChar, char newChar)返回一个新的字符串,它是通过用 newChar 替换此字符串中出现的所有                      oldChar 得到的。
      13.String[] split(String regex)根据给定正则表达式的匹配拆分此字符串。  
      14.String substring(int beginIndex) 返回一个新的字符串,它是此字符串的一个子字符串。 
      15.String substring(int beginIndex, int endIndex)返回一个新字符串,它是此字符串的一个子字符串。 结束位置索引获取不到
      16.char[] toCharArray() 将此字符串转换为一个新的字符数组。 
      17.String toLowerCase()  
      18.String toUpperCase() 使用默认语言环境的规则将此 String 中的所有字符都转换为大写。 
      19.String trim()返回字符串的副本,忽略前导空白和尾部空白。 
      20.static String valueOf(int i)   返回 int 参数的字符串表示形式。 

public class StringDemo02 {
	public static void main(String[] args) throws UnsupportedEncodingException {
		String str="shsxtverygood";
		String str2="Shsxtverygood";
		String str3=" name=李四";
		System.out.println("charAt():"+str.charAt(3)); //x
		System.out.println("codePointAt():"+str.codePointAt(3));  //120
		System.out.println("compareTo():"+str2.compareTo(str));  //-32
		System.out.println("compareToIgnoreCase():"+str.compareToIgnoreCase(str2));  //0
		System.out.println("concat():"+str.concat(str2));  //shsxtverygoodShsxtverygood
		System.out.println("contains():"+str.contains("sxt"));  //true
		System.out.println("copyValueOf():"+String.copyValueOf(new char[]{'s','x','t'}));  //sxt
		System.out.println("copyValueOf():"+String.copyValueOf(new char[]{'s','x','t'},1,2));  //xt
		System.out.println("endsWith():"+str.endsWith("good"));  //true
		System.out.println("startsWith():"+str.startsWith("shsxt"));  //true
		System.out.println("getBytes():"+str.getBytes("utf-8"));  //true
		System.out.println("indexOf():"+str.indexOf("o"));  //10
		System.out.println("lastIndexOf():"+str.lastIndexOf("o"));  //11
		System.out.println("replace():"+str.replace("oo","OO"));  //11
		System.out.println(str);  //11
		String[] arr=str3.split("=");
		System.out.println(Arrays.toString(arr));
		System.out.println(arr[1]);
		
		System.out.println("substring():"+str.substring(5));  //11
		System.out.println("substring():"+str.substring(5,8));  //11
		System.out.println(str.toCharArray());  //shsxtverygood
		System.out.println(str3.toUpperCase());  //shsxtverygood
		System.out.println("trim():"+str3.trim());  //shsxtverygood
		System.out.println("valueOf():"+String.valueOf(123).length());  

	}
}

四.基本数据类型的包装类

     1.基本数据类型                                     对应的包装类

              byte             --------------------->        Byte
              short            --------------------->        Short
              int                --------------------->        Integer
              long             --------------------->        Long
              char             --------------------->        Character
              float             --------------------->        Float
              double         --------------------->        Double
              boolean       --------------------->        Boolean

        自动装箱:基本数据类型-->包装类型
        自动拆箱:包装类型-->基本数据类型

五.Math 类

//Math数学类
import static java.lang.Math.max;
public class Math01 {
	public static void main(String[] args) {
		//static double ceil(double a) 向上取整 
		System.out.println(Math.ceil(-3.3));  //-3.0
		//static double floor(double a)  向下取整
		System.out.println(Math.floor(-3.3));  //-4.0
		
		/*
		 * static long max(long a, long b) 
		          返回两个 long 值中较大的一个。 
		   static double min(double a, double b) 
		          返回两个 double 值中较小的一个。 
		 */
		System.out.println(max(5, 7));
	}
}

六.Date 日期

     1.构造器:
        Date() 以当前系统时间构建日期对象
        Date(long date) 参数为毫秒数,默认从1970.1.1.0.0.0开始计算
        注意:导包  java.util.Date

public class Date01 {
	public static void main(String[] args) {
		Date date=new Date();
		System.out.println(date);
		long l=date.getTime();
		System.out.println(l);
		date=new Date(1557133130296L);
		System.out.println(date);
		
		//日期对象转为字符串
		String s=date.toString();
		System.out.println(s.length());
		
		/*
		 boolean after(Date when) 
		          测试此日期是否在指定日期之后。 
		 boolean before(Date when) 
		          测试此日期是否在指定日期之前。 
		 int compareTo(Date anotherDate) 
		          比较两个日期的顺序。 
		 boolean equals(Object obj) 
		          比较两个日期的相等性。 
		 */
		Date date2=new Date();
		System.out.println(date2);
		
		System.out.println(date2.after(date));  //true
		System.out.println(date2.before(date));  //false
		System.out.println(date2.before(date));  //false
		System.out.println(date.compareTo(date2));  //-1
	}
}
/*
 * SimpleDateFormat 日期格式类|转换类|转换器
 * 	指定格式
 * 	y->年
 * 	M->月
 *  d->日
 *  H->24小时
 *  h->12小时
 *  m->分
 *  s->秒
 *  S->毫秒
 *  
 *  format(Date)-->日期对象转为字符串,可以按照指定格式,可以使用转换器的默认格式
 *  parse(String) -->把字符串转为日期对象,按照指定格式转换
 */
public class SimpleDateFormat02 {
	public static void main(String[] args) throws ParseException {
		SimpleDateFormat format=new SimpleDateFormat();
		Date date=new Date();
		System.out.println(format.format(date));//19-5-6 下午5:05
		SimpleDateFormat format2=new SimpleDateFormat("yyyy/MM/dd E a hh:mm:ss SSS");
		System.out.println(format2.format(date)); //2019/05/06 05:06:57 182
		
		String str="2019年05月06日 05:09:26";
		SimpleDateFormat format3=new SimpleDateFormat("yyyy年MM月dd日 hh:mm:ss");
		System.out.println(format3.parse(str));
		
	}
}

七.枚举  enum

     1.列举所有情况,可以枚举类
        1)枚举类也是类,类中的字段|属性都是该类的一个实例,默认相当于使用public static final修饰
        2)枚举类隐式的继承了java.lang.Enum

public class EnumDemo01 {
	public static void main(String[] args) {
		Week sun=Week.Mon;
		System.out.println(sun.name());  //获取实例的名称
		System.out.println(sun.ordinal());  //2 字段索引值
		System.out.println(Arrays.toString(sun.values()));  //[Mon, Thes, Sun]返回这个枚举类实例的数组
		sun.setName("星期天");
		sun.test();
		System.out.println(sun.getName());
		//switch (枚举)
		switch(sun){
		case Sun:
			System.out.println(sun.getName()+"Sun");
			break;
		case Mon:
			System.out.println(sun.getName()+"Mon");
			break;
		}
	}
}
//定义枚举类  星期
enum Week{
	Mon,  
	Thes,
	Sun;
	private String name;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	//可以定义成员方法
	void test(){
		System.out.println("hahahha"+name);
	}
}
class WeekDay{
	public static final WeekDay Mon=new WeekDay();
}

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值