为什么我在这个Java代码中收到"死代码"警告?

nid*_*r09 5 java dead-code

有人请告诉我为什么我在其他分支中收到死代码警告if (projectId != null)?如果我做对了,翻译认为projectId永远不会是空的 - 是吗?在我看来,这是不可能的......

Integer projectId = null;

if (!sprintTaskConnections.isEmpty())
    projectId = sprintTaskConnections.get(0).getProjectId();

// init name, state, startDate, endDate here

JiraSprint sprint = new JiraSprint(sprintInfo.getInt("id"), name, state, projectId, startDate, endDate);

if (projectId != null)
{
   ...
}
Run Code Online (Sandbox Code Playgroud)

即使我放了一个

sprintTaskConnections.add(new JiraSprintTaskConnection(1, 1, 1));
Run Code Online (Sandbox Code Playgroud)

或者a

sprintTaskConnections.clear();
Run Code Online (Sandbox Code Playgroud)

在...前面

if (!sprintTaskConnections.isEmpty())
projectId = sprintTaskConnections.get(0).getProjectId();
Run Code Online (Sandbox Code Playgroud)

结果总是一样的!

请帮助我,我现在还没有得到它!

小智 11

我没有JiraSprint代码,所以我无法确认这一点,但我怀疑JiraSprint构造函数int在projectId中传递的位置而不是Integer.这迫使Java自动取消整理整数.如果Integer projectId为null,那么你会得到一个NullPointerException(因为自动取消装箱),所以你甚至不会进入if块.因此,projectId不能为null.