1

I know that the goto is a keyword in java. Also, from wiki, I can excerpt that, "It is not used and has no function".

I wonder, why goto is declared as a keyword if it has no use and not used anywhere? Did anyone ever used goto in the java program? Are there any other keywords that are like goto declared in the java programming but had metaphorically no use.

Where do we use goto in our daily programming or rarely when?

srk
  • 4,857
  • 12
  • 65
  • 109
  • The little [2] right there leads to "The keywords const and goto are reserved, even though they are not currently used. This may allow a Java compiler to produce better error messages if these C++ keywords incorrectly appear in programs." – chris Apr 20 '13 at 05:28
  • Already posted on SO. – JDGuide Apr 20 '13 at 05:36

5 Answers5

4

Here is a bit of summary about goto statements

Usage:

They are used to jump out of a block of statements in a language, JMP instruction does this work at assembly level.

Goto's are strictly prohibited as they make the program difficult to trace Excessive jumps in a code makes it slow. Imagine a loop having goto inside it. Its not at all good.

Java reserved this keyword may be as it also involves some native code inside the jvm(that's a bit of C/C++) code, may be to avoid ambiguity.

Java provides break as an implementation of goto but its usage is also limited and should not be used often.

Here's an example of break as a GOTO in java

Aniket Kapse
  • 315
  • 3
  • 20
cafebabe1991
  • 4,928
  • 2
  • 34
  • 42
2

To find out the real reason why goto and const were originally reserved, you would need to ask the Java language designers. The decision would have been made when the language was called Oak.

How ever there are a couple of plausible explanations:

  • They thought they might need to extend the language in a way that might be use these keywords, and they were keeping their options open.

  • They wanted to avoid cognitive dissonance for C/C++ programmers reading Java code where some misguided person decided to use "goto" or "const" as a variable or method name.

But the plain fact is that there is zero chance that these reserved words will ever be used now.


UPDATE

Apparently Java did have goto at one point.

"One of the things I did was there were some issues with goto. Java had a goto at one point. I did this study of what people were doing with goto? And based on that study of a half million lines of code, I just got rid of it."

From "James Gosling on Java, May 2001".

So the real explanation is that goto is reserved because it was used in early versions of Java, and they decided to get rid of it.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
1

The reason that it is declared and not used is to reserve it in case it is needed in the future. It currently serves no purpose.

const is another reserved word which doesn't do anything. Since they are reserved, you cannot use them in your program (as variables).

Igor
  • 33,276
  • 14
  • 79
  • 112
0

The Java keyword list specifies the goto keyword, but it is marked as "not used". See Java Keyword list to know what are other keywords that are like goto declared in the java programming but has metaphorically no use.

startAgain:
for(int i =0; i <10 ;i++){
    for(int i =0; i <10 ;i++){
        if(someCondition)
        break startAgain;

    }

}

Above code is somehow similar to goto.

Achintya Jha
  • 12,735
  • 2
  • 27
  • 39
0

go to statement is available in java.but it is marked as "not used".

You can use labled break statement instead of goto

BREAK

PSR
  • 39,804
  • 41
  • 111
  • 151