%b, %c, %d, %f, %s How does this work in java? I have been trying to read Formatter class and formattable interface however, I am unable to understand in regards to the conversions passed as arguments.
For example:
System.out.printf("%f not equals %b", Math.PI, Math.E)
Even though formatter such as %b, %c, %d, %f, %s are limited in the ocjp6 exam, it feels like huge topic to prepare
I think you are having trouble understanding how System.out.printf() works. It is very simple once you get the idea.
You orginal question was regarding below
Here System.out.printf is trying to output a String . %f and %b can be understood as placeholder with special meaning.
Placeholders because they will get replaced with the data that comes after comma. In this case %f is replaced with value of Math.PI and %b is replaced with value of Math.E
Special meaning because each formatter stands for somethings for example as mention above
Now to write you orginal query in a simple manner
Here %f (meaning decimal float) is replaced with float value 3.14 and %b is replaced with value "true".
if you switch %f and %b in the above like
But this will work
System.out.printf("%b is not equal to %f", 3.14, 3.14);// will work because 3.14 will evaluate to true The above works because of some automatic type conversion by java. But you can look into that later.Now Regarding your last question what is happening in
I am guessing you meant printf .
Now all formatters and their explanation are present in the link
http://docs.oracle.com/javase/7/docs/api/java/util/Formatter.html
It might look intimidating . but it is quite easy.
So Basically what %+04.2f means is show a positive sign for all positive numbers.The number should have 4 characters in total and two after decimal. It should be formatted as floating point number.
More examples
Please go through the below links . It will help you understand the concept more easily
http://www.homeandlearn.co.uk/java/java_formatted_strings.html
https://answers.yahoo.com/question/index?qid=20101017181211AAbtWC0
https://docs.oracle.com/javase/tutorial/java/data/numberformat.html
http://alvinalexander.com/programming/printf-format-cheat-sheet