Is there auto type inferring in Java?

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

    It's not a pure Java solution, however adding a library called lombok will enable the magic below to compile and work very much similar to auto keyword in C++

    List strList = Arrays.asList("foo", "bar", "baz");
    for (val s: strList){
        System.out.println(s.length());
    }
    

提交回复
热议问题