String variable may not have been initialized (error diects to line 34)

前端 未结 4 555
太阳男子
太阳男子 2021-01-26 00:59

I\'ve been trying to get the string initialize, but to no avail. I have tried all of the solutions I have come across and I am not sure whether it is because of my ineptness or

4条回答
  •  抹茶落季
    2021-01-26 01:49

    You can initialize the values of Strings in two ways:

    String abc = "I love basketball";
    

    or

    String abc = new String("I love basketball");  
    

    What you are seeing is a warning and not an error. If you insist on not putting a value into the String, just do

    String abc = null;    
    

    or

    String abc = ""  
    

    In the latter case, your String is initialized to the characters between the quotations. Nothing, basically, but it is not null.

    To avoid potential NPE problems, compare the Strings as follows:

    "rock".equalsIgnoreCase(playerChoice);
    

提交回复
热议问题