退货后的陈述

时间:2011-04-21 12:09:14

标签: android return

大家好,这个问题可能看起来很傻。但我仍然需要知道这个答案

public void onCreate(Bundle paramBundle)
{
    super.onCreate(paramBundle);


    while (true)
    {
      return;
      Toast.makeText(this, "GPS Tracker running in the background", 0).show();
    }
}

在上面的代码中,要显示的吐司有任何变化。据我所知,我不这么认为。但是当我使用jd-gui-0.3.3.linux.i686查看.class文件时,我看到了这样的代码。 任何人都可以澄清它。

2 个答案:

答案 0 :(得分:4)

返回后的语句在Java中具有明确定义的名称。这些陈述是“无法到达的代码”。在返回执行一个例外之后,代码(与所涉及的返回的范围相同)不可能执行; finally块中的代码将执行。这是一个例子:

try
{
  String schmarr;
  ... blah
  return;
  System.out.println("OMG!  Schmarr: " + schmarr); // This is unreachable.
}
finally
{
  System.out.println("Not so OMG.  code in a finally will execute");
}

答案 1 :(得分:1)

不,return语句会将控制权交还给调用方法。

许多IDE会将Toast标记为无法访问的语句。