Is there auto type inferring in Java?

后端 未结 6 875
予麋鹿
予麋鹿 2021-01-30 18:54

Is there an auto variable type in Java like you have in C++?

An example:

for ( auto var : object_array)
    std::cout << var <<          


        
6条回答
  •  时光取名叫无心
    2021-01-30 19:49

    Might be Java 10 has what you (and I) want, through the var keyword.

    var list = new ArrayList();  // infers ArrayList
    var stream = list.stream();          // infers Stream
    

    From JDK Enhancement Proposals 286


    Update: Yap, that feature made it into the Java 10 release!

提交回复
热议问题