Java Reference
In-Depth Information
By default, the output is right justified. You can put the minus sign ( - ) in the format spec-
ifier to specify that the item is left justified in the output within the specified field. For exam-
ple, the following statements
right justify
left justify
System.out.printf( "%8d%8s%8.1f\n" , 1234 , "Java", 5.63 );
System.out.printf( "%-8d%-8s%-8.1f \n" , 1234 , "Java", 5.63 );
display
8
1234
8
8
Java
5.6
1234
Java
5.6
where the square box (
) denotes a blank space.
Caution
The items must match the format specifiers in exact type. The item for the format spec-
ifier %f or %e must be a floating-point type value such as 40.0 , not 40 . Thus, an int
variable cannot match %f or %e .
Tip
The % sign denotes a format specifier. To output a literal % in the format string, use %% .
3.36
What are the format specifiers for outputting a Boolean value, a character, a decimal
integer, a floating-point number, and a string?
Check
Point
3.37
What is wrong in the following statements?
a. System.out.printf( " %5d %d " , 1 , 2 , 3 ) ;
b. System.out.printf( " %5d %f " , 1 ) ;
c. System.out.printf( " %5d %f " , 1 , 2 ) ;
3.38
Show the output of the following statements.
a. System.out.printf( " amount is %f %e\n " , 32.32 , 32.32 ) ;
b. System.out.printf( " amount is %5.4f %5.4e\n " , 32.32 , 32.32 ) ;
c. System.out.printf( " %6b\n " , ( 1 > 2 )) ;
d. System.out.printf( " %6s\n " , " Java " ) ;
e. System.out.printf( " %-6b%s\n " , ( 1 > 2 ) , " Java " ) ;
f. System.out.printf( " %6b%-8s\n " , ( 1 > 2 ) , " Java " ) ;
3.17 Operator Precedence and Associativity
Operator precedence and associativity determine the order in which operators are
evaluated.
Key
Point
Section 2.11 introduced operator precedence involving arithmetic operators. This section
discusses operator precedence in more details. Suppose that you have this expression:
3 + 4 * 4 > 5 * ( 4 + 3 ) - 1 && ( 4 - 3 > 5 )
What is its value? What is the execution order of the operators?
The expression in the parentheses is evaluated first. (Parentheses can be nested, in which case
the expression in the inner parentheses is executed first.) When evaluating an expression without
 
 
Search WWH ::




Custom Search