zoukankan      html  css  js  c++  java
  • Java知识点:琐碎知识点(2)

    49个关键字一览

    abstract

    default

    if

    private

    this

    boolean

    do

    implements

    protected

    throw

    break

    double

    import

    public

    throws

    byte

    else

    instanceof

    return

    transient

    case

    extends

    int

    short

    try

    catch

    final

    interface

    static

    void

    char

    finally

    long

    strictfp

    volatile

    class

    float

    native

    super

    while

    const

    for

    new

    switch

    null

    continue

    goto

    package

    synchronized

    main函数

    • main函数并不只有public static void main(String args[]),还可以是 final public static void main(String[]args) 或 public static void main(String...args)
    • 不允许:public static void main(String[]...args).
    • 如果类A没有main函数,当 “java A” 时,会抛:Exception NoSuchMethodError: main.

    断言机制

    语法:

    • assert <boolean expression>;
    • assert <boolean expression> : <str>; str是在抛出AssertionError时输出的描述信息。
    • 断言默认是关闭的。
    • 可以针对某个类、某个包、或全部类 使用断言或禁用断言。

    准则:

    • 如果 assert fails,则抛出AssertionError。
    • 不要使用断言验证任何公共方法(包括main方法)的参数
    • 不要使用可能产生副作用的断言表达式。
    • 断言的宗旨:加和不加断言对于程序没有任何影响,因为断言只用来验证。
    • 对于断言可能抛出的AssertionError,不需要去特别处理这个Error。
    • 如果用-source 1.3编译代码时assert用作标识符,则虽然编译成功,但会有警告。

    静态变量

    • 在一般方法内不能声明静态变量,因为静态变量要求在类加载时就分配空间。

    异常层次

    异常准则:

    • 编译器禁止永远不可达的catch子句,否则会编译错误:XXX has already been caught。
     1 public class Exception02
     2 {
     3     public static void main(String[] args) {
     4         try { 
     5             int x = 0; 
     6             int y = 5 / x; 
     7         } catch (Exception e) { 
     8             System.out.println("Exception"); 
     9         } catch (ArithmeticException ae) {     //Compile Error: 因为永远不可达
    10             System.out.println("Arithmetic Exception"); 
    11          } 
    12         System.out.println("finished");
    13     }
    14 }

    类声明的访问级别

    • 声明一般类时,只能使用public或default访问级别,因为其他两个访问修饰符对一般类没有意义。

    包装类的equals方法实现

    这里用Integer举例子:

    public boolean equals(Object obj) {
        if (obj instanceof Integer) {
            return value == ((Integer)obj).intValue();
        }
        return false;
    }

    因此不管怎么调用equals方法,都不会抛异常。

    如果a是Integer类型的,执行a.equals(b),只要b不是Integer类型的,都会返回false。

    包装器类的构造函数

    存在参数为String的构造函数,这里以Boolean为例。

    • new Boolean(String str);

    死循环编译错误

    下面的代码会编译错误:

    while(true)
    { 
    }
    int a = 1;    //Compile Error:不可达的语句

    但是下面的代码不会编译错误:

    while(true)
    { 
    }

    内部类注意点

    一般内部类中不允许定义任何静态成员。

    数组的声明方法

    • int[]arr = new int[]{1,2,3};    //YES
    • int[]arr = new int[3]{1,2,3};   //NO

    Math类

    • Math.sqrt(double a)
      • 如果a是NaN或者小于0,则返回NaN。
      • 如果a是正无穷大,则返回正无穷大。
      • 如果a是正0或负0,则返回与参数相同。
    • Math.cos(double a)
      • a是用弧度表示的角。
    • Math.toRadians(double a):角度->弧度
    • Math.toDegrees(double a):弧度->角度

    位运算问题

    • 正数的无符号右移并不一定还是正数。

    对于 c = a >>> b,即使 a>0,b>1,有可能使得 c<0的,比如:

    int a = 1;
    int b = Integer.MAX_VALUE + 1;
    int c = a >>> b;     // c=-1

    文件I/O问题

    如果要设置输出的编码,只能使用 OutputStreamWriter,因为它能够通过如下构造函数设置输出编码:

    • new OutputStreamWriter(OutputStream out, String charSet);

    FileOutputStream存在 FileOutputStream(File file,boolean append); 第二个参数如果为true,表示将写入的内容放入文件末尾。

    带标签的break和continue

    • 标签一定要直接位于while或for语句之前,否则Compile Error。

    静态代码块异常

    • 如果在静态初始化程序时(比如静态代码块)发生异常,则统一抛出:ExceptionInInitializerError。
    public class InitializationBlock02
    {
        static
        {
            int a = 1/0;        //抛ExceptionInInitializerError,
        }
        public static void main(String args[])
        {
    
        }
    }

    外部类声明注意点

    • static不能用于修饰外部类。

    重写的注意点

    • 重写方法不能抛出更广的检验异常。
    • 编译器没有判断多态性的能力,如果有类A和类B,且B extends A,如果A a = new B(); a.fun(),则编译器认为你在调用A的fun方法,因此如果A的fun方法抛出检验异常,则你需要捕获,不然会Compile Error。
    public class Overridding02
    {
        public static void main(String[] args) {
            A b = new B();    //Compile Error: 未报告的异常 java.lang.Exception
            b.fun();
        }
    }
    class A
    {
        public void fun() throws Exception
        {
        }
    }
    class B extends A{
        public void fun()
        {
        }
    }
  • 相关阅读:
    当当网css代码
    当当网代码6
    游戏UI设计(2.1)窗口之父CXWnd的封装
    英语(1)备考——词汇
    UML的五类图(UML笔记)
    Sieve of Eratosthenes[ZT]
    std::map初体验
    “非计算机相关专业”的定义
    英语(1)备考——翻译
    使用回调函数发送自定义“消息”
  • 原文地址:https://www.cnblogs.com/xiazdong/p/3241377.html
Copyright © 2011-2022 走看看