我对此感到困惑。我们大多数人都听说过Java中没有goto语句。

但我发现它是Java中的关键字之一。它可以在哪里使用?如果它不能使用,那么为什么它作为关键字包含在Java中?


当前回答

http://java.sun.com/docs/books/tutorial/java/nutsandbolts/_keywords.html

关键字const和goto是 矜持,尽管事实并非如此 目前使用。”

其他回答

James Gosling创建了支持goto语句的原始JVM,但后来他删除了这个不必要的特性。没有必要使用goto的主要原因是,通常可以用可读性更好的语句(如break/continue)或将一段代码提取到方法中来代替它。

来源:James Gosling,问答环节

是的,这是可能的,但不像在c#(在我看来,c#更好!)那些总是模糊软件的观点是愚蠢的!很遗憾java没有至少goto case xxx。

跳转向前:

 public static void main(String [] args) {
   myblock: {
     System.out.println("Hello");
     if (some_condition)
       break myblock; 
     System.out.println("Nice day");
   }
   // here code continue after performing break myblock
   System.out.println("And work");
 }

向后跳:

 public static void main(String [] args) {
   mystart: //here code continue after performing continue mystart
   do {
     System.out.println("Hello");
     if (some_condition)         
       continue mystart; 
     System.out.println("Nice day");
   } while (false);
   System.out.println("And work");
 }

它被认为是不能做的事情之一,但可能被列为保留词,以避免开发人员感到困惑。

goto不在Java中

你必须使用GOTO 但它不能正常工作。在关键的Java词中,它没有被使用。 http://docs.oracle.com/javase/tutorial/java/nutsandbolts/_keywords.html

   public static void main(String[] args) {            
            GOTO me;
            //code;
            me:
            //code; 
            }   
   }

http://java.sun.com/docs/books/tutorial/java/nutsandbolts/_keywords.html

关键字const和goto是 矜持,尽管事实并非如此 目前使用。”