您的位置:寻梦网首页编程乐园Java天地Core JavaJava Lecture Notes
Introduction
Content
Apply
Reflect
Extend
previous.gif
 (3087 bytes)
next.gif
 (2959 bytes)

 
 
Content Index
Content Page # 15

Layout convention: use of spaces

Java treats multiple space characters as if there were only one, unless they occur inside double quotation marks as part of a string. Also there are many parts of Java statements where no space character is needed, but where such a character can help to make the Java code easier to read for a human. Each of the following statements is treated just the same by the Java compiler:

if((cost<2000)&&(product=="car"))
if( (cost<2000) && (product=="car") )
if( (cost < 2000) && (product == "car") )
if( ( cost < 2000 ) && ( product == "car" ) )
if( ( cost < 2000) && ( product == "car" ) )
Most programmers would agree that in the last line the use of space characters does not help a human reader of the program see that there are 2 parts to the expression, i.e.(cost<2000) and (product=="car"). However, as to which of the first four lines is most clear might be the subject of some debate. 

We would recommend you do use some space characters to help show the different parts of a statement (which would rule out the first line), however as long as you are consistent in your usage, any of the styles of the middle three lines above would be reasonable.

As a general rule, when a message is being sent to an object that contains arguments, inserting a space after each comma in the argument list does make the statement easier for a human to read. Thus rather than this form:

balls[0] = new Ball(100,100,radius,4,5);
we would recommend you insert the spaces after commas as follows:
balls[0] = new Ball(100, 100, radius, 4, 5);
Back to top

basicline.gif (169 bytes)

RITSEC - Global Campus
Copyright © 1999 RITSEC- Middlesex University. All rights reserved.
webmaster@globalcampus.com.eg