[英]Why do I get a “dead code” warning in this Java code?
Would someone please tell me why i am getting a dead code warning in the else branch of if (projectId != null)
? 有人请告诉我为什么我在if (projectId != null)
的else分支中收到死代码警告? If i got this right, the interpreter thinks projectId
can never be null - is that right? 如果我做对了,解释器认为projectId
永远不会为空 - 是吗? In my opinion this is not possible... 在我看来,这是不可能的......
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)
{
...
}
Even if i put a 即使我放了一个
sprintTaskConnections.add(new JiraSprintTaskConnection(1, 1, 1));
or a 或者a
sprintTaskConnections.clear();
in front of 在...前面
if (!sprintTaskConnections.isEmpty())
projectId = sprintTaskConnections.get(0).getProjectId();
the result is always the same! 结果总是一样的!
Please help me, i just don't get it at the moment! 请帮助我,我现在还没有得到它!
I don't have the JiraSprint
code, so I can't confirm this, but I suspect the JiraSprint constructor takes an int
where you pass in the projectId rather than an Integer
. 我没有JiraSprint
代码,所以我无法确认这一点,但我怀疑JiraSprint构造函数在传递projectId而不是Integer
地方采用了int
。 That forces Java to auto-unbox the Integer. 这迫使Java自动取消整理整数。 If the Integer projectId
is null, you'll get a NullPointerException
(because of the auto-unboxing) there, so you won't even get to the if block. 如果Integer projectId
为null,那么你将得到一个NullPointerException
(因为自动取消装箱),所以你甚至都不会进入if块。 Therefore, projectId must not be null. 因此,projectId不能为null。
Maybe: new JiraSprint(..)
gets int projectId
? 也许: new JiraSprint(..)
得到int projectId
?
In that case, if projectId
is null
, this line: 在这种情况下,如果projectId
为null
,则此行:
JiraSprint sprint = new JiraSprint(sprintInfo.getInt("id"), name, state, projectId, startDate, endDate);
will throw NPE, so projectId
cannot be null
after this line. 将抛出NPE,因此在此行之后projectId
不能为null
。
projectId很有可能永远为null,因此“if”中的代码永远不会被执行
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.