Win a copy of Building Blockchain Apps this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
Sheriffs:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
Bartenders:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

Interesting Syntax

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Consider the following syntax in let's say the main method:
if (args.length == 0);;;;;; {//COMPILES!!!
System.out.println("Usage: HelpTest [argument]");
}
/*else*/ {
System.out.println("Invalid Argument");
}
This actually appeared in a mock exam.
For me, there are 3 possible explanations:
A. I had too much wine over Christmas and now I am brain dead
B. My compiler went crazy
C. There is a catch here
Could someone explain to me what's going on here??
Thanks
 
Author
Posts: 836
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I assume that you refer particularly to the fact that there are multiple semi-colons terminating a single line?
I have experienced the same "phenomena" (quoted since there is a true explanation), and I put it down to the fact that a semi-colon with no further statements is used to terminate a line of the code (or an expression such as an initialisation or the three sections of a for(...) statement).
Hence, since there is nothing significant between the semi-colons in your example, the compiler will interpret this syntax as being empty lines of whitespace and just ignore the whole sequence.
The following will also work:

In essence: Java code lines (more particularly statements) are terminated by semi-colons, and not by new line white-space characters in the text.
Now, back to the conditional statements:
It is perfectly legal to have the following code (i.e. without the curly braces around the if block):

In the situation where an if block does not have curly braces, only the line following the if(...) statement will be conditionally tested. The rest of the code will execute outside of the conditional block.
Hence, if we use the set of semi-colons after the if(...) statement, we simply declare an empty conditional block - the if(...) statement with there being nothing in the next line of code to execute regardless of whether the condition is satisfied or not.
The else block in your example must be commented out since it does not immediately follow the if(...) block statement (due to the extra lines created by the semi-colon sequence). If you uncomment that statement, the compiler will complain.
Finally you need to understand the significance, or lack of in this case, of the curly braces: when they are used without some form of qualifying statement (such as a conditional block or a loop initialisation), they don't do anything. For example:

is exactly the same as if the curly brace block had been totally omitted.
In terms of the exam, I can't really see them asking any sorts of confusing questions such as this; they are more likely to test your knowledge of operator precedence, and the difficult topics like threads and the GC.
Sorry that was a long explanation, but there were several points to cover. Hope that helps!
[ January 02, 2004: Message edited by: Charles Lyons ]
 
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello karolos,
From JLS


14.6 The Empty Statement
An empty statement does nothing.
EmptyStatement:
;
Execution of an empty statement always completes normally.


A succession of empty statements is okay for the compiler, you can't falsify a non-existing statement, right? Have you ever tried to compile an empty *.java file? Try it an see what happens.


{
System.out.println("Invalid Argument");
}


This is a valid code block, it becomes handy when dealing with anonymouse classes. Anonymouse classes doesn't have a constructor, we initialize them using 'inistance initializer'.
An instance initializer is a block of code (not a method!) that automatically runs every time you create a new instance of an object. In effect, it is an anonymous constructor! Of course, instance initializers are rather limited; because they don�t have arguments, you can�t overload them, so you can have only one of these �constructors� per variable.
For more information about instance initializers see JLS 8.6.

Hope this helps.
 
karolos ignatiadis
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's clear to me now.
Charles-Vicken, thank you very much for your help, I really appreciate it!
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just an interesting historical note... the Java behavior of allowing extraneous empty statements is a legacy of a bug in Sun's javac compiler. The original JLS did not allow that syntax, but a bug in the compiler did... and the bug stayed unfixed for so long, they finally just amended the JLS to make it match the behavior of the compiler.
For more details, see: http://developer.java.sun.com/developer/bugParade/bugs/4057172.html
or
http://developer.java.sun.com/developer/bugParade/bugs/4063150.html
[ January 03, 2004: Message edited by: Phil Rhodes ]
 
It runs on an internal combustion engine. This ad does not:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!