tags:

views:

790

answers:

9

Why is the goto keyword in java reserved though it is not used?

EDIT : Just to rephrase the question once again to avoid the ambiguity, I understand goto functionality is not supported in java and also understand that it makes code difficult to read. But its not supported, so then why make it reserved. It can be used as variable names atleast

+11  A: 

So they could be used one day if the language designers felt the need.

Also, if programmers from languages that do have these keywords (eg. C, C++) use them by mistake, then the Java compiler can give a useful error message.

Or maybe it was just to stop programmers using goto :)

dave
reasonable !! :)
Ajay
A: 

To prohibit declarations of variables with the same name.

e.g. int i = 0, goto;

+3  A: 

Because it's not supported and why would you want a goto keyword that did nothing or a variable named goto.

Although you can use break label; and continue label; statements to effectively do what goto does. But I wouldn't recommend it.

public static void main(String [] args) {

     boolean t = true;

     first: {
        second: {
           third: {
               System.out.println("Before the break");

               if (t) {
                  break second;
               }

               System.out.println("Not executed");

           }

           System.out.println("Not executed - end of second block");

        }

        System.out.println("End of third block");

     }
}
pjp
-1 Why should I not be allowed to have a variable or method named goto?
Michael Borgwardt
Because it has a meaning in the land of programming which is not applicable to Java. It's also a poor choice of variable name.
pjp
then wht about words like print, read , etc?
Ajay
What would a variable 'print' be used for? The words that you have mentioned are more like method names than variables or keywords.
pjp
-1: First of all, just because Java doesn't support "goto" flow control statement, doesn't mean that's the reason it has the "goto" keyword that doesn't do anything. Second, "goto" might be a poor variable name, but it can be an excellent method name, which we can't use because "goto" is a keyword. Third, "break label" and "continue label" exist for a very good reason, so "not recommending it" is not very useful. And fourth, since Sun says it's "not currently used", it most likely means that they once planned to implement , but didn't want to get rid of the keyword when they decided otherwise.
Vojislav Stojkovic
"not recommending it" = it's poor *style* - not that `break` and `continue` are bad
pjp
A: 

Because 'continue' already satisfies the purpose

Niger
That doesnt answer the question!
Ajay
A: 

For no good reason whatsoever!

Sindri Traustason
+11  A: 
Alceu Costa
Nice!
Zoidberg
+1  A: 

See the following link is shows all java reserved words and tells you what versions they where added.

http://java.sun.com/docs/books/tutorial/java/nutsandbolts/%5Fkeywords.html

goto is reserved, even though it is not currently used, never say ever however :)

Paul Whelan
A: 

Maybe it will be like in PHP - they've added goto keyword in PHP 5.3.. So maybe it's only not yet implemented in Java (LOL)

Martin Lazar
hmm.. possible :)
Ajay
+1  A: 

Note that you can replace most of the benign uses of goto by

  • return

  • break

  • break label

  • throw inside try-catch-finally

starblue
Exceptions should NEVER be used for flow control
ChssPly76
Exceptions _are_ used for flow control, but should be used only for exceptional cases. For example, one of the uses of goto in C is error-handling, especially if it involves cleaning up.
starblue