Java Reference
In-Depth Information
public static void main ( String [ ] args )
System . out . print ( "Volume of the box (in cubic meter):" );
// Result is in cubic meter unit
System . out . println (0.5
1
0.2) ;
}
}
1.5 Indenting programs
Java source codes are parsed by the compiler javac , which checks the syntax
of the program and generates an overly optimized bytecode that is a low-level
machine-interpretable bytecode. Indenting a program consists of making the
source code prettier by adding a few extra spaces or return lines. Indenting
consists of applying several conventions like aligning columns of opening/closing
braces, etc. Code indentation does not change the bytecode. For example, the
following badly indented source code will produce the same bytecode although
it obfuscates its readibility.
class NotIndentedVolumeBox { public static void main ( String [
]
args) { System . out . print ( "Volume of the box (in cubic
meter):" ) ; System . out . println (0.5 1 0.2) ; }}
As a rule of thumb, it is important for programmers to comment and indent
source codes well to improve their readibility. Indenting also helps browsing
lengthy source codes.
1.6 Variables, assignments and type checking
Suppose now that we would like to compute the balance of a set of credits
with a set of debits. We first need to compute the total credit figure, then the
total debit figure and subtract these two figures to get the balance. Computing
the total credit and debit can be done and displayed using the former syntax
System.out.println(myExpression); . But what about the balance? For example,
consider we have two credit lines (say, 100 and 150 dollars), and three debit
lines (50, 25 and 100 dollars). Then, we would have the following code:
Program 1.7 Sketch of the balance sheet program
class BalanceSheet
{
public static void main ( String [
]
args )
 
Search WWH ::




Custom Search