java: try finally blocks execution

This question already has an answer here: Does finally always execute in Java? 50 answers Strange finally behaviour? 7 answers When you return from try block, the return value is stored on the stack frame for that method. After that the finally block is executed. Changing the value in the finally block will not change the value already on the stack. However if you return again from th

java:尝试最后阻止执行

这个问题在这里已经有了答案: 最终总是在Java中执行? 50个答案 奇怪的最后的行为? 7个答案 从try块返回时,返回值将存储在该方法的堆栈帧中。 之后,finally块被执行。 更改finally块中的值不会更改堆栈中已有的值。 但是,如果您从finally块中再次返回,堆栈上的返回值将被覆盖,并且新的x将被返回。 如果你在finally块中打印x的值,你会知道它被执行,并且x的值将被打印出来。 static int test(){ int x

try/finally without catch and return value

This question already has an answer here: Does finally always execute in Java? 50 answers From the JLS (emphasis mine): If execution of the try block completes abruptly because of a throw of a value V, then there is a choice: [...] If the run-time type of V is not assignment compatible with a catchable exception class of any catch clause of the try statement, then the finally block is

尝试/最后没有捕获和返回值

这个问题在这里已经有了答案: 最终总是在Java中执行? 50个答案 来自JLS(强调我的): 如果try块的执行由于抛出一个值V而突然完成,那么有一个选择: [...] 如果V的运行时类型不是与try语句的任何catch子句的可捕获异常类分配兼容的,则执行finally块。 然后有一个选择: 如果finally块正常完成,那么try语句突然完成,因为抛出了值V. 如果因为S原因finally块突然完成,那么try语句因S原因突然完成( 并丢弃并

java

This question already has an answer here: Multiple returns: Which one sets the final return value? 6 answers If the return in the try block is reached, it transfers control to the finally block, and the function eventually returns normally (not a throw). If an exception occurs, but then the code reaches a return from the catch block, control is transferred to the finally block and the func

java的

这个问题在这里已经有了答案: 多重回报:哪一个设定最终回报值? 6个答案 如果try块中的return已到达,它将控制权交给finally块,并且函数最终正常返回(而不是一个throw)。 如果发生异常,但代码从catch块return ,则控制权将转移到finally块,并且函数最终会正常返回(而不是抛出)。 在你的例子中,你finally有了return ,所以无论发生什么,函数都会返回34 ,因为finally会有最后的(如果你愿意的话)。 尽管在

In Java, is the result of the addition of two chars an int or a char?

当添加'a' + 'b'它产生195.输出数据类型是char还是int ? The result of adding Java chars, shorts, or bytes is an int : Java Language Specification on Binary Numeric Promotion: If any of the operands is of a reference type, unboxing conversion (§5.1.8) is performed. Then: If either operand is of type double, the other is converted to double. Otherwise, if either operand is of

在Java中,添加两个字符的结果是int还是char?

当添加'a' + 'b'它产生195.输出数据类型是char还是int ? 添加Java字符,短裤或字节的结果是int : 关于二进制数字促销的Java语言规范: 如果任何操作数是引用类型,则执行拆箱转换(§5.1.8)。 然后: 如果其中一个操作数是double类型,另一个操作数转换为double。 否则,如果任一操作数的类型为float,则另一个操作数转换为float。 否则,如果任一操作数的类型为long,则另一个操作数转换为long。

Java 8 autoboxing + generics: different behaviour with variable vs. method

I found a piece of code that after switching from Java 7 to Java 8 stopped compiling. It does not feature any of the new Java 8 stuff like lambda or streams. I narrowed the problematic code down to the following situation: GenericData<Double> g = new GenericData<>(1d); Double d = g == null ? 0 : g.getData(); // type error!!! You can probably guess that GenericData 's construc

Java 8 autoboxing +泛型:变量与方法的不同行为

我发现从Java 7切换到Java 8后停止编译的一段代码。 它没有任何新的Java 8的东西,如lambda或流。 我将有问题的代码缩小到以下情况: GenericData<Double> g = new GenericData<>(1d); Double d = g == null ? 0 : g.getData(); // type error!!! 您大概可以猜测GenericData的构造函数具有该泛型类型的一个参数,而getData()方法只返回该泛型类型。 (完整的源代码见下文。) 现在困扰我的是,在Java 7中,

Different Data Types Operations double

I know that when having to do with operators in Java , and different data types, then the result is promoting to the larger of the data types So, because double is larger than float, when having for example double z=39.21; float w=2.1F; System.out.println(z+w ); The result z+w will be of double type. If I don't put the F on the float data type, then the float is assumed to be double. So

不同的数据类型操作加倍

我知道,当与Java中的运算符以及不同的数据类型有关时,结果会推广到更大的数据类型。因此,因为double大于float,所以举例时 double z=39.21; float w=2.1F; System.out.println(z+w ); 结果z + w将是双重类型。 如果我不把浮点数据类型放在F上,那么浮点数就是双倍的。 那么为什么下面的代码不能编译? double z=39.21; float w=2.1; System.out.println(z+w ); 如果z和w都被认为是双重的,结果不应该是双倍的吗? 相

Do Java 8 default methods break source compatibility?

It has generally been the case the Java source code has been forward compatible. Until Java 8, as far as I know, both compiled classes and source have been forward compatible with later JDK/JVM releases. [Update: this is not correct, see comments re 'enum', etc, below.] However, with the addition of default methods in Java 8 this appears to no longer be the case. For example, a librar

Java 8默认方法是否打破源代码兼容性?

通常情况下,Java源代码已向前兼容。 据我所知,在Java 8之前,编译的类和源都与后来的JDK / JVM版本向前兼容。 [更新:这是不正确的,见下面的评论重新'枚举',等等。]但是,随着在Java 8中添加默认方法,这似乎不再是这种情况。 例如,我一直使用的库有java.util.List的实现,它包含List<V> sort() 。 此方法返回已排序列表内容的副本。 该库作为jar文件依赖项进行部署,在使用JDK 1.8构建的项目中运行良

Is a Java string really immutable?

We all know that String is immutable in Java, but check the following code: String s1 = "Hello World"; String s2 = "Hello World"; String s3 = s1.substring(6); System.out.println(s1); // Hello World System.out.println(s2); // Hello World System.out.println(s3); // World Field field = String.class.getDeclaredField("value"); field.setAccessible(true); char[] value = (char[])field.g

Java字符串真的不可变吗?

我们都知道String在Java中是不可变的,但请检查以下代码: String s1 = "Hello World"; String s2 = "Hello World"; String s3 = s1.substring(6); System.out.println(s1); // Hello World System.out.println(s2); // Hello World System.out.println(s3); // World Field field = String.class.getDeclaredField("value"); field.setAccessible(true); char[] value = (char[])field.get(s1); value[6] =

Java += compiler/jre bug?

This question already has an answer here: Why don't Java's +=, -=, *=, /= compound assignment operators require casting? 11 answers This is the way += has worked from C . It's "feature" not a bug See Java's +=, -=, *=, /= compound assignment operators BTW You can try this char ch = '0'; ch *= 1.1; // ch = '4' Also see a post of mine http://vanillajava.blogspo

Java + =编译器/ jre错误?

这个问题在这里已经有了答案: 为什么Java的+ =, - =,* =,/ =复合赋值操作符需要转换? 11个答案 这是+ =从C开始工作的方式。 这是“功能”而不是bug 请参阅Java的+ =, - =,* =,/ =复合赋值运算符 顺便说一句你可以试试这个 char ch = '0'; ch *= 1.1; // ch = '4' 另见我的博文http://vanillajava.blogspot.com/2012/11/java-and-implicit-casting.html int a = a + (3/2.0); 因为: int + double导致一

Why does Java perform implicit type conversion from double to integer when using the "plus equals" operator?

Possible Duplicate: Varying behavior for possible loss of precision Code Sample A public class Test { public static void main(String[] args) { int i = 0; i = i + 1.5; } } Code Sample B public class Test { public static void main(String[] args

为什么Java在使用“plus equals”运算符时执行从double到integer的隐式类型转换?

可能重复: 变化的行为可能导致精度损失 代码示例A public class Test { public static void main(String[] args) { int i = 0; i = i + 1.5; } } 代码示例B public class Test { public static void main(String[] args) { int i = 0; i +=