Printing Integers

An integer is a whole number, such as 776, 0 or 52, that contains no decimal point. Integer values are displayed in one of several formats. Figure 28.1 describes the integral conversion characters.

Figure 28.1. Integer conversion characters.

Conversion character

Description

d

Display a decimal (base 10) integer.

o

Display an octal (base 8) integer.

x or X

Display a hexadecimal (base 16) integer. X causes the digits 09 and the letters AF to be displayed and x causes the digits 09 and af to be displayed.

Figure 28.2 prints an integer using each of the integral conversions. In lines 910, note that the plus sign is not displayed by default, but the minus sign is. Later in this chapter (Fig. 28.14) we will see how to force plus signs to print.

Figure 28.2. Using integer conversion characters.

 1 // Fig. 28.2: IntegerConversionTest.java
 2 // Using the integral conversion characters.
 3
 4 public class IntegerConversionTest
 5 {
 6 public static void main( String args[] )
 7 {
 8 System.out.printf( "%d
", 26 );
 9 System.out.printf( "%d
", +26 );
10 System.out.printf( "%d
", -26 );
11 System.out.printf( "%o
", 26 );
12 System.out.printf( "%x
", 26 );
13 System.out.printf( "%X
", 26 );
14 } // end main
15 } // end class IntegerConversionTest
 
26
26
-26
32
1a
1A
 

The printf method has the form


       printf( format-string, argument-list );

where format-string describes the output format, and argument-list contains the values that correspond to each format specifier in format-string. There can be many format specifiers in one format string.

Each format string in lines 810 specifies that printf should output a decimal integer (%d) followed by a newline character. At the format specifier's position, printf substitutes the value of the first argument after the format string. If the format string contained multiple format specifiers, at each subsequent format specifier's position, printf would substitute the value of the next argument in the argument list. The %o format specifier in line 11 outputs the integer in octal format. The %x format specifier in line 12 outputs the integer in hexadecimal format. The %X format specifier in line 13 outputs the integer in hexadecimal format with capital letters.

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

Arrays

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

Recursion

Searching and Sorting

Data Structures

Generics

Collections

Introduction to Java Applets

Multimedia: Applets and Applications

GUI Components: Part 2

Multithreading

Networking

Accessing Databases with JDBC

Servlets

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



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

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