简体   繁体   English

Eclipse死代码警告,但是出了什么问题?

[英]Eclipse dead code warning, but what is wrong?

I've written a method that checks if my string is in a file, but eclipse gives dead code warning. 我已经编写了一种方法来检查我的字符串是否在文件中,但是eclipse会给出无效代码警告。 Here is my 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 is the source of the warning. else if(s==null)部分是警告的来源。 Why? 为什么? If you can't find any matching result (and coming outputs are all null), return false . 如果找不到任何匹配的结果(并且即将输出均为null),则return false I think it is OK. 我认为还可以。 What is wrong? 怎么了?

And one more question. 还有一个问题。 Which is better to use? 哪个更好用?

String s;
while(true){
    s="New Value";
    ....
}

or 要么

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

I think garbage collectors consume system resources, so the first one is better. 我认为垃圾收集器会消耗系统资源,因此第一个更好。 However, I see more examples on the second one. 但是,我在第二个例子中看到了更多示例。 Which one would you use? 您会使用哪一个?

Look at the whole if/else: 查看整个if / else:

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

If s is null, then s.equals(key) will have thrown a NullPointerException - so you'll never get into the second if block. 如果s null,则s.equals(key)将引发NullPointerException因此,您永远不会进入第二个if块。

You should use a try-with-resources block anyway, and personally I wouldn't catch the IOException either... just let it bubble up: 无论如何,您都应该使用try-with-resources块,就我个人而言,我也不会捕获IOException ……只要让它冒泡:

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 that there's no garbage collection difference here; 注意这里没有垃圾收集差异。 declaring the variable before the loop just means you can assign the value within the while condition. 在循环之前声明变量仅意味着您可以在while条件内分配值。

If s is null, your first if will be executed and a NullPointerException will be thrown. 如果s为null,则将执行第一个if并将抛出NullPointerException。 Your else if can't be executed. 您的else if无法执行。 You have to switch your if and else if to handle the null pointer properly. 您必须切换ifelse if才能正确处理空指针。

Answerd is a simple: if s will by null, so in this row 回答很简单:如果s将为null,则在此行

s.equals(key)

NullPointerException will be thrown, and programm will never rich this NullPointerException将被抛出,并且programm将永远不会丰富此内容

else if (s == null)

condition. 条件。 And programm will rich this condition only if s != null; 仅当s!= null时,programm才会丰富此条件。 so this condition always will be false . 因此,这种情况总是错误的 You better rewrite this snippet this way: 您最好用以下方式重写此代码段:

while (true) {
            String s = fileReader.readLine();
            if (s == null) {
                fileReader.close();
                return false;
            } else if (s.equals(key)) {
                fileReader.close();
                return true;
            }
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM