`
jiasanshou
  • 浏览: 15752 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

解析Java finally

阅读更多

问题分析

首先来问大家一个问题:finally 语句块一定会执行吗?
很多人都认为 finally 语句块是肯定要执行的,其中也包括一些很有经验的 Java 程序员。可惜并不像大多人所认为的那样,对于这个问题,答案当然是否定的,我们先来看下面这个例子。
清单 1.

  1. publicclassTest{
  2. publicstaticvoidmain(String[]args){
  3. System.out
  4. .println("returnvalueoftest():"+test
  5. ());
  6. }
  7. publicstaticinttest(){
  8. inti=1;
  9. //if(i==1)
  10. //return0;
  11. System.out
  12. .println("thepreviousstatementoftryblock");
  13. i=i/0;
  14. try{
  15. System.out
  16. .println("tryblock");
  17. returni;
  18. }finally{
  19. System.out
  20. .println("finallyblock");
  21. }
  22. }
  23. }

清单 1 的执行结果如下:

the previous statement of try block
Exception in thread "main" java.lang.ArithmeticException: / by zero
at com.bj.charlie.Test.test(Test.java:15)
at com.bj.charlie.Test.main(Test.java:6) 

另外,如果去掉上例中被注释的两条语句前的注释符,执行结果则是:

  1. returnvalueoftest():0

在以上两种情况下,finally 语句块都没有执行,说明什么问题呢?只有与 finally 相对应的 try 语句块得到执行的情况下,finally 语句块才会执行。以上两种情况,都是在 try 语句块之前返回(return)或者抛出异常,所以 try 对应的 finally 语句块没有执行。

那好,即使与 finally 相对应的 try 语句块得到执行的情况下,finally 语句块一定会执行吗?不好意思,这次可能又让大家失望了,答案仍然是否定的。请看下面这个例子(清单 2)。

清单 2.

  1. publicclassTest{
  2. publicstaticvoidmain(String[]args){
  3. System.out
  4. .println("returnvalueoftest():"+test
  5. ());
  6. }
  7. publicstaticinttest(){
  8. inti=1;
  9. try{
  10. System.out
  11. .println("tryblock");
  12. System.exit
  13. (0);
  14. returni;
  15. }finally{
  16. System.out
  17. .println("finallyblock");
  18. }
  19. }
  20. }

清单 2 的执行结果如下:

try block 

finally 语句块还是没有执行,为什么呢?因为我们在 try 语句块中执行了 System.exit (0) 语句,终止了 Java 虚拟机的运行。那有人说了,在一般的 Java 应用中基本上是不会调用这个 System.exit(0) 方法的。OK !没有问题,我们不调用 System.exit(0) 这个方法,那么 finally 语句块就一定会执行吗?

再一次让大家失望了,答案还是否定的。当一个线程在执行 try 语句块或者 catch 语句块时被打断(interrupted)或者被终止(killed),与其相对应的 finally 语句块可能不会执行。还有更极端的情况,就是在线程运行 try 语句块或者 catch 语句块时,突然死机或者断电,finally 语句块肯定不会执行了。可能有人认为死机、断电这些理由有些强词夺理,没有关系,我们只是为了说明这个问题。

finally 语句剖析

说了这么多,还是让我们拿出些有说服力的证据吧!还有什么证据比官方的文档更具说服力呢?让我们来看看官方网站上的《The Java Tutorials》中是怎样来描述 finally 语句块的吧!

以下位于 **** 之间的内容原封不动的摘自于《 The Java Tutorials 》文档。

*******************************************************************************
The finally Block

The finally block always executes when the try block exits. This ensures that the finally block is executed even if an unexpected exception occurs. But finally is useful for more than just exception handling — it allows the programmer to avoid having cleanup code accidentally bypassed by a return, continue, or break. Putting cleanup code in a finally block is always a good practice, even when no exceptions are anticipated.

Note: If the JVM exits while the try or catch code is being executed, then the finally block may not execute. Likewise, if the thread executing the try or catch code is interrupted or killed, the finally block may not execute even though the application as a whole continues.
*******************************************************************************

请仔细阅读并认真体会一下以上两段英文,当你真正的理解了这两段英文的确切含义,你就可以非常自信的来回答“finally 语句块是否一定会执行?”这样的问题。看来,大多时候,并不是 Java 语言本身有多么高深,而是我们忽略了对基础知识的深入理解。

接下来,我们看一下 finally 语句块是怎样执行的。在排除了以上 finally 语句块不执行的情况后,finally 语句块就得保证要执行,既然 finally 语句块一定要执行,那么它和 try 语句块与 catch 语句块的执行顺序又是怎样的呢?还有,如果 try 语句块中有 return 语句,那么 finally 语句块是在 return 之前执行,还是在 return 之后执行呢?带着这样一些问题,我们还是以具体的案例来讲解。

关于 try、catch、finally 的执行顺序问题,我们还是来看看权威的论述吧!以下 **** 之间的内容摘自 Java 语言规范第四版(《 The Java™ Programming Language, Fourth Edition 》 )中对于 try,catch,和 finally 的描述。

*******************************************************************************
12.4. Try, catch, and finally

You catch exceptions by enclosing code in Try blocks. The basic syntax for a Try block is:

  1. try{
  2. statements
  3. }catch(exception_type1identifier1){
  4. statements
  5. }catch(exception_type2identifier2){
  6. statements
  7. ...
  8. }finally{
  9. statements
  10. }


where either at least one catch clause, or the finally clause, must be present. The body of the try statement is executed until either an exception is thrown or the body finishes successfully. If an exception is thrown, each catch clause is examined in turn, from first to last, to see whether the type of the exception object is assignable to the type declared in the catch. When an assignable catch clause is found, its block is executed with its identifier set to reference the exception object. No other catch clause will be executed. Any number of catch clauses, including zero, can be associated with a particular TRy as long as each clause catches a different type of exception. If no appropriate catch is found, the exception percolates out of the try statement into any outer try that might have a catch clause to handle it.

If a finally clause is present with a try, its code is executed after all other processing in the try is complete. This happens no matter how completion was achieved, whether normally, through an exception, or through a control flow statement such as return or break .
*******************************************************************************

上面这段文字的大体意思是说,不管 try 语句块正常结束还是异常结束,finally 语句块是保证要执行的。如果 try 语句块正常结束,那么在 try 语句块中的语句都执行完之后,再执行 finally 语句块。如果 try 中有控制转移语句(return、break、continue)呢?那 finally 语句块是在控制转移语句之前执行,还是之后执行呢?似乎从上面的描述中我们还看不出任何端倪,不要着急,后面的讲解中我们会分析这个问题。如果 try 语句块异常结束,应该先去相应的 catch 块做异常处理,然后执行 finally 语句块。同样的问题,如果 catch 语句块中包含控制转移语句呢? finally 语句块是在这些控制转移语句之前,还是之后执行呢?我们也会在后续讨论中提到。

其实,关于 try,catch,finally 的执行流程远非这么简单,有兴趣的读者可以参考 Java 语言规范第三版(《 The Java™ Language Specification, Third Edition 》 )中对于 Execution of try-catch-finally 的描述,非常复杂的一个流程。限于篇幅的原因,本文不做摘录,请感兴趣的读者自行阅读。

finally 语句示例说明

下面,我们先来看一个简单的例子(清单 3)。

清单 3.

  1. publicclassTest{
  2. publicstaticvoidmain(String[]args){
  3. try{
  4. System.out
  5. .println("tryblock");
  6. return;
  7. }finally{
  8. System.out
  9. .println("finallyblock");
  10. }
  11. }
  12. }

清单 3 的执行结果为

try block
finally block 

清单 3 说明 finally 语句块在 try 语句块中的 return 语句之前执行。我们再来看另一个例子(清单 4)。

清单 4.

  1. publicclassTest{
  2. publicstaticvoidmain(String[]args){
  3. System.out
  4. .println("returevalueoftest():"+test
  5. ());
  6. }
  7. publicstaticinttest(){
  8. inti=1;
  9. try{
  10. System.out
  11. .println("tryblock");
  12. i=1/0;
  13. return1;
  14. }catch(Exceptione){
  15. System.out
  16. .println("exceptionblock");
  17. return2;
  18. }finally{
  19. System.out
  20. .println("finallyblock");
  21. }
  22. }
  23. }

清单 4 的执行结果为:

try block
exception block
finally block
reture value of test() : 2 

清单 4 说明了 finally 语句块在 catch 语句块中的 return 语句之前执行。

从上面的清单 3 和清单 4,我们可以看出,其实 finally 语句块是在 try 或者 catch 中的 return 语句之前执行的。更加一般的说法是,finally 语句块应该是在控制转移语句之前执行,控制转移语句除了 return 外,还有 break 和 continue。另外,throw 语句也属于控制转移语句。虽然 return、throw、break 和 continue 都是控制转移语句,但是它们之间是有区别的。其中 return 和 throw 把程序控制权转交给它们的调用者(invoker),而 break 和 continue 的控制权是在当前方法内转移。请大家先记住它们的区别,在后续的分析中我们还会谈到。

还是得来点有说服力的证据,下面这段摘自 Java 语言规范第四版(《 The Java™ Programming Language, Fourth Edition 》 ),请读者自己体会一下其含义。

*******************************************************************************

A finally clause can also be used to clean up for break , continue , and return , which is one reason you will sometimes see a try clause with no catch clauses. When any control transfer statement is executed, all relevant finally clauses are executed. There is no way to leave a try block without executing its finally clause.

*******************************************************************************

好了,看到这里,是不是有人认为自己已经掌握了 finally 的用法了?先别忙着下结论,我们再来看两个例子 – 清单 5 和清单 6。

清单 5.

  1. publicclassTest{
  2. publicstaticvoidmain(String[]args){
  3. System.out
  4. .println("returnvalueofgetValue():"+getValue
  5. ());
  6. }
  7. publicstaticintgetValue(){
  8. try{
  9. return0;
  10. }finally{
  11. return1;
  12. }
  13. }
  14. }


清单 5 的执行结果:

return value of getValue(): 1 


清单 6.

  1. publicclassTest{
  2. publicstaticvoidmain(String[]args){
  3. System.out
  4. .println("returnvalueofgetValue():"+getValue
  5. ());
  6. }
  7. publicstaticintgetValue(){
  8. inti=1;
  9. try{
  10. returni;
  11. }finally{
  12. i++;
  13. }
  14. }
  15. }

清单 6 的执行结果:

return value of getValue(): 1 

利用我们上面分析得出的结论:finally 语句块是在 try 或者 catch 中的 return 语句之前执行的。 由此,可以轻松的理解清单 5 的执行结果是 1。因为 finally 中的 return 1,语句要在 try 中的 return 0;语句之前执行,那么 finally 中的 return 1;语句执行后,把程序的控制权转交给了它的调用者 main()函数,并且返回值为 1。那为什么清单 6 的返回值不是 2,而是 1 呢?按照清单 5 的分析逻辑,finally 中的 i++;语句应该在 try 中的 return i;之前执行啊? i 的初始值为 1,那么执行 i++;之后为 2,再执行 return i;那不就应该是 2 吗?怎么变成 1 了呢?

关于 Java 虚拟机是如何编译 finally 语句块的问题,有兴趣的读者可以参考《 The JavaTM Virtual Machine Specification, Second Edition 》中 7.13 节 Compiling finally。那里详细介绍了 Java 虚拟机是如何编译 finally 语句块。实际上,Java 虚拟机会把 finally 语句块作为 subroutine(对于这个 subroutine 不知该如何翻译为好,干脆就不翻译了,免得产生歧义和误解。)直接插入到 try 语句块或者 catch 语句块的控制转移语句之前。但是,还有另外一个不可忽视的因素,那就是在执行 subroutine(也就是 finally 语句块)之前,try 或者 catch 语句块会保留其返回值到本地变量表(Local Variable Table)中。待 subroutine 执行完毕之后,再恢复保留的返回值到操作数栈中,然后通过 return 或者 throw 语句将其返回给该方法的调用者(invoker)。请注意,前文中我们曾经提到过 return、throw 和 break、continue 的区别,对于这条规则(保留返回值),只适用于 return 和 throw 语句,不适用于 break 和 continue 语句,因为它们根本就没有返回值。

 

是不是不太好理解,那我们就用具体的例子来做形象的说明吧!

为了能够解释清单 6 的执行结果,我们来分析一下清单 6 的字节码(byte-code):

Compiled from "Test.java"
public class Test extends java.lang.Object{
public Test();
  Code:
   0: aload_0
   1:invokespecial#1; //Method java/lang/Object."":()V    4: return    LineNumberTable:    line 1: 0  public static void main(java.lang.String[]);   Code:    0: getstatic #2; //Field java/lang/System.out:Ljava/io/PrintStream;    3: new #3; //class java/lang/StringBuilder    6: dup    7: invokespecial #4; //Method java/lang/StringBuilder."":()V    10: ldc #5; //String return value of getValue():    12: invokevirtual    #6; //Method java/lang/StringBuilder.append:(        Ljava/lang/String;)Ljava/lang/StringBuilder;    15: invokestatic #7; //Method getValue:()I    18: invokevirtual    #8; //Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;    21: invokevirtual    #9; //Method java/lang/StringBuilder.toString:()Ljava/lang/String;    24: invokevirtual #10; //Method java/io/PrintStream.println:(Ljava/lang/String;)V    27: return  public static int getValue();   Code:    0: iconst_1    1: istore_0    2: iload_0    3: istore_1    4: iinc 0, 1    7: iload_1    8: ireturn    9: astore_2    10: iinc 0, 1    13: aload_2    14: athrow   Exception table:    from   to  target type      2     4     9   any      9    10     9   any } 

对于 Test()构造方法与 main()方法,在这里,我们不做过多解释。让我们来分析一下 getValue()方法的执行。在这之前,先让我把 getValue()中用到的虚拟机指令解释一下,以便读者能够正确的理解该函数的执行。

1. iconst_
Description: Push the int constant  (-1, 0, 1, 2, 3, 4 or 5) onto the operand stack.
Forms: iconst_m1 = 2 (0x2)  iconst_0 = 3 (0x3)  iconst_1 = 4 (0x4) 
iconst_2 = 5 (0x5) iconst_3 = 6 (0x6)  iconst_4 = 7 (0x7)  iconst_5 = 8 (0x8) 
2. istore_
Description: Store int into local variable. The  must be an index into the
local variable array of the current frame.
Forms: istore_0 = 59 (0x3b)  istore_1 = 60 (0x3c)  istore_2 = 61 (0x3d) 
istore_3 = 62 (0x3e) 
3. iload_
Description: Load int from local variable. The  must be an index into the
local variable array of the current frame.
Forms: iload_0 = 26 (0x1a)  iload_1 = 27 (0x1b)  iload_2 = 28 (0x1c)  iload_3 = 29 (0x1d) 
4. iinc index, const
Description: Increment local variable by constant. The index is an unsigned byte that
must be an index into the local variable array of the current frame. The const is an
immediate signed byte. The local variable at index must contain an int. The value
const is first sign-extended to an int, and then the local variable at index is
incremented by that amount.
Forms:  iinc = 132 (0x84)

Format:
iinc
index
const 
5. ireturn
Description: Return int from method.
Forms:  ireturn = 172 (0xac) 
6. astore_
Description: Store reference into local variable. The  must be an index into the
local variable array of the current frame.
Forms: astore_0 = 75 (0x4b) astore_1 = 76 (0x4c) astore_2 =77 (0x4d) astore_3 =78 (0x4e) 
7. aload_
Description: Load reference from local variable. The  must be an index into the
local variable array of the current frame.
Forms: aload_0 = 42 (0x2a) aload_1 = 43 (0x2b) aload_2 = 44 (0x2c) aload_3 = 45 (0x2d) 
8. athrow
Description: Throw exception or error.
Forms: athrow = 191 (0xbf) 

有了以上的 Java 虚拟机指令,我们来分析一下其执行顺序:分为正常执行(没有 exception)和异常执行(有 exception)两种情况。我们先来看一下正常执行的情况,如图 1 所示:

图 1. getValue()函数正常执行的情况
图 1. getValue()函数正常执行的情况

由上图,我们可以清晰的看出,在 finally 语句块(iinc 0, 1)执行之前,getValue()方法保存了其返回值(1)到本地表量表中 1 的位置,完成这个任务的指令是 istore_1;然后执行 finally 语句块(iinc 0, 1),finally 语句块把位于 0 这个位置的本地变量表中的值加 1,变成 2;待 finally 语句块执行完毕之后,把本地表量表中 1 的位置上值恢复到操作数栈(iload_1),最后执行 ireturn 指令把当前操作数栈中的值(1)返回给其调用者(main)。这就是为什么清单 6 的执行结果是 1,而不是 2 的原因。

再让我们来看看异常执行的情况。是不是有人会问,你的清单 6 中都没有 catch 语句,哪来的异常处理呢?我觉得这是一个好问题,其实,即使没有 catch 语句,Java 编译器编译出的字节码中还是有默认的异常处理的,别忘了,除了需要捕获的异常,还可能有不需捕获的异常(如:RunTimeException 和 Error)。

从 getValue()方法的字节码中,我们可以看到它的异常处理表(exception table), 如下:

Exception table:
from to target type
2 4 9 any 

它的意思是说:如果从 2 到 4 这段指令出现异常,则由从 9 开始的指令来处理。

图 2. getValue()函数异常执行的情况
图 2. getValue()函数异常执行的情况

先说明一点,上图中的 exception 其实应该是 exception 对象的引用,为了方便说明,我直接把它写成 exception 了。

由上图(图 2)可知,当从 2 到 4 这段指令出现异常时,将会产生一个 exception 对象,并且把它压入当前操作数栈的栈顶。接下来是 astore_2 这条指令,它负责把 exception 对象保存到本地变量表中 2 的位置,然后执行 finally 语句块,待 finally 语句块执行完毕后,再由 aload_2 这条指令把预先存储的 exception 对象恢复到操作数栈中,最后由 athrow 指令将其返回给该方法的调用者(main)。

通过以上的分析,大家应该已经清楚 try-catch-finally 语句块的执行流程了吧!

为了更具说服力,我们还是来引经据典吧!大家可以不相信我,难道还不相信“高司令”(Gosling)吗?下面这段仍然摘自 Java 语言规范第四版 《 The Java™ Programming Language, Fourth Edition 》 ,请读者自己体会吧!

*******************************************************************************
a finally clause is always entered with a reason. That reason may be that the try code finished normally, that it executed a control flow statement such as return, or that an exception was thrown in code executed in the Try block. The reason is remembered when the finally clause exits by falling out the bottom. However, if the finally block creates its own reason to leave by executing a control flow statement (such as break or return) or by throwing an exception, that reason supersedes the original one, and the original reason is forgotten. For example, consider the following code:
try {
// … do something …
return 1;
} finally {
return 2;
}
When the Try block executes its return, the finally block is entered with the “reason” of returning the value 1. However, inside the finally block the value 2 is returned, so the initial intention is forgotten. In fact, if any of the other code in the try block had thrown an exception, the result would still be to return 2. If the finally block did not return a value but simply fell out the bottom, the “return the value 1 ″ reason would be remembered and carried out.
*******************************************************************************
好了,有了以上的知识,让我们再来看以下 3 个例子。

清单 7.

  1. publicclassTest{
  2. publicstaticvoidmain(String[]args){
  3. System.out
  4. .println("returnvalueofgetValue():"+getValue
  5. ());
  6. }
  7. @SuppressWarnings("finally")
  8. publicstaticintgetValue(){
  9. inti=1;
  10. try{
  11. i=4;
  12. }finally{
  13. i++;
  14. returni;
  15. }
  16. }
  17. }

清单 7 的执行结果:

return value of getValue(): 5 

清单 8.

  1. publicclassTest{
  2. publicstaticvoidmain(String[]args){
  3. System.out
  4. .println("returnvalueofgetValue():"+getValue
  5. ());
  6. }
  7. publicstaticintgetValue(){
  8. inti=1;
  9. try{
  10. i=4;
  11. }finally{
  12. i++;
  13. }
  14. returni;
  15. }
  16. }

清单 8 的执行结果:

return value of getValue(): 5 

清单 7 和清单 8 应该还比较简单吧!利用我们上面讲解的知识,很容易分析出其结果。让我们再来看一个稍微复杂一点的例子 – 清单 9。我建议大家最好先不要看执行结果,运用学过的知识来分析一下,看是否能推断出正确的结果。

清单 9.

  1. publicclassTest{
  2. publicstaticvoidmain(String[]args){
  3. System.out
  4. .println(test
  5. ());
  6. }
  7. publicstaticStringtest(){
  8. try{
  9. System.out
  10. .println("tryblock");
  11. returntest1
  12. ();
  13. }finally{
  14. System.out
  15. .println("finallyblock");
  16. }
  17. }
  18. publicstaticStringtest1(){
  19. System.out
  20. .println("returnstatement");
  21. return"afterreturn";
  22. }
  23. }

清单 9 的结果:

try block
return statement
finally block
after return 

你分析对了吗?其实这个案例也不算很难,return test1(); 这条语句等同于 :

  1. Stringtmp=test1();
  2. returntmp;

这样,就应该清楚为什么是上面所示的执行结果了吧!

好了,就写到这吧!希望大家看完这篇文章能够有所收获!

总结

没想到吧!一个小小的、看似简单的 finally 语句块背后居然隐藏了这么多玄机。看来,我们平时还是应该认真的阅读 Java 相关的基础文档,比如:Java 语言规范、Java 虚拟机规范等,很多棘手的问题都可以从中得到答案。只有真正的吃透了基础知识,才能达到运用自如的境界!

原文链接:http://run-wang.iteye.com/blog/1262063

 

分享到:
评论

相关推荐

    完全解析Java编程中finally语句的执行原理

    主要深度介绍了Java编程中finally语句的执行原理,细致讲解了finally在异常处理中的流程控制作用,需要的朋友可以参考下

    Java中finally和return的关系实例解析

    主要介绍了Java中finally和return的关系实例解析,总结了二者的关系,然后分享了相关代码示例,小编觉得还是挺不错的,具有一定借鉴价值,需要的朋友可以参考下

    java解析给定url

    HtmlParse,解析给定url中的中文字符,输出到文本文件中: url:可配置多个 输出路径:可配置 package com.lhs; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import ...

    java面试题解析困惑之一

    java面试题解析困惑之final、finally和finalize的区别

    深入Java虚拟机(原书第2版).pdf【附光盘内容】

    第18章 finally子句 18.1 微型子例程 18.2 不对称的调用和返回 18.3 一个模拟:“hop around” 18.4 随书光盘 18.5 资源页 第19章 方法的调用与返回 19.1 方法调用 19.1.1 java方法的调用 19.1.2...

    Java经典面试题(答案解析)

    含基础篇,核心篇,框架篇,微服务篇 1.1.1 面向对象特征 封装,继承,多态和抽象 1、 封装 封装给对象提供了隐藏内部特性和行为的能力。对象提供一些能被其他对象访问...1.1.2 final, finally, finalize 的区别。。。

    java抛出异常与finally实例解析

    主要介绍了java抛出异常与finally实例解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

    java jdk实列宝典 光盘源代码

    表达式解析器;字符串编码的转换;字符串对齐器;密码加密和验证(MD5); 制作命令行程序;使用正则表达式操作字符串;使用正则表达式验证电话号码的格式; 6java异常处理 throw和throws、try和catch;自定义异常类...

    Java程序设计习题(含答案解析).doc

    目 录 一、判断题 2 二、选择题 4 三、填空题 17 四、写出下列程序完成的功能 25 五、写出下面程序的运行结果 29 六、使用Java语言编写程序 33 七、简答题 34 Java程序设计习题集 一、判断题 1.Java程序里,创建新的...

    深入Java虚拟机

    第18章 finally子句 18.1 微型子例程 18.2 不对称的调用和返回 18.3 一个模拟:“Hop Around” 18.4 随书光盘 18.5 资源页 第19章 方法的调用与返回 19.1 方法调用 19.1.1 Java方法的调用 19.1.2...

    使用Java XML解析器实现一个简单的XML文档操作.txt

    这个代码实现了一个简单的XML文档操作。首先导入了所需的库,然后定义了一个名为SimpleXML的类,并在...在finally块中关闭了所有的资源。由于使用了DOM解析器来访问XML文档,因此可以方便地获取和修改XML文档中的数据。

    Java学习题答案

    《Java学习题》 <br>据说,如果你能独立完成(不用看资料,不借助调试)回答在60分以上,找个6000元以上的工作由***来给你介绍。 <br>1.简述逻辑操作(&,|,^)与条件操作(&&,||)的区别。(15分) 2.下面程序运行会...

    java面试要点集合

    1、xml解析方式 2、ejb几种类型 3、String和StringBuffer区别 4、jsp有哪些动作标记 5、动态Include和静态Include的区别 6、Exception中Finally的用法 7、jsp的内置对象有哪些 北京环贸通科技有限公司 1、jsp两种...

    java基础案例与开发详解案例源码全

    2.3.5 常见错误解析24 2.4 Java类库组织结构和文档27 2.5 Java虚拟机简介28 2.6 Java技术两种核心运行机制29 2.7 上机练习30 第3章 3.1 变量32 3.1.1 什么是变量32 3.1.2 为什么需要变量32 3.1.3 变量的声明和赋值33...

    JAVA 范例大全 光盘 资源

    实例76 使用finally避免资源漏洞 189 实例77 反射机制 191 第10章 I/O及文件操作 196 实例78 创建文件和目录 196 实例79 查找文件 199 实例80 删除文件夹和文件 201 实例81 文件复制与移动 204 实例82 多种...

    Java JDK实例宝典

    7 表达式解析器 5. 8 字符串编码的转换 5. 9 字符串对齐器 5. 10 密码加密与验证 5. 11 制作命令行程序 5. 12 使用StringTokenizer 5. 13 使用正则表达式操作字符串 5. 14 使用正则...

    深入java虚拟机第二版

    第18章 finally子句 18.1 微型子例程 18.2 不对称的调用和返回 18.3 一个模拟:“Hop Around” 18.4 随书光盘 18.5 资源页 第19章 方法的调用与返回 19.1 方法调用 19.1.1 Java方法的调用 19.1.2 本地方法...

    java面试800题

    2.因为SQL是从右向左解析,多表查询时,记录数少的表放在右边 3.多个条件时,收敛快的条件放在右边。 4.避免使用复杂的集合函数,象not in等。 5.避免在条件中对字段进行函数操作 6.尽量避免使用select *,应该写出...

    深入JAVA虚拟机(第2版)

    第18章 finally子句 18.1 微型子例程 18.2 不对称的调用和返回 18.3 一个模拟:“Hop Around” 18.4 随书光盘 18.5 资源页 第19章 方法的调用与返回 19.1 方法调用 19.1.1 Java方法的调用 19.1.2 ...

Global site tag (gtag.js) - Google Analytics