Java中的原始类

时间:2015-06-23 19:04:23

标签: java class types primitive

int.classdouble.class

等类的用途是什么?

例如,您无法执行类似

的操作
    Object val=getValue();
    if(val instanceof int){

    }

int i = new int();

是的我知道这是不可能的,因为int不是Object类型。但是,需要拥有这些原始类是什么?我在Oracle documentation.

中没有看到任何明确的解释

2 个答案:

答案 0 :(得分:3)

它可用于反思。

例如,如果您想使用@Singleton查找以import org.junit.Test; import javax.inject.Inject; import javax.inject.Named; import dagger.Component; import dagger.Module; import dagger.Provides; // WordNet - this is the expensive shared resource we would // like to make a singleton class WordNet { final String path; public WordNet(@Named("path") String path) { System.out.println("Loading Fake wordnet from " + path); this.path = path; } } @Module class WordNetModule { final String path; public WordNetModule(String path) { this.path = path; } // uncommenting the line below causes errors //@Singleton @Provides public WordNet provideWordNet() { return new WordNet(path); } } interface TriggerFinder { } class TriggerFinderUsingWordnet implements TriggerFinder { WordNet wordNet; @Inject public TriggerFinderUsingWordnet(WordNet wordNet) { this.wordNet = wordNet; } } @Module(includes = WordNetModule.class) class TriggerFinderWithWordnetModule { @Provides public TriggerFinder provideTriggerFinder(TriggerFinderUsingWordnet triggerFinder) { return triggerFinder; } } interface ArgFinder { } class ArgFinderUsingWordnet implements ArgFinder { WordNet wordNet; @Inject public ArgFinderUsingWordnet(WordNet fakeWordNet) { this.wordNet = fakeWordNet; } } @Module(includes = WordNetModule.class) class ArgFinderWithWordNetModule { @Provides public ArgFinder provideArgFinder(ArgFinderUsingWordnet argFinder) { return argFinder; } } // the composite object we wish to create class EventFinder { private final TriggerFinder triggerFinder; private final ArgFinder argFinder; @Inject public EventFinder(TriggerFinder triggerFinder, ArgFinder argFinder) { this.triggerFinder = triggerFinder; this.argFinder = argFinder; } } // components to wire everything together interface ArgFinderComponent { ArgFinder argFinder(); } interface TriggerFinderComponent { TriggerFinder triggerFinder(); } @Component(modules = ArgFinderWithWordNetModule.class) interface ArgFinderUsingWordNetComponent extends ArgFinderComponent { } @Component(modules = TriggerFinderWithWordnetModule.class) interface TriggerFinderUsingWordNetComponent extends TriggerFinderComponent { } interface EventFinderComponent { EventFinder eventFinder(); } @Component(dependencies = {ArgFinderUsingWordNetComponent.class, TriggerFinderUsingWordNetComponent.class}) interface WithWordNetEventFinderComponent extends EventFinderComponent { } public class DaggerComponentTest { @Test public void withWordNetTest() { final WordNetModule wordNetModule = new WordNetModule("myPath"); DaggerWithWordNetEventFinderComponent.builder() .argFinderUsingWordNetComponent( DaggerArgFinderUsingWordNetComponent.builder().wordNetModule(wordNetModule).build()) .triggerFinderUsingWordNetComponent( DaggerTriggerFinderUsingWordNetComponent.builder().wordNetModule(wordNetModule).build()) .build().eventFinder(); } } 作为参数的方法,您可能会执行以下操作:

#scheduler .k-scheduler-header-wrap .k-scheduler-table > tbody > tr:nth-child(2),
#scheduler .k-scheduler-layout > tbody > tr:first-child > td > .k-scheduler-times .k-scheduler-table > tbody > tr:nth-child(2) {
       display: none;
   }

答案 1 :(得分:3)

在Java中,X.class类文字的语法。当X是类或接口的名称时,表达式X.class 将计算为表示该类或接口类型的Class对象(它们是引用类型)。

由于还有基本类型的Class对象(在Nicholas中指出的反射中使用),为了保持一致性,他们扩展了语法,以便对于基本类型{{1} },表达式X也计算表示该基本类型的Class对象。

#Note:对于所有基本类型,此值也可用作相应包装类的静态字段X.class。因此TYPE相当于int.class