O'Reilly logo

Core Java® Volume II—Advanced Features, Ninth Edition by Gary Cornell, Cay S. Horstmann

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

12.2.1. Using printf for Formatting Numbers

Until Java SE 5.0, Java had no direct analog of the C printf function. In the following examples, we will suppose you are stuck with an ancient JDK release and decide to implement the same functionality by calling the C printf function in a native method.

Listing 12.5 shows a class called Printf1 that uses a native method to print a floating-point number with a given field width and precision.

Listing 12.5. printf1/Printf1.java

 1  /**  2   * @version 1.10 1997-07-01  3   * @author Cay Horstmann  4   */  5  class Printf1  6  {  7     public static native int print(int width, int precision, double x);  8  9     static 10     { 11        System.loadLibrary("Printf1"); 12     } ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required