JAVA2核心技术卷I:基础知识(原书第7版) -- 第3章. Java基本程序结构

来源:互联网 发布:如何报考网络大专 编辑:程序博客网 时间:2024/05/15 00:50

第3章. Java基本程序结构  -- 详细阅读

  1. 强行退出: System.exit(0);

  2. 数据类型

    2.1. 整形(允许负数)

      int  4字节 32位

      short  2字节 16位

      long  8字节 64位

      byte  1字节  8位

    2.2. 浮点型

      float  4字节 32位

      double  8字节 64位

    2.3. char类型

      转义写法:  /b 退格,/t制表,/n换行,/r回车,/"双引号,/'单引号,//反斜杠

    2.4. boolean类型

  3. 类常量static final - 可以在一个类的多个方法中使用,常量名大写,定义在方法体外。

  4. 枚举类型

    enum Size{SMALL, MEDIUM, LARGE, EXTRA_LARGE};

    Size s = Size.MEDIUM;

  5. 比较字符串相等(equals),一定不要使用==,不区分大小写使用equalsIgnoreCase,==只是比较两个字符串是否被放置在同一个位置。

  6. java.util.Scanner

    6.1 读取输入行 Scanner in = new Scanner(System.in); 

                                 String name = in.nextLine();

                                 int age = in.nextInt();

    6.2 读取文件中的内容 Scanner scanner = new Scanner(new File(filename), "BIG5");  //编码方式

                                             while (scanner.hasNextLine()) {

                                                       String s = scanner.nextLine();

                                             }

  7. String to Integer: Integer.parseInt(String); 

      Integer to String:  Integer.toString();

      int to String: String.valueOf(int);

  8. while(condition) statement;  // if condition is false, do not execute statment

       do statement while (condition); //至少执行一次

  9. swith (input) {} // case标签必须是整数或者枚举常量,不能检测字符串。

  10. 大数字  BigInteger / BigDecimal

        BigInteger a = BigInteger.valueOf(100);

        BigInteger c = a.add(b);

  11. 数组 - 允许数组长度为0

        int[] a = new int[100];

        int[] anonymous = {17, 20, 32, 48};

        数组拷贝: System.arraycopy(from, fromIndex, to, toIndex, count);

        Arrays.sort(a); //数组排序

 

原创粉丝点击