Null Pointer Exception,简称NPE

在java中,static final修饰的是常量。根据编译器的不同行为,常量又可分为编译时常量和运行时常量。

举例说明吧

  • public static final int a = 10就是一个编译时常量,在编译后的符号中找不到a,所有对a的引用都被替换成了20;它是不依赖于类的,在编译时就可以确定值。
  • public static final int b = “hello”.length()就是一个运行时常量;它是依赖于类的,它的赋值会引起类的初始化。
  • 归根结底还是javac的编译机制导致了这样的结果。当涉及到方法的调用时,必须在运行的时候才能确定结果。
public class TestConstant {
    public static void main(String[] args) {
        System.out.println(Test.a);
        System.out.println(Test.s);
//      System.out.println(Test.b);
//      System.out.println(Test.a2);
//      System.out.println(Test.a3);
//      System.out.println(Test.e);
    }
}

class Test{
    static{
        System.out.println("Test正在被初始化");
    }
    public static final int a = 10;
    public static final int b = "test".length();
    public static final String s = "world";
    public static Integer a2 = 20;
    public static final A a3 = new A();
    public static final E e = E.A;
}
class A{

}
enum E{
    A,B,C,D,E,F,G
} 
  • 挨个运行main函数中的打印语句,我们会发现编译时常量不会引起类的初始化,而运行时常量会引起类的初始化。

  • 通过运行我们还可以得出如下结论,编译时常量必须定义为基本类型或者String,而不可能是引用类型或者包装类、枚举。

一道面试题:编译时常量存在什么样的风险?

编译时常量,在使用时会被直接写成值,而不会再从原来的类中读取。这样就会导致问题的产生:如果A类定义了常量,B类使用了常量,并且都进行了编译。当A类的源码被改动了,常量的值发生了变化。我们对A类进行了重新编译,但是没有对B类进行重新编译;那么B类中用到的是原来A类中的常量值,即旧值。这就导致了风险的发生。  

EQ 就是 EQUAL等于  eq
NE 就是 NOT EQUAL不等于 ne 
GT 就是 GREATER THAN大于  gt  
LT 就是 LESS THAN小于  lt
GE 就是 GREATER THAN OR EQUAL 大于等于  ge
LE 就是 LESS THAN OR EQUAL 小于等于 le

an integral type

byte: -128 to 127

short: -32768 to 32767

int: -2147483648 to 2147483647

long: -9223372036854775808 to 9223372036854775807

char: 0 to 65535

finalizer

The class Object has a protected method called finalize; this method can be overridden by other classes. The particular definition of finalize that can be invoked for an object is called the finalizer of that object. Before the storage for an object is reclaimed by the garbage collector, the Java Virtual Machine will invoke the finalizer of that object.

参考:https://docs.oracle.com/javase/specs/jls/se8/html/jls-12.html#jls-12.6

final与effectively final

局部内部类和匿名内部类访问的局部变量必须由final修饰,java8开始,可以不加final修饰符,由系统默认添加。java将这个功能称为:Effectively final 功能。

In a uni-catch clause, an exception parameter that is not declared final (implicitly or explicitly) is considered effectively final if it never occurs within its scope as the left-hand operand of an assignment operator (§15.26).

comb rule

Choosing to search a nested class's superclass hierarchy before than the lexically enclosing scope is called the "comb rule" (§15.12.1).

Choosing to search a nested class's superclass hierarchy before than the lexically enclosing scope is called the "comb rule" (§15.12.1).

15.12.2.7. Inferring Type Arguments Based on Actual Arguments

U << V indicates that type U is convertible to type V by method invocation conversion (§5.3), and U >> V indicates that type V is convertible to type U by method invocation conversion.

 
例如interface A<T>{}中的T。class B<T extends InputStream>中的T都是a type variable
public class Test3<X> {

	// A extends Object和B extends Comparable为R1,R2,Object与Comparable为B1与B2。 List<? extends A>与B为F1,F2
	public <A extends Object, B extends Comparable,C extends X> void test(List<? extends A> x, B b) {

	}
}

如上解释了TypeBound: extends TypeVariable形式。方法test中的类型参数中C extends X中的X为Type Variable形式。

 

 
 
 
关于构造函数和方法的类型擦除,举个例子:
public class TestGenericErasure<T extends InputStream> {

	public <X extends Serializable&Comparable<T>>void test01(T t,X x){

	}

}

擦除后变为:

public class TestGenericErasure {

    public TestGenericErasure() {
        super();
    }

    public void test01(InputStream t, Serializable x) {
    }
}

可以看到方法中所有的type parameter全部被擦除。一般方法中的类型参数、返回类型与形式参数类型都会进行泛型擦除。 

下面来看理解这句话:

The erasure of a type variable (§4.4) is the erasure of its leftmost bound.

举个例子,如下:

public class Generic<T extends Object & Appendable & AutoCloseable> {

  T t;

  T method() throws Exception {
    t.close();
    char c='\u0000';
    t.append(c);
    return t;
  }

  public <T> T method2(T t) {
    return t;
  }  

}

那么T将全部替换为Object,最后泛型擦除后的结果如下:

public class Generic{

    public Test() {
        super();
    }
    Object t;

    Object method() throws Exception {
        ((AutoCloseable)t).close();  // 强制类型转换
        char c = '\u0000';
        ((Appendable)t).append(c);  // 强制类型转换
        return t;
    }

    public Object method2(Object t) {
        return t;
    }
}

  

a variable arity method与a fixed arity method

public class Test1 {

	public void m(Object ...x){
		System.out.println("a variable arity method");
	}

	public void m(Object x){
		System.out.println("a fixed arity method");
	}

	public static void main(String[] args) {
		new Test1().m(null); // 打印为a variable arity method
	}
}

  

if and only if  会被写为iff  

Covariance, Invariance and Contravariance 概念解析

At heart, these terms describe how the subtype relation is affected by type transformations. That is, if A and B are types, f is a type transformation, and ≤ the subtype relation (i.e. A ≤ B means that A is a subtype of B), we have

  • f is covariant if A ≤ B implies that f(A) ≤ f(B)
  • f is contravariant if A ≤ B implies that f(B) ≤ f(A)
  • f is invariant if neither of the above holds

如某些泛型是invariant,因为String≤ Object,但是List<String>与List<Object>没有关系

数组是covariant,因为String≤Object,有String[]≤Object[]

某些泛型是contravariant,因为String << Object,有List<? super Object> << List<? super String>

class AA{
    public Integer[] aa(){
        return null;
    }

    public String[] aa(int a){
        return null;
    }

    public String[] bb(int a){
        return null;
    }
}

class BB extends AA{
    public String[] bb(){
        return null;
    }
}

有通配符的泛型是contravariant,因为String≤Number,有List<? super Number> ≤ List<? super String>

class Food{}
class Fruit extends Food{}

class Plate<T>{}

public class Test3 {

    public void test(){
        Plate<? extends Fruit> a = null;
        Plate<? extends Food>  b = null;
        b = a;

        Plate<? super Food> c = null;
        Plate<? super Fruit> d = null;
        d = c;
    }
}

参阅文章:

(1)http://stackoverflow.com/questions/8481301/covariance-invariance-and-contravariance-explained-in-plain-english/42239324#42239324

(2)http://www.angelikalanger.com/GenericsFAQ/FAQSections/TechnicalDetails.html#FAQ201

阅读The Java® Language Specification需要知道的术语的更多相关文章

  1. 阅读The Java® Language Specification需要知道的英文单词

      In any case/on any account  在任何情况下 “Varargs”是“variable number of arguments”的意思.有时候也被简单的称为“variable ...

  2. Java® Language Specification

    Java™ Platform, Standard Edition 8 API Specification http://docs.oracle.com/javase/8/docs/api/ The J ...

  3. 笔记:Java Language Specification - 章节17- 线程和锁

    回答一个问题:多线程场景下,有时一个线程对shared variable的修改可能对另一个线程不可见.那么,何时一个线程对内存的修改才会对另一个线程可见呢? 基本的原则: 如果 读线程 和 写线程 不 ...

  4. 如何从oracle官网中下载The java language specification(java 语言规范)

    第一步: 第二步: 第三步:下面这个图在这个页面的下方,所以你只要一直往下看,直到看到下图的文字为止: 第四步: 第五步: 这样你就可以成功下载该java 语言规范的pdf了. 它直接下载的网址为: ...

  5. Java Language and Virtual Machine Specifications

    The Java Language Specification, Java SE 8 Edition HTML | PDF The Java Virtual Machine Specification ...

  6. 如何阅读《ECMAScript 2015 Language Specification》

    你不需要把<ECMAScript 2015 Language Specification>通读一遍,因为没那个必要.   阮一峰建议: 对于一般用户来说,除了第4章,其他章节都涉及某一方面 ...

  7. C# Language Specification 5.0 (翻译)第一章 引言

    C#(念作 See Sharp)是一种简单.现代.面向对象并且类型安全的编程语言.C# 源于 C 语言家族,因此 C.C++ 和 Java 工程师们能迅速上手.ECMA 国际[1](ECMA Inte ...

  8. study java language

    2016.11.30 1).About the Java Technology 2).The Java Language Environment: Contents

  9. The P4 Language Specification v1.0.2 Header and Fields

    前言 本文参考P4.org网站给出的<The P4 Language Specification v1.0.2>的第二部分首部及字段,仅供学习:). 欢迎交流! Header and Fi ...

随机推荐

  1. kali下firefox的安装

    在kali的系统中自带了一个firefox分支下的浏览器Iceweasel(Iceweasel是Mozilla Firefox浏览器的Debian再发布版),但是怎么说也配不上kali的强悍气势.还是 ...

  2. github注册与使用

    个人信息: 姓名:赵建 学号:1413042015 班级:网络工程141班 兴趣爱好:码代码,看电影,折腾linux github注册: 首先在地址栏输入https://www.github.com, ...

  3. WP8.1StoreApp(WP8.1RT)---发送邮件和短信

    在WP7/8中,发送短信是利用了EmailComposeTask和SmsComposeTask来实现的. 在WP8.1 Store App中,原来的方式已经失效,采用了新的方法:ChatMessage ...

  4. c# Net XML文档(2,2)中有错误

    错误如图所示: xml转实体,需求很简单,度娘找了几个方法试了下,转换代码仔细看了看 没毛病啊  但是 就是提示 XML文档(2,2)中有错误,百度搜索了一大会 没解决方案,仔细分析了一下, 最后发现 ...

  5. poj 1006 Biorhythms (中国剩余定理模板)

    http://poj.org/problem?id=1006 题目大意: 人生来就有三个生理周期,分别为体力.感情和智力周期,它们的周期长度为23天.28天和33天.每一个周期中有一天是高峰.在高峰这 ...

  6. char、varchar、nchar、nvarchar、text的区别

    char.varchar.nchar.nvarchar.text的区别 1.有var前缀的,表示是实际存储空间是变长的,varchar,nvarchar 所谓定长就是长度固定的,当输入的数据长度没有达 ...

  7. 【题解】 AGC029-A Irreversible operation

    传送门 定位:思维好题. 考虑无论如何每一个W都会和前面的B在一起交换一次,所以直接求和就好了. 注意long long的使用. #include<stdio.h> #include< ...

  8. c语言------第一次作业,分支,顺序结构

    1.1思维导图 1.2本章学习体会及代码量学习体 1.2.1学习体会 初次接触C语言,由于比较懒惰,感觉学习脚步跟不上身边的同学,也比较困扰.但伴随着pta上多次显示的##编译错误##,坚持不懈地问舍 ...

  9. OpenStack 业务链networking-sfc介绍 (2) - 底层原理

    原文链接:https://blog.csdn.net/bc_vnetwork/article/details/65630475 1.  SFC底层实现原理 port chain和ovs driver/ ...

  10. Exp4 恶意代码分析 20164323段钊阳

    网络对抗技术 20164323 Exp4 恶意代码分析 1.如果在工作中怀疑一台主机上有恶意代码,但只是猜想,所有想监控下系统一天天的到底在干些什么.请设计下你想监控的操作有哪些,用什么方法来监控. ...