简体   繁体   English

Java获取类型的默认值

[英]Java get the default value of a type

So I'm writing a program in Java after only using C# for a few years and I can't seem to find the answer to this anywhere. 所以我在使用C#几年后用Java编写程序,我似乎无法在任何地方找到答案。 When creating a generic<T> class in C#, if you want to set a variable to the default value of T , you use default(T) . 在C#中创建generic<T>类时,如果要将变量设置为默认值T ,则使用default(T) Is there the same functionality in Java and if so how would I do it? Java中是否有相同的功能,如果是这样,我该怎么做?

Java does not have an equivalent facility. Java没有相同的功能。 If you want to pass a value to a generic method you must pass it explicitly, even if you're just invoking the type's default constructor. 如果要将值传递给泛型方法,则必须显式传递它,即使您只是调用类型的默认构造函数。

the default of T in Java is null, as there is no support for primitive generics. Java中的默认值为null,因为不支持原始泛型。 This is actually inline with c#, where default of reference type is null and default of numeric types is 0. 这实际上与c#内联,其中引用类型的默认值为null,而数值类型的默认值为0。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM