----?基础知识

  -- 编译时

编译器将源代码翻译成机器能够读懂的代码,如java中就是翻译成jvm能够读懂的字节码文件。简单说,编译时就是机器帮我们检查代码是否有出现语法错误,关键字写错之类的,是为之后的类加载做好准备,所以,在这个过程中并不会出现什么分配内存之类的操作。

  -- 运行时

这个过程是指将编译好后的储存在磁盘上的字节码文件(.class文件)加入到内存中运行,在运行的过程中,会进行一系列的类型检查,如空间内存分配,逻辑判断之类的。因此,在这个过程中经常会出现一些我们无法预知的错误。

---- 举个栗子

public class ConstantFolding {

 static final int number1 = 5;

 static final int number2 = 6;

 static int number3 = 5;

 static int number4= 6;

 public static void main(String[ ] args) {

 int product1 = number1 * number2; //line A

 int product2 = number3 * number4; //line B

 }

}

--- 分析

同时被static和final修饰的常量称作编译时常量,所以number1 和 number2在编译时已经被加载了,即product1 在编译期间就已经确定好了值为多少。而number3 和number4 只有在运行时,分配好了内存空间并且才能被成功赋值,所以product2 的值只有在运行时才能够确定是多少。反编译如下:

public class ConstantFolding
{
static final int number1 = 5;
static final int number2 = 6;
static int number3 = 5;
static int number4 = 6; public static void main(String[ ] args)
{
int product1 = 30;
int product2 = number3 * number4;
}
}

---- 举个栗子

   方法的重载:这个是发生在编译时的。方法重载也被称为编译时多态,因为编译器可以根据参数的类型来选择使用哪个方法。

public class Test{
public static void A(String param1); // method #1
public static void A(int param1); // method #2
}

如果编译器调用的方法是下面

new Test().A("classlodaer");

那么它就会在编译的时候自己去寻找menthod #1的方法

---- 举个栗子

   方法覆盖:这个是在运行时发生的。方法重载被称为运行时多态,因为在编译期编译器不知道并且没法知道该去调用哪个方法。JVM会在代码运行的时候做出决定。

public class A {
public int compute(int input) { //method #3
return 3 * input;
}
} public class B extends A {
@Override
public int compute(int input) { //method #4
return 4 * input;
}
}

如果编译器遇到如下代码,就在编译时就无法判断究竟传入的参数是A类型还是B类型,只有在运行时才能够进行确定,进而来判断要调用方法#3还是#4

public int evaluate(A reference, int arg2) {
int result = reference.compute(arg2);
}

---- 举个栗子

泛型(又称类型检验):这个是发生在编译期的。编译器负责检查程序中类型的正确性,然后把使用了泛型的代码翻译或者重写成可以执行在当前JVM上的非泛型代码。这个技术被称为“类型擦除“。

    public class Test4 {
public static void main(String[] args) {
ArrayList<String> arrayList1=new ArrayList<String>();
arrayList1.add("abc");
ArrayList<Integer> arrayList2=new ArrayList<Integer>();
arrayList2.add(123);
System.out.println(arrayList1.getClass()==arrayList2.getClass());
}
}

---- 分析

在这个例子中,我们定义了两个ArrayList数组,不过一个是ArrayList<String>泛型类型,只能存储字符串。一个是ArrayList<Integer>泛型类型,只能存储整形。最后,我们通过arrayList1对象和arrayList2对象的getClass方法获取它们的类的信息,最后发现结果为true。说明泛型类型String和Integer都被擦除掉了,只剩下了原始类型。

---- 举个栗子

  异常:分为编译时异常和运行时异常

运行时异常(RuntimeException)也称作未检测的异常(unchecked exception),这表示这种异常不需要编译器来检测。RuntimeException是所有可以在运行时抛出的异常的父类。一个方法除要捕获异常外,如果它执行的时候可能会抛出。RuntimeException的子类,那么它就不需要用throw语句来声明抛出的异常。

例如:NullPointerException,ArrayIndexOutOfBoundsException,等等

受检查异常(checked exception)都是编译器在编译时进行校验的,也称为编译时异常,通过throws语句或者try{}cathch{} 语句块来处理检测异常。编译器会分析哪些异常会在执行一个方法或者构造函数的时候抛出。

最新文章

  1. 使用rsync同步文件
  2. 12月22日《奥威Power-BI财务报表数据填报》腾讯课堂开课啦
  3. AllanCodeMaker 代码生成器 release0.9.0 下载 支持C#,Java,可自订模板
  4. StringBuffer学习笔记
  5. 浩哥解析MyBatis源码(七)——DataSource数据源模块之托管数据源
  6. C# WinForm 富文本编辑器 用kindeditor实现本地图片只选取不上传到编辑器
  7. vue stylus 格式化问题
  8. python入门——热量转换 I
  9. (爬虫向)python_json学习笔记
  10. js变量提升与函数提升的详细过程
  11. 洛谷P1477 假面舞会
  12. ****** 二十八 ******、软设笔记【数据库】-分布式数据库、特点、数据存储、DBMS组成
  13. 在BootStrap的modal中使用Select2搜索框无法输入
  14. orcl 复杂查询
  15. 一个或多个音频服务未运行 win7 错误1079:此服务的账户不同于运行于同一进程上的其他服务账户
  16. dump()
  17. Fully Update And Upgrade Offline Debian-based Systems
  18. 《DSP using MATLAB》Problem 2.2
  19. jedispool 连 redis
  20. Kali-linux枚举服务

热门文章

  1. poj2239 poj1274【二分匹配】
  2. bzoj 3109: [cqoi2013]新数独【dfs】
  3. 笔记-JavaWeb学习之旅
  4. iOS 上传的图片在HTML上显示时,图片方向信息(EXIF Orientation)异常
  5. Python函数和面向对象题目
  6. 转 做了两款数据库监控工具(mysql and nosql),打算在近期开源
  7. random模块详解
  8. leetcode315 Count of Smaller Numbers After Self
  9. CF779A(round 402 div.2 A) Pupils Redistribution
  10. pre-network android 网络优化预加载框架