java - Eclipse dead code warning, but what is wrong? -


i've written method checks if string in file, eclipse gives dead code warning. here method:

private boolean keypresent(string key){     try{         bufferedreader filereader=new bufferedreader(new filereader(keypath));           while(true){             string s=filereader.readline();             if(s.equals(key)){                 filereader.close();                 return true;             }else if(s==null){                 filereader.close();                 return false;             }         }     }catch(ioexception e){             e.getstacktrace()             return false;     } } 

the else if(s==null) part source of warning. why? if can't find matching result(and coming outputs null), return false. think ok. wrong?

and 1 more question. better use?

string s; while(true){     s="new value";     .... } 

or

while(true){     string s="new value";     ... } 

i think garbage collectors consume system resources, first 1 better. however, see more examples on second one. 1 use?

thanks...

look @ whole if/else:

if (s.equals(key)) {     ... } else if (s == null) {     ... } 

if s is null, s.equals(key) have thrown nullpointerexception - you'll never second if block.

you should use try-with-resources block anyway, , wouldn't catch ioexception either... let bubble up:

private boolean keypresent(string key) throws ioexception {     try (bufferedreader reader = new bufferedreader(new filereader(keypath))) {         string line;         while ((line = reader.readline()) != null) {             if (line.equals(key)) {                 return true;             }         }         return false;     } } 

note there's no garbage collection difference here; declaring variable before loop means can assign value within while condition.


Comments

Popular posts from this blog

jOOQ update returning clause with Oracle -

java - Warning equals/hashCode on @Data annotation lombok with inheritance -

java - BasicPathUsageException: Cannot join to attribute of basic type -