Use of final class in Java

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 , doe

在Java中使用final类

我正在阅读一本关于Java的书,它说你可以宣布整个班级都是final 。 我想不出任何我会使用它的地方。 我只是编程的新手,我想知道程序员是否真的在程序中使用它 。 如果他们这样做,他们什么时候使用它,这样我可以更好地理解它,并知道何时使用它。 如果Java是面向对象的,并且你声明了一个类final ,它不会阻止类具有对象特性的想法吗? 如果他们这样做,他们什么时候使用它,这样我可以更好地理解它,并知道何时使用它

Does use of final keyword in Java improve the performance?

In Java we see lots of places where the final keyword can be used but its use is uncommon. For example: String str = "abc"; System.out.println(str); In the above case, str can be final but this is commonly left off. When a method is never going to be overridden we can use final keyword. Similarly in case of a class which is not going to be inherited. Does the use of final keyword in any

在Java中使用final关键字可以提高性能吗?

在Java中,我们看到很多可以使用final关键字的地方,但它的使用并不常见。 例如: String str = "abc"; System.out.println(str); 在上面的例子中, str可以是final但这通常是中止的。 当一个方法永远不会被覆盖时,我们可以使用final关键字。 同样的情况下,一个类不会被继承。 在任何或所有这些情况下使用final关键字是否真的可以提高性能? 如果是这样,那么怎么样? 请解释。 如果正确使用final对于性能确实非常

final keyword in method parameters

This question already has an answer here: Why should I use the keyword “final” on a method parameter in Java? 12 answers Java always makes a copy of parameters before sending them to methods. This means the final doesn't mean any difference for the calling code. This only means that inside the method the variables can not be reassigned. (note that if you have a final object, you can s

方法参数中的最终关键字

这个问题在这里已经有了答案: 为什么我应该在Java的方法参数中使用关键字“final”? 12个答案 Java在将它们发送给方法之前总是制作一个参数的副本。 这意味着最终并不意味着调用代码的任何区别。 这只意味着在方法内变量不能被重新分配。 (注意,如果你有最终的对象,你仍然可以改变对象的属性)。 有一种情况需要最终声明它 - 否则会导致编译错误 - 即将它们传递给匿名类。 基本示例: public FileFilter createFil

what's the point?

In Java, it is perfectly legal to define final arguments in interface methods and do not obey that in the implementing class, eg: public interface Foo { public void foo(int bar, final int baz); } public class FooImpl implements Foo { @Override public void foo(final int bar, int baz) { ... } } In the above example, bar and baz has the opposite final definitions in the c

重点是什么?

在Java中,在接口方法中定义final参数并且不遵守实现类中的final参数是完全合法的,例如: public interface Foo { public void foo(int bar, final int baz); } public class FooImpl implements Foo { @Override public void foo(final int bar, int baz) { ... } } 在上面的例子中, bar和baz在类VS接口中有相反的final定义。 以同样的方式,当一个类方法扩展另一个类时,不论是否abstract ,

Is an object that contains a fixed set of mutable objects mutable?

Mutability still confuses me from time to time. If I have an object that contains a fixed set of mutable objects. Do you consider this object mutable? Example: I have an object called Puzzle which containts a fixed set of Pieces. The Pieces are mutable. For example, they can be upsideDown and its orientation can change. Assuming you cannot loose pieces in this example (if only that were t

包含一组固定可变对象的对象是否可变?

易变性仍然让我时不时困惑。 如果我有一个包含一组固定可变对象的对象。 你认为这个对象是可变的吗? 例子:我有一个叫Puzzle的对象,它包含一组固定的Pieces。 碎片是可变的。 例如,它们可以向上并且其方向可以改变。 假设在这个例子中你不能松动棋子(如果在现实生活中这只是真的......),我猜Puzzle对象仍然是不变的,对吗? 从维基百科:在面向对象和函数式编程中,不可变对象(不可更改对象)是一个对象,其状

C++: Reasons for passing objects by value

In Java, all variables containing proper objects are actually references (ie pointers). Therefore, method calls with these objects as arguments are always "by reference". Calling a method which modifies the state of the object also affects the original object (on the caller side). C++ is different: Here arguments can be passed by value or passed by reference. Calling a mutator meth

C ++:按值传递对象的原因

在Java中,包含正确对象的所有变量实际上都是引用(即指针)。 因此,以这些对象作为参数的方法调用总是“通过引用”。 调用修改对象状态的方法也会影响原始对象(在调用者方)。 C ++是不同的:这里的参数可以通过值传递或通过引用传递。 对通过值传递的对象调用增变器方法会使原始对象不受影响。 (我想通过值调用创建对象的本地副本)。 所以我对Java的第一个反应 - 从Java到C ++ - 是:总是在使用对象作为参数时使用

Why can't static methods be abstract in Java

The question is in Java why can't I define an abstract static method? for example abstract class foo { abstract void bar( ); // <-- this is ok abstract static void bar2(); //<-- this isn't why? } Because "abstract" means: "Implements no functionality", and "static" means: "There is functionality even if you don't have an object instance&

为什么静态方法不能在Java中抽象

问题是在Java中为什么我不能定义一个抽象的静态方法? 例如 abstract class foo { abstract void bar( ); // <-- this is ok abstract static void bar2(); //<-- this isn't why? } 因为“抽象”意味着:“不实现任何功能”,“静态”意味着:“即使没有对象实例,也有功能”。 这是一个合乎逻辑的矛盾。 糟糕的语言设计。 直接调用静态抽象方法比创建仅用于抽象方法的实例要有效得多。 当使用抽象类作为enum无法

Are static variables shared between threads?

My teacher in an upper level java class on threading said something that I wasn't sure of. He stated that the following code would not necessarily update the ready variable. According to him, the two threads don't necessarily share the static variable, specifically in the case when each thread (main thread versus ReaderThread) is running on its own processor and therefore doesn't s

线程之间是否共享静态变量?

我在一个关于线程的高级java类的老师说了一些我不确定的东西。 他表示,下面的代码不一定会更新ready变量。 据他介绍,这两个线程不一定共享静态变量,特别是在每个线程(主线程与ReaderThread)在其自己的处理器上运行并且因此不共享相同的寄存器/缓存/等等以及一个CPU不会更新其他。 从本质上讲,他说主线程中ready可能被更新,而不是在ReaderThread中,所以ReaderThread将无限循环。 他还声称该程序可以打印'0'

How do I use an equivalent to C++ reference parameters in Java?

Suppose I have this in C++: void test(int &i, int &j) { ++i; ++j; } The values are altered inside the function and then used outside. How could I write a code that does the same in Java? I imagine I could return a class that encapsulates both values, but that seems really cumbersome. Simulating reference with wrappers. One way you can have this behavior somehow simulated i

如何在Java中使用与C ++参考参数等效的内容?

假设我在C ++中有这个: void test(int &i, int &j) { ++i; ++j; } 这些值在函数内部被改变,然后在外部使用。 我如何编写一个在Java中执行相同的代码? 我想我可以返回一个封装两个值的类,但这看起来很麻烦。 用包装器模拟引用。 你可以通过某种方式模拟这种行为的一种方法是创建一个通用包装器。 public class _<E> { E ref; public _( E e ){ ref = e; } public E g(

value, reference variables

I have a problem with understanding the "pass-by-value" action of Java in the following example: public class Numbers { static int[] s_ccc = {7}; static int[] t_ccc = {7}; public static void calculate(int[] b, int[] c) { System.out.println("s_ccc[0] = " + s_ccc[0]); // 7 System.out.println("t_ccc[0] = " + t_ccc[0]); // 7 b[0] = b[0] + 9;

价值,参考变量

在以下示例中,我对理解Java的“传值”操作存在问题: public class Numbers { static int[] s_ccc = {7}; static int[] t_ccc = {7}; public static void calculate(int[] b, int[] c) { System.out.println("s_ccc[0] = " + s_ccc[0]); // 7 System.out.println("t_ccc[0] = " + t_ccc[0]); // 7 b[0] = b[0] + 9; System.out.println("nb[0] = " + b[0]); // 16 c