final

Use of final keyword in Java method performance? [duplicate]

蓝咒 提交于 2019-12-06 03:12:26
This question already has an answer here: Does use of final keyword in Java improve the performance? 11 answers Does the use final in method arguments allow the compiler oor runtime environment to work faster? For example, if you have a variable to pass to a method that you know will not be modified and be used as is, is it more efficient to declare it final ? Example: The first method should be faster than the second method public int isLargerAfterTripledFaster(int num, final int limit) { num *= 3; return (num > limit); } public int isLargerAfterTripled(int num, int limit) { num *= 3; return

Java method keyword “final” and its use

半城伤御伤魂 提交于 2019-12-06 02:57:26
问题 When I create complex type hierarchies (several levels, several types per level), I like to use the final keyword on methods implementing some interface declaration. An example: interface Garble { int zork(); } interface Gnarf extends Garble { /** * This is the same as calling {@link #zblah(0)} */ int zblah(); int zblah(int defaultZblah); } And then abstract class AbstractGarble implements Garble { @Override public final int zork() { ... } } abstract class AbstractGnarf extends AbstractGarble

How could not using “final” be a security issue?

随声附和 提交于 2019-12-05 21:49:36
问题 From page 113 of O'Reilly's Essential ActionScript 3.0 (2007): Methods that are final help hide a class’s internal details. Making a class or a method final prevents other programmers from extending the class or overriding the method for the purpose of examining the class’s internal structure. Such prevention is considered one of the ways to safeguard an application from being maliciously exploited . Does this refer to users of the API of a compiled, closed-source package, and "maliciously

Unexpected output when using a ternary operator and final variable

江枫思渺然 提交于 2019-12-05 16:49:05
问题 Consider this code snippet: public static void main(String[] args) { int z1 = 0; final int z2 = 0; System.out.println(false ? z1 : 'X'); System.out.println(false ? z2 : 'X'); } When running this code, I would expect to see two X in your console. However, the real output is: 88 X If we take a look at the Java specifications regarding the ternary operator, we found that If one of the operands is of type T where T is byte, short, or char, and the other operand is a constant expression of type

Why is Java's BigDecimal class not declared as final?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-05 16:43:13
问题 While checking the source code of Java's BigDecimal class, I was surprised that it was not declared as a final class : Class BigDecimal public class BigDecimal extends Number implements Comparable<BigDecimal> Immutable , arbitrary-precision signed decimal numbers. (from the Oracle Docs) Is there a specific reason for this or did the developers just forget to add that keyword? Is it a good practice to not declare immutable classes as final? The same goes for BigInteger, but not for String

java.final关键字和权限修饰符

和自甴很熟 提交于 2019-12-05 15:36:54
概述 final关键字用于修饰不可改变的内容 final:不可改变。可以用于修饰类、方法和变量 类:被修饰的类不能被继承 方法:被修饰的方法不能被重写 变量:被修饰的变量不能被重新赋值 修饰类 final class 类名{ } 修饰方法 修饰符 final 返回值类型 方法名(参数列表){ //方法体 } 修饰变量 1、局部变量--基本类型 基本类型的局部变量,被final修饰后,只能赋值一次,不能再更改 public class Demo01 { public static void main(String[] args) { //声明变量,使用final修饰 final int a; //第一次赋值 a = 10; //第二次赋值 //a = 20; //报错,不可重新赋值 //声明变量,直接赋值,使用final修饰 final int b = 10; //第二次赋值 b = 20;//报错,不可重复赋值 } } 2、局部变量--引用类型 引用类型的局部变量,被final修饰后,只能指向一个对象,地址不能再更改,但是不影响对象内部的成员变量值的修改 public class Demo02 { public static void main(String[] args) { //创建user对象 final User u = new User(); //创建另一个User对象

Final Keyword in Constant utility class

£可爱£侵袭症+ 提交于 2019-12-05 12:28:53
Is the any difference in performance and/or any other benefits we can get when using final keyword with constant utility class. [ This class contains only static final fields and private constructor to avoid object creation] public class ActionConstants { private ActionConstants() // Prevents instantiation { } public static final String VALIDFIRSTLASTNAME = "[A-Za-z0-9.\\s]+"; public static final String VALIDPHONENUMBER = "\\d{10}"; ... ... } Only diffrence is class is made final public final class ActionConstants { private ActionConstants() // Prevents instantiation { } public static final

Why is the java.util.Scanner class declared 'final'?

99封情书 提交于 2019-12-05 11:20:33
I use the Scanner class for reading multiple similar files. I would like to extend it to make sure they all use the same delimiter and I can also add methods like skipUntilYouFind(String thisHere) that all valid for them all. I can make a utility-class that contain them, or embed the Scanner Class as a variable inside another class but this is more cumbersome. I have found some reasons to declare a class final, but why is it done here? Probably because extending it and overwriting some of it's methods would probably break it. And making it easier to overwrite methods would expose to much of

java nested interfaces and inner classes

笑着哭i 提交于 2019-12-05 10:57:10
Why can't a java nested Interface be non-static ? And why can't an inner class contain static non final members ? I came across the questions while going through Gosling and haven't been able to figure out the answer yet. If an nested class is non-static (i.e. an inner class), this means that each instance of it is bound to an instance of the outer class. As an interface has no instances of its own, it seems to not be useful for the implementing classes to be bound to an outer object, so having it static by default seems reasonable. I'm not sure why you can't have static non final members in

Scala final variables in constructor

☆樱花仙子☆ 提交于 2019-12-05 10:37:53
I'm still pretty new to Scala, but I know you can define class variables that are initialized in the constructor like class AClass(aVal: String) which would be like doing the following in java class AClass { private String aVal; public AClass(String aVal) { this.aVal = aVal; } } In Java, I would declare aVal as final. Is there a way to make the aVal variable final in the Scala syntax? EDIT: Here is what I am seeing when I compile the following Scala class: class AClass(aVal: String) { def printVal() { println(aVal) } } I ran javap -private and got the output public class AClass extends java