These are prefix for date and time conversion characters Results in platform specific line separator Results in hex string based on the output of hashCode() method Results in decimal number in computerized scientific notationįormat is based on computerized scientific notation or decimal format, depending on the precision and the value after rounding. String.format(“value is %b”,”a”.equals(“a”)) īyte, Byte, short, Short, int, Integer, long, Long and BigInteger If arg is a boolean or Boolean, then the result is the string returned by String.valueOf(arg). If the argument arg is null, then the result is “false”. The result is a hexadecimal representation of the floating point number Lets take a look at the available conversion specifiers along with their examples. As the specifiers are the ones that tells how the conversion has to be processed and where to add the arguments in the text. Java String Format conversion specifiers are the ones which plays a major role in String formatting. We can use formatter with StringBuffer and StringBuilder as well.įormatter.format("value is %f",32.33434) Java String Fromatter with StringBuffer and StringBuilder ("Error occurred with cause: %s", ex.getMessage()) ("The result of %d / %d = %d", num1,num2,result) Also to provide a custom error messages on console. (): Mostly used to print an error on console.("The result of %d * %d = %d", num1,num2,result) (): Most commonly used for printing on console for debugging purpose or for printing an output on the screen.The examples for the above mentioned methods are provided below. Printf() and format() methods behave in the same way. In order to get formatted strings on console we need to use the following methods. There are situation and scenarios when we would like to have formatted string appear on the console the usage of it can vary from debugging to better readable code. If the locale is not specified in String.format() method, it uses default locale by calling Locale.getDefault() method. The format method also uses locale for formatting a string. It contains a format specifier “%s” which tells how the argument should be processed and where it will be placed in the output. In the example above, “My name is %s” is the format string. The String.format() method returns the formatted String based on the format String and the arguments provided to the method. String Formatter benefit is clearly visible where the format string is read from a property file.With String concatenation we have to do the conversion between different objects to strings.If formatted string is long then string concatenation cause readability issues.String concatenation is a costly affair.