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

前端 未结 6 1815
攒了一身酷
攒了一身酷 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:47

    I prefer to initialize a String in Java as "", purely because I try to avoid null pointers. Null pointers add a lot of overhead that can be removed by assigning "" instead of null.

提交回复
热议问题