Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java get the default value of a type

Tags:

java

generics

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?

like image 843
Sean Lev Avatar asked Nov 01 '22 01:11

Sean Lev


1 Answers

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.

like image 175
Mureinik Avatar answered Nov 15 '22 05:11

Mureinik