DEV Community

Arshad Ali
Arshad Ali

Posted on

JAVA- Printf



    public class Main {

        public static void main(String[] args) {

        //printf() = an optional method to control, format, and display text to the console windows
        //          two arguments = format string + (object/variable/value)
        //          % [flages] [precision] [conversion-character]

        boolean myBoolean = true;
        char myChar = '@';
        String myString = "Bro";
        int myInt = 50;
        double myDouble = 1000;

        //Conversion Character
        //System.out.printf("%b", myBoolean);
        //System.out.printf("%c", myChar);
        //System.out.printf("%s", myString);
        //System.out.printf("%d", myInt);
        //System.out.printf("%f", myDouble);

        //Width
        //minimum number of characters to be written as output
        System.out.println("Hello %s", myString);

        //precision
        //sets number of digits of precision when outputting floating-point values
        //System.out.println("You have this money %.2f ", myDouble);

        //flags
        //adds an effect to output based on the flag added to formate specifier
        // - : left-justify
        // + : output a plus (+) or minus(-) sign for a numeric value
        // 0 : numeric values are zero-padded
        // , : comma grouping separator if numbers > 1000
    }           

}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)