Should Java String method (local) variables be initialized to null or “”?

前端 未结 6 1822
攒了一身酷
攒了一身酷 2021-02-13 16:55

What is the best practice for initializing a String method (local) variable to avoid the \"Variable might not have been initialized\" error in Java?

String s=null; or St

6条回答
  •  不思量自难忘°
    2021-02-13 17:40

    It all depends on what you want your program to do. Sometimes it can be useful to see whether a string has been set already or not at which null would be best. Other times you want to prevent NullPointers which makes "" better.

提交回复
热议问题