Is there auto type inferring in Java?

后端 未结 6 867
予麋鹿
予麋鹿 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:55

    Java 7 introduces the diamond syntax

    Box integerBox = new Box<>(); // Java 7
    

    As compared to old java

    Box integerBox = new Box(); // Before Java 7
    

    The critical reader will notice that this new syntax doesn't help with writing the for loops in the original question. That's correct and fully intentional it seems. See the other answer that cites Oracle's bug database.

提交回复
热议问题