final

Comparing strings with == which are declared final in Java

别等时光非礼了梦想. 提交于 2019-11-25 23:39:10
问题 I have a simple question about strings in Java. The following segment of simple code just concatenates two strings and then compares them with == . String str1=\"str\"; String str2=\"ing\"; String concat=str1+str2; System.out.println(concat==\"string\"); The comparison expression concat==\"string\" returns false as obvious (I understand the difference between equals() and == ). When these two strings are declared final like so, final String str1=\"str\"; final String str2=\"ing\"; String

What is the point of “final class” in Java?

最后都变了- 提交于 2019-11-25 23:38:37
问题 I am reading a book about Java and it says that you can declare the whole class as final . I cannot think of anything where I\'d use this. I am just new to programming and I am wondering if programmers actually use this on their programs . If they do, when do they use it so I can understand it better and know when to use it. If Java is object oriented, and you declare a class final , doesn\'t it stop the idea of class having the characteristics of objects? 回答1: If they do, when do they use it

Change private static final field using Java reflection

与世无争的帅哥 提交于 2019-11-25 22:55:29
问题 I have a class with a private static final field that, unfortunately, I need to change it at run-time. Using reflection I get this error: java.lang.IllegalAccessException: Can not set static final boolean field Is there any way to change the value? Field hack = WarpTransform2D.class.getDeclaredField(\"USE_HACK\"); hack.setAccessible(true); hack.set(null, true); 回答1: Assuming no SecurityManager is preventing you from doing this, you can use setAccessible to get around private and resetting the

In ArrayBlockingQueue, why copy final member field into local final variable?

泄露秘密 提交于 2019-11-25 22:45:30
问题 In ArrayBlockingQueue , all the methods that require the lock copy it to a local final variable before calling lock() . public boolean offer(E e) { if (e == null) throw new NullPointerException(); final ReentrantLock lock = this.lock; lock.lock(); try { if (count == items.length) return false; else { insert(e); return true; } } finally { lock.unlock(); } } Is there any reason to copy this.lock to a local variable lock when the field this.lock is final ? Additionally, it also uses a local copy

How does the “final” keyword in Java work? (I can still modify an object.)

陌路散爱 提交于 2019-11-25 22:43:27
问题 In Java we use final keyword with variables to specify its values are not to be changed. But I see that you can change the value in the constructor / methods of the class. Again, if the variable is static then it is a compilation error. Here is the code: import java.util.ArrayList; import java.util.List; class Test { private final List foo; public Test() { foo = new ArrayList(); foo.add(\"foo\"); // Modification-1 } public static void main(String[] args) { Test t = new Test(); t.foo.add(\"bar

Cannot refer to a non-final variable inside an inner class defined in a different method

那年仲夏 提交于 2019-11-25 22:14:59
问题 Edited: I need to change the values of several variables as they run several times thorugh a timer. I need to keep updating the values with every iteration through the timer. I cannot set the values to final as that will prevent me from updating the values however I am getting the error I describe in the initial question below: I had previously written what is below: I am getting the error \"cannot refer to a non-final variable inside an inner class defined in a different method\". This is

pre_exam exercise 3

拟墨画扇 提交于 2019-11-25 21:32:11
>>> single_factors(2) 2 >>> single_factors(4096)   # 4096 == 2**12 2 >>> single_factors(85)   # 85 == 5 * 17 85 >>> single_factors(10440125)   # 10440125 == 5**3 * 17**4 85 >>> single_factors(154)   # 154 == 2 * 7 * 11 154 >>> single_factors(52399401037149926144)   # 52399401037149926144 == 2**8 * 7**2 * 11**15 154 def single_factors(number): dic_number={} for i in range(2,number+1): #这里一定是加1,不然当factor为质数时返回的结果只有1 while number%i==0: dic_number[i]=dic_number.get(i,0)+1 number=number//i #采用//可以极大的节约等待时间,因为操作对象是证书而不再是浮点数 if number==1: break final=1 for m in dic_number.keys(): final=final*m return

Why is the String class declared final in Java?

谁都会走 提交于 2019-11-25 18:43:40
From when I learned that the class java.lang.String is declared as final in Java, I was wondering why that is. I didn't find any answer back then, but this post: How to create a replica of String class in Java? reminded me of my query. Sure, String provides all the functionality I ever needed, and I never thought of any operation that would require an extension of class String, but still you'll never know what someone might need! So, does anyone know what the intent of the designers was when they decided to make it final? It is very useful to have strings implemented as immutable objects . You