A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 段孟钊 中级黑马   /  2019-5-7 14:48  /  863 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 小石姐姐 于 2019-5-9 18:32 编辑

个人笔记一、基本类型包装类
1、什么是基本类型包装类
​        基本类型包装类可以提供一些功能供基本类型数据使用
2、基本类型包装类有哪些
​        基本数据类型---基本数据类型的包装类
​        byte----Byte
​        short---Short
​        int---Integer
​        long---Long
​        float---Float
​        double--Double
​        char---Character
​        boolean---Boolean
二、Integer类Integer类的定义格式
1.构造方法
[Java] 纯文本查看 复制代码
 
Integer i1 = new Integer(100);//把int类型的100转换成Integer
  ​
  Integer i2 = new Integer("100");//把String类型的“100”转换成Integer
  ​
  //Integer i2 = new Integer("abc");//把String类型的“abc”转换成Integer
  ​
  //不能进行转换,数值类型格式不匹配,//NumberFormatException
  Integer i1 = new Integer(100);//把int类型的100转换成Integer
  ​
  Integer i2 = new Integer("100");//把String类型的“100”转换成Integer
  ​
  //Integer i2 = new Integer("abc");//把String类型的“abc”转换成Integer
  ​
  //不能进行转换,数值类型格式不匹配,//NumberFormatException
  ​2.Integer封装类的valueof()  
  Integer i3 = Integer.valueOf(100);//把int类型的100转换成Integer
  ​
  Integer i4 = Integer.valueOf("100");//把String类型的“100”转换成Integer
  ​
  Integer i4 = Integer.valueOf("abc");//把String类型的“abc”转换成Integer
  ​
  //不能进行转换,数值类型格式不匹配,//NumberFormatException
  ​
​               
三、int和String的相互转换int---String
1.简单方式  
  int  i =10; String s = i+"";
2.专业方式  
  int i =10; String s = s.valueOf(i);
String---int
1.基本逻辑方式  
[Java] 纯文本查看 复制代码
 //String --- Integer ,Integer --- int
  String s = "100"; Integer i1 = Integer.valueOf(s); int i2 = Integer.intValue(i1);
​ 2.便捷方式
  
[Java] 纯文本查看 复制代码
//String --- int
  String s = "100"; int i = Integer.parseInt(s);

四、字符串中数据排序  
[Java] 纯文本查看 复制代码
  import java.util.Arrays;
  //定义一个字符串  "27 98 49 16 57",打印输出 "16 27 49 57 98"
  public class Test{
      public static void main(String[] args){
          //定义字符串
          String s = "27 98 49 16 57";
          //拆分成字符串数组
          String[] strArr = s.split(" ");
          //定义int数组
          int[] arr = new int[strArr.length];
          //把字符串数组中的每个元素,放到int数组中
          for(int i = 0; i < arr.length; i++){
              arr = Integer.parseInt(strArr);
          }
          //将int数组进行排序
          Arrays.sort(arr);
          //把int数组再转换成String类型,借用StringBuilder优化
          StringBuilder sb = new StringBuilder();
          for(int i=0; i< arr.length ; i++){
              sb.append(arr);
              if(i != arr.length - 1){
                  sb.append(" ");
              }
          }
          String ss = sb.toString();
          System.out.println(ss);
      }
  }

五、自动装箱和自动拆箱
1.自动装箱
​       概念:把基本数据类型自动转换成基本数据类型封装类
​       例:int i=10; Integer ii = Integer.valueOf(i);//这是装箱,Integer iii = i;//自动装箱
2.自动拆箱
​        概念:把基本数据类型封装类自动转换成基本数据类型
​        例:Integer i = Integer.valueOf(10); int ii = i.intValue();//这是拆箱,int iii = i;//自动拆箱
六、Date类1.概述:
Date类是代表了一个特定的时间类,精确到毫秒值。
2.构造方法:
无参构造  public Date(){};
Date d = new Date();表示d这个对象,是当前系统时间
有参构造:public Date(long time){};
Date d = new Date(10006060);表示d这个对象,是距1970.1.1加上这个毫秒值的时间
七、Date类常用方法  
  Date d = new Date();
  d.getTime();//获取d这个对象的时间,的毫秒值
  d.setTime(long time);//传入一个毫秒值,设置d这个对象的时间
八、SimpleDateFormat类
1.作用
为了把Date类对象的时间进行格式化,并且按照开发者的模式进行格式化
2.SimpleDateFormat类的构造方法
无参构造: SimpleDateFormat() //按照这个类中的默认日期模式格式化对象
有参构造:SimpleDateFormat(String format)//按照指定的日期模式格式化对象
3.如何将日期对象格式化成字符串对象  Date---String  
[Java] 纯文本查看 复制代码
Date d = new Date();
  SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  String s = sdf.format(d);
  system.out.println(s);//输出2019-04-28 11:11:11

4.如何将字符串对象解析化成日期对象   String---Date  
[Java] 纯文本查看 复制代码
 String s = "2019-04-08 11:11:11";
  SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  Date d = sdf.parse(s);
  System.out.println(d);//Mon Apr 08 11:11:11 CST 2019

九、日期工具类  
[HTML] 纯文本查看 复制代码
 public class DateUtil {
      private DateUtil(){};
      //完成将Date对象格式化成字符串的功能
      public static String dateToString(Date d, String format){
          SimpleDateFormat sdf = new SimpleDateFormat(format);
          String s = sdf.format(d);
          return s;
      }
      //将字符串日期解析成Date对象功能
      public static Date StringToDate(String s,String format) throws ParseException {
          SimpleDateFormat sdf = new SimpleDateFormat(format);
          Date d = sdf.parse(s);
          return d;
      }
  ​
  }
  ​
  ​
  public class DateUtilTest {
      public static void main(String[] args) throws ParseException {
          Date d = new Date();
          String s = DateUtil.dateToString(d, "yyyy-MM-dd HH:mm:ss");
          System.out.println(s);
  ​
          String ss = "2019-04-28 20:49:26";
          Date dd = DateUtil.StringToDate(ss, "yyyy-MM-dd HH:mm:ss");
          System.out.println(d);
      }
  }

十、Calendar类
1.概述
表示特定瞬间与一组日历字段之间的转换提供了一些方法      
并且也为操作日历字段提供了一些常用方法
2.Calendar获取对象的方式
Calendar c = Calendar.getInstance();
3.Calendar获取日历字段的方法
c.get(日历字段 int file);
十一、Calendar常用方法  
[Java] 纯文本查看 复制代码
Calendar c = Calendar.getInstance();
  c.get(Calendar.YEAR);//获取指定日历字段的当前系统时间值
  c.add(Calendar.YEAR,5);//表示五年后的今天,将指定日历字段进行增加或减少
  c.set(2018,01,01);//设置日历的年月日,输出2018-02-01,因为月是从0开始

十二、二月天  
[Java] 纯文本查看 复制代码
public class CalendarTest {
      public static void main(String[] args) {
          Scanner sc = new Scanner(System.in);
          System.out.println("请输入一个年份:");
          int year = sc.nextInt();
          Calendar c = Calendar.getInstance();
          c.set(year,2,1);
          c.add(Calendar.DATE,-1);
          int date = c.get(Calendar.DATE);
          System.out.println(year + "年二月有"+date+"天");
      }
  }

十三、异常
1.概述
程序报错,不正常的现象
2.异常的体系结构
​                                Throwable
​        Error                                                Exception
​                                        RunTimeException        非RunTimeException
Throwable:所有异常类的祖宗类
Error:不可修复的错误,严重问题,不需要开发员处理
Exception:可修复的错误,(异常),表示程序本身可以处理的问题
RunTimeException:运行时异常,在编译期是不检查的,出现问题后,需要我们回来修改代码
非RunTimeException:编译时异常,编译期就必须处理,否则程序不能通过编译,不能正常运行
十四、JVM的默认处理方案
把异常的名称、原因、位置等信息输出在控制台      
结束程序的运行
十五、异常处理之try...catch
1.try...catch的定义格式  
  try{
      可能会出现异常的代码;
  }catch(异常类 对象名){
      处理方式;
  }
2.try...catch的用处
它是直接面对异常,并且可以给出结局方式
十六、Throwable类的成员方法
Throwable常用方法有哪些?
getMessage();//表示获取异常的简短消息,有返回值--String类型
toString();//表示获取异常的详细消息,有返回值---String类型
printStacktrace();表示获取异常的完整消息,无返回值,直接打印。
十七、编译时异常和运行时异常的区别[td]
编译时异常
运行时异常
Exception类及其子类RuntimeException类及其子类
写代码时必须处理,否则无法运行写代码时不需要处理
十八、异常处理之throws
有些时候我们不想使用try-catch或没有权限使用try-catch方式处理异常时      
还可以选择throws抛出的方式,格式为:     
  在方法声明上:throws 异常类名
例如:public static void show() throws ArraysIndexOutOfFoundsException{
}
十九、自定义异常  
[Java] 纯文本查看 复制代码
 //异常类
  public class ScoreException extends Exception {
     public ScoreException(){}
     public ScoreException(String message){
         super(message);
     }
  }
  //使用异常类
  public class Teacher {
      public void score(int score) throws ScoreException {
          if (score<0 || score>100){
              throw new ScoreException("你输入的分数不正确!");
          }else {
              System.out.println("分数正常");
          }
      }
  }
  //测试类
  public class Test1 {
      public static void main(String[] args) {
          Scanner sc =new Scanner(System.in);
          System.out.println("请输入一个分数:");
          int score = sc.nextInt();
          Teacher t = new Teacher();
          try {
              t.score(score);
          } catch (ScoreException e) {
              e.printStackTrace();
          }
  ​
      }
  }


0 个回复

您需要登录后才可以回帖 登录 | 加入黑马