• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Optional labels

 
Jason Attin
Ranch Hand
Posts: 232
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys, in a book about the OCA exam (it has only exercises, here is the book) there is a question about optional labels, here is the code:

This apparently print numbers from 0 to 4.
My answer was that it wouldn't compile because I thought that a label couldn't be assigned to a System.out statement as they do in here.
ANy idea?


 
Paweł Baczyński
Bartender
Posts: 1878
35
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JLS says a labeled statement has the form:
Identifier: Statement

So it is allowed to put a label before any statement.
 
Henry Wong
author
Marshal
Pie
Posts: 22124
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Java labels may be applied to any Java statement (except declarations, I believe) -- so, your code example should compile.

Henry
 
Jason Attin
Ranch Hand
Posts: 232
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for that guys. Is the JLS only available in pdf format as far as you know ?
 
Maneesh Godbole
Saloon Keeper
Posts: 11342
16
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nope. Also available as HTML
Check out https://docs.oracle.com/javase/specs/
 
Campbell Ritchie
Sheriff
Posts: 51456
87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That code will of course fail to compile because the ++ operator has been split by included space. I presume that is a copying error. It sho‍uld read c++ without any spaces, for the best formatting.
 
Ganesh Patekar
Bartender
Posts: 696
23
Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It would have been great If we had downloadable HTML documention (Offline, not online) of JLS as we have of Java™ Tutorials and Java SE Doc, I found only Pdf of JLS.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic