Other Conversion Characters

The remaining conversion characters are b, B, h, H, % and n. These are described in Fig. 28.10.

Figure 28.10. Other conversion specifiers.



b or B

Print "true" or "false" for the value of a boolean or Boolean. These conversion characters can also format the value of any reference. If the reference is non-null, "TRue" is output; otherwise, "false" is output. When conversion character B is used, the output is displayed in uppercase letters.

h or H

Print the string representation of an object's hash code value in hexadecimal format. If the corresponding argument is a null reference, "null" is printed. When conversion character H is used, the output is displayed in uppercase letters.


Print the percent character.


Print the platformspecific line separator (e.g., on Windows or on UNIX/LINUX).

Lines 910 of Fig. 28.11 use %b to print the value of boolean values false and TRue. Line 11 associates a String to %b, which returns true because it is not null. Line 12 associates a null object to %B, which displays FALSE because test is null. Lines 1314 use %h to print the string representations of the hash code values for strings "hello" and "Hello". These values could be used to store or locate the strings in a Hashtable or HashMap (both discussed in Chapter 19, Collections). Note that the hash code values for these two strings differ because one string starts with a lowercase letter and the other starts with an uppercase letter. Line 15 uses %H to print null in uppercase letters. The last two printf statements (lines 1617) use %% to print the % character in a string and %n to print a platformspecific line separator.

Figure 28.11. Using the b, B, h, H, % and n conversion characters.

(This item is displayed on page 1336 in the print version)

 1 // Fig. 28.11: OtherConversion.java
 2 // Using the b, B, h, H, % and n conversion characters.
 4 public class OtherConversion
 5 {
 6 public static void main( String args[] )
 7 {
 8 Object test = null;
 9 System.out.printf( "%b
", false );
10 System.out.printf( "%b
", true );
11 System.out.printf( "%b
", "Test" );
12 System.out.printf( "%B
", test );
13 System.out.printf( "Hashcode of "hello" is %h
", "hello" );
14 System.out.printf( "Hashcode of "Hello" is %h
", "Hello" );
15 System.out.printf( "Hashcode of null is %H
", test );
16 System.out.printf( "Printing a %% in a format string
" );
17 System.out.printf( "Printing a new line %nnext line starts here" );
18 } // end main
19 } // end class OtherConversion
Hashcode of "hello" is 5e918d2
Hashcode of "Hello" is 42628b2
Hashcode of null is NULL
Printing a % in a format string
Printing a new line
next line starts here

Common Programming Error 28.2

Trying to print a literal percent character using % rather than %% in the format string might cause a difficult-to-detect logic error. When % appears in a format string, it must be followed by a conversion character in the string. The single percent could accidentally be followed by a legitimate conversion character, thus causing a logic error.

Introduction to Computers, the Internet and the World Wide Web

Introduction to Java Applications

Introduction to Classes and Objects

Control Statements: Part I

Control Statements: Part 2

Methods: A Deeper Look


Classes and Objects: A Deeper Look

Object-Oriented Programming: Inheritance

Object-Oriented Programming: Polymorphism

GUI Components: Part 1

Graphics and Java 2D™

Exception Handling

Files and Streams


Searching and Sorting

Data Structures



Introduction to Java Applets

Multimedia: Applets and Applications

GUI Components: Part 2



Accessing Databases with JDBC


JavaServer Pages (JSP)

Formatted Output

Strings, Characters and Regular Expressions

Appendix A. Operator Precedence Chart

Appendix B. ASCII Character Set

Appendix C. Keywords and Reserved Words

Appendix D. Primitive Types

Appendix E. (On CD) Number Systems

Appendix F. (On CD) Unicode®

Appendix G. Using the Java API Documentation

Appendix H. (On CD) Creating Documentation with javadoc

Appendix I. (On CD) Bit Manipulation

Appendix J. (On CD) ATM Case Study Code

Appendix K. (On CD) Labeled break and continue Statements

Appendix L. (On CD) UML 2: Additional Diagram Types

Appendix M. (On CD) Design Patterns

Appendix N. Using the Debugger

Inside Back Cover

show all menu

Java(c) How to Program
Java How to Program (6th Edition) (How to Program (Deitel))
ISBN: 0131483986
EAN: 2147483647
Year: 2003
Pages: 615
Similar book on Amazon

Flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net