zoukankan      html  css  js  c++  java
  • Java基础

    1.跨平台 Java语句(*.java) -> 字节码指令(.class) -> CPU指令(汇编)


    2.自动垃圾收集 GC


    3.基础类型
    类型 大小 默认值 值范围
    boolean 不适用 false true 或 false
    byte 8 位 0 -128 到 127
    char 16 位 (无符号) u0000' u0000' 到 uffff' 或 0 到 65535
    short 16 位 0 -32768 到 32767
    int 32 位 0 -2147483648 到 2147483647
    long 64 位 0 -9223372036854775808 到 9223372036854775807
    float 32 位 0.0 1.17549435e-38 到 3.4028235e+38
    double 64 位 0.0 4.9e-324 到 1.7976931348623157e+308


    4.位运算
    运算符 用法 返回 true 的条件……
    & a & b a 和 b 均为 true,则始终计算 b
    | a | b 如果 a 或 b 为 true,则始终计算 b
    ^ a ^ b a 和 b 不同


    5.try-with-resources
    try (BufferedReader bufferedReader = new BufferedReader(new FileReader(file))) {
    String line = bufferedReader.readLine();
    while (line != null) {
    // Read the file
    }
    } catch (Exception e) {
    l.severe(e.getMessage());
    }
    在圆括号内的 try 后分配资源变量,在 try 代码块超出范围时,这些资源会自动关闭。这些资源必须实现 java.lang.AutoCloseable 接口


    6.*构造方法做的第一件事是调用其直接超类的默认构造方法,除非您(在构造方法的第一行代码上)调用一个不同的构造方法
    *如果您提供了一个替代性构造方法,必须显式提供默认构造方法;否则它将不可用。例如,以下代码会得到一个编译错误
    *构造方法可通过 this 关键字和一个参数列表来调用同一个类中的另一个构造方法。像 super() 一样,this() 调用必须是构造方法中的第一行

    7.访问修饰符
    访问修饰符 描述
    public 可由任何类调用。
    protected 仅能由同一个包中的类或任何子类调用。
    无修饰符(包私有) 可由同一个包内的任何类调用。
    private 仅能由定义它的类调用。

    8.嵌套类的实例生成
    先生成外部类,在用外部类的实例生成内部类实例
    public class Manager extends Employee {
    public Manager() {
    }
    . . .
    public class DirectReports {
    . . .
    }
    }
    // Meanwhile, in another method somewhere...
    public static void main(String[] args) {
    Manager manager = new Manager();
    Manager.DirectReports dr = manager.new DirectReports();
    }

    或者使用静态内部类
    public class Manager extends Employee {
    . . .
    public static class ManagerComparator implements Comparator<Manager> {
    . . .
    }
    }
    // Meanwhile, in another method somewhere...
    public static void main(String[] args) {
    Manager.ManagerComparator mc = new Manager.ManagerComparator();
    . . .
    }

    9.匿名内部类

    new StockOptionEligible() {
    @Override
    public void awardStockOptions(int number, BigDecimal price) {
    // This employee is not eligible
    log.warning("It would be nice to award " + number + " of shares at $" +
    price.setScale(2, RoundingMode.HALF_UP).toPlainString() +
    ", but unfortunately, Employee " + person.getName() +
    " is not eligible for Stock Options!");
    }
    }

    10.正则表达式 Regular Expressions API
    Pattern 描述了一种字符串模式。
    Matcher 测试一个字符串,查看它是否与该模式匹配。
    PatternSyntaxException 告诉您,您尝试定义的模式的某个方面无法被接受。

    正则表达式结构 符合匹配条件的内容
    . 任何字符
    ? 前面的零 (0) 或一 (1) 个字符或数字
    * 前面的零 (0) 或更多个字符或数字
    + 前面的一 (1) 或更多个字符或数字
    [] 一个字符或数字范围
    ^ 后面的条件的否定条件(即 “非后面的条件”)
    d 任何数字(也可表示为 [0-9])
    D 任何非数字(也可表示为 [^0-9])
    s 任何空格字符(也可表示为 [ f ])
    S 任何非空格字符(也可表示为 [^ f ])
    w 任何单词字符(也可表示为 [a-zA-Z_0-9])
    W 任何非单词字符(也可表示为 [^w])


    Pattern pattern = Pattern.compile("[Aa].*string");
    Matcher matcher = pattern.matcher("A string");
    boolean didMatch = matcher.matches();
    Logger.getAnonymousLogger().info (didMatch);
    int patternStartIndex = matcher.start();
    Logger.getAnonymousLogger().info (patternStartIndex);
    int patternEndIndex = matcher.end();
    Logger.getAnonymousLogger().info (patternEndIndex);

    matches() 告诉您整个输入序列是否与该模式准确匹配。
    start() 告诉您匹配的字符串在输入字符串中的起点的索引值。
    end() 告诉您匹配的字符串在输入字符串中的起点的索引值加 1 的结果。


    lookingAt() 查找匹配部分
    matches() 完全匹配

    匹配和操作分组
    在每种模式中,通常通过会将模式的各部分放在圆括号中来创建分组。分组从左向右编号,从 1 开始编号(分组 0 表示完整的匹配结果)。

    String input = "Here is a WikiWord followed by AnotherWikiWord, then SomeWikiWord.";
    Pattern pattern = Pattern.compile("[A-Z][a-z]*([A-Z][a-z]*)+");
    Matcher matcher = pattern.matcher(input);
    Logger.getAnonymousLogger().info("Before: " + input);
    String result = matcher.replaceAll("blah$0blah");
    Logger.getAnonymousLogger().info("After: " + result);

    通过在替换字符串中包含 $0 来引用完整匹配结果。$ int 格式的替换字符串的任何部分引用该整数所标识的分组(所以 $1 引用分组 1,依此类推)。换句话说,$0 等效于 matcher.group(0);。

    11.enum
    public enum Gender {
    MALE,
    FEMALE,
    OTHER
    }

  • 相关阅读:
    Hdu 4496 D-City
    Hdu 1213 How Many Tables
    T1387:搭配购买(buy)
    codevs 2597 团伙
    Hdu 1232 畅通工程
    RQNOJ PID331 家族
    提高组day4
    xjoi2018提高组训训练25
    关于upp和lower
    矩阵快速幂求fib
  • 原文地址:https://www.cnblogs.com/xuemanjiangnan/p/7458540.html
Copyright © 2011-2022 走看看