简体   繁体   中英

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. 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) . Is there the same functionality in Java and if so how would I do it?

Java does not have an equivalent facility. 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. This is actually inline with c#, where default of reference type is null and default of numeric types is 0.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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