简体   繁体   English

在没有Java数据的情况下创建没有数据的对象而不是使用静态方法会花费多少钱?

[英]How much more expensive is the creation of an object without data in Java instead of the use of static methods?

Java lacks the ability to specify interfaces for static methods. Java缺乏为静态方法指定接口的能力。 A method in an interface must be non static. 接口中的方法必须是非静态的。 This makes it impossible to specify requirements for Classes. 这使得无法指定类的要求。 Instead one is limited to specify requirements for Objects. 相反,只能指定对象的要求。 This makes it also impossible for example to specify the singleton functionality in an interface, because in Java the singleton pattern requires to be implemented as a static method. 这使得例如在接口中指定单例功能也是不可能的,因为在Java中,单例模式需要实现为静态方法。 Here is a nice article, which explains it, but it is only in German. 是一篇很好的文章,它解释了它,但它只是用德语。

When one is forced to implement something as a functionality of an object instead of the functionality of a class, an instance of this object has to be created, before the functionality can be used. 当强制实现某个对象的功能而不是类的功能时,必须先创建该对象的实例,然后才能使用该功能。 But such object has some special characteristic: it has no state, because class functionality has no state either. 但是这样的对象有一些特殊的特征:它没有状态,因为类功能也没有状态。 Theoretically the instance creation of an object without data can be optimized to an NOP , because all methods can be linked to the class instead of any object. 从理论上讲,没有数据的对象的实例创建可以优化为NOP ,因为所有方法都可以链接到类而不是任何对象。 Java could implement some kind of implicit singleton functionality. Java可以实现某种隐式单例功能。

But how it this actually handled? 但这实际上是如何处理的?

Think about some kind of functionality without any state. 想想某种没有任何状态的功能。

interface Adder<T> { T add(T ... arguments); }

Basically it would be sufficient to implement this as a static method: 基本上将它作为静态方法实现就足够了:

class IntegerAdder implements Adder<Integer> {
   public static Integer add (Integer ... arguments) { }
}

But because Java does not allow static interface methods it has to be implemented in a non static way. 但是因为Java不允许静态接口方法,所以它必须以非静态方式实现。 The result is, that when ever an IntegerAdder is required one has to create an instance. 结果是,当需要IntegerAdder ,必须创建一个实例。

IntegerAdder integer_adder = new IntegerAdder();
Integer a = 1;
Integer b = 2;
Integer c = integer_adder.add (1, 2);

I fear this might be slower than the version without the instance creation: 我担心这可能比没有实例创建的版本慢:

Integer a = 1;
Integer b = 2;
Integer c = IntegerAdder.add (1, 2);

But how much slower is it in reality? 但实际上它慢了多少? Is it possible for the Java compiler to optimize the first version in that way that it performs as fast as the second one? Java编译器是否有可能以与第二个版本一样快的速度优化第一个版本? And is this actually done? 这实际上完成了吗?

You can create an instance of IntegerAdder once and reuse it, it is thread safe. 您可以创建一次IntegerAdder实例并重复使用它,它是线程安全的。 Also pay attention that Integer ... arguments leads to 1) using objects instead of primitive ints 2) creating an array to pass parameters. 还要注意Integer ... arguments导致1)使用对象而不是原始int 2)创建一个数组来传递参数。 Both things should be avoided if performance is concern 如果考虑到性能,应该避免这两件事

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 有数百种方法的 Java object 贵吗? - Is a Java object with hundreds of methods expensive? 在Java中,在使用Java创建对象期间,静态方法在哪里? - In Java where do the static methods go during the creation of an object in java? 如何使用“静态工厂方法”而不是构造函数? - How to use “Static factory methods” instead of constructors? 如果没有静态方法或块,也没有对象,一个类可以获得多少内存 - How much memory does a class get if there is no static methods or blocks and no object Java - 只有一个类实例:使用静态方法代替? - Java - Only one instance of class: use static methods instead? 如何在没有getter方法的情况下使用java中的数据类? - How to use data classes in java, without getter Methods? 在Java中,创建对象或获取对象值是否更昂贵? - In Java, is it more expensive to create an object, or get the objects value? 如何在Java中的其他静态方法中使用静态方法变量? - How to use static method variables in other static methods in Java? Java使用实例方法而不是类/静态方法为每个实例化对象创建唯一ID - Java create a unique ID for each instantiated object using instance methods instead of class/static methods 静态ArrayList的价格是多少? - How expensive is a static ArrayList?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM