2

I'm not entirely sure what the issue is, but here is a snippet of my code. I get the warning for my line variable

StringBuffer stringBuffer = new StringBuffer();

String line = "" ;
while(( line = bufferedReader.readLine()) != null ){
    stringBuffer.append(line);
}
1
  • Why not use newer APIs? new String(Files.readAllBytes(...), UTF-8) Commented Dec 15, 2018 at 16:19

2 Answers 2

2

The warning simply means that the empty string with which the line variable is initialized is useless.

Before line is read, it is being assigned in the loop declaration:

line = bufferedReader.readLine()

So assigning line = "" is redundant. You can leave it uninitialized:

String line;
while((line = bufferedReader.readLine()) != null) {
    stringBuffer.append(line);
}
Sign up to request clarification or add additional context in comments.

Comments

1

Append to @ernest_k answer, assigning line = "" is redundant only if you reassign line with another value and you don't use line in return statement or set statement (set value need at least null value).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.